linux-C-命名管道

# gcc -o test12send test12send.c
test12send.c: In function ‘main’:
test12send.c:23: warning: incompatible implicit declaration of built-in function ‘exit’
test12send.c:26: warning: incompatible implicit declaration of built-in function ‘strcpy’
test12send.c:27: warning: incompatible implicit declaration of built-in function ‘strlen’
test12send.c:39: warning: incompatible implicit declaration of built-in function ‘exit’
test12send.c:46: warning: incompatible implicit declaration of built-in function ‘exit’
# gcc -o test12recv test12recv.c
test12recv.c: In function ‘main’:
test12recv.c:18: warning: incompatible implicit declaration of built-in function ‘memset’
test12recv.c:34: warning: incompatible implicit declaration of built-in function ‘exit’
# ./test12send &
[1] 11362
# ./test12recv &
[2] 11388
# process 11362 opening pipe !process 11362 result 3
23 bytes sending........
23 bytes sending........
46 bytes sended!
read process 11388 opening pipe !read process 11388 result 3
deepfuture.iteye.com
deepfuture.iteye.com
46 bytes readed!

[1]-  Done                    ./test12send
[2]+  Done                    ./test12recv
#
两个程序必须同时启动,这样才有效果。如果读进程没发现数据,则阻塞直到有数据写入管道。命名管道是阻塞的。
send:

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <limits.h>
#include <fcntl.h>

#define PIPE_NAME "/tmp/dpfifo"
#define BUFFER_SIZE PIPE_BUF

//PIPE_BUF为limits.h定义的管道最大容量
int main(void){
	  int pipe_fd;
	  int res;
	  int len;
	  int alllen;
	  char buffer[BUFFER_SIZE+1];//每次写数据用的缓冲区
	  int bytes_sent=0;
	  
	  if (access(PIPE_NAME,F_OK)==-1){//如果不存在PIPE_NAME,则建立
	  	  res=mkfifo(PIPE_NAME,0777);//命名管道 
	  	  if (res!=0){
	  	     perror("create pipe error!");	
	  	     exit(1);	  	     
	  	  }	
	  }
	  strcpy(buffer,"deepfuture.iteye.com\n");
	  len=strlen(buffer); 	  	
	  alllen= len*2;
	  //打开管道,管道都是FIFO
	  printf("process %d opening pipe !",getpid());
	  pipe_fd=open(PIPE_NAME,O_WRONLY);
	  printf("process %d result %d\n",getpid(),pipe_fd);
	  if (pipe_fd!=-1){//发送数据
	      while (bytes_sent<alllen){
	        res=write(pipe_fd,buffer,len);	
            printf("%d bytes sending........\n",res); 		        
	        if (res==-1){
	        	  perror("write error on pipe\n");
	  	          exit(1);	        	  
	        }
	        bytes_sent+=res;//res为本次写的字节数,bytes_sent为总字节数
	      }
	      close(pipe_fd);	
	  }	
	  else{
	  	  exit(1);
	  }
	  printf("%d bytes sended!\n",bytes_sent); 	
	  return 0;
}
	

 

recv:

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <limits.h>
#include <fcntl.h>

#define PIPE_NAME "/tmp/dpfifo"
#define BUFFER_SIZE PIPE_BUF

//PIPE_BUF为limits.h定义的管道最大容量
int main(int argc,char *argv[]){
	  int pipe_fd;
	  int res;
	  int len;
	  int alllen=30;
	  char buffer[BUFFER_SIZE+1];//每次写数据用的缓冲区
	  int bytes_read=0;
	  memset(buffer,'\0',BUFFER_SIZE+1);		  

	  //打开管道,管道都是FIFO
	  printf("read process %d opening pipe !",getpid());
	  pipe_fd=open(PIPE_NAME,O_RDONLY);
	  printf("read process %d result %d\n",getpid(),pipe_fd);
	  if (pipe_fd!=-1){//发送数据
	      do{
	        res=read(pipe_fd,buffer,BUFFER_SIZE);	
	        printf("%s",buffer); 			        
	        bytes_read+=res;//res为本次写的字节数,bytes_sent为总字节数
	      } while (res<=alllen);	 
	      
	      close(pipe_fd);	
	  }	
	  else{
	  	  exit(1);
	  }
	  printf("%d bytes readed!\n",bytes_read); 	
	  unlink(PIPE_NAME);
	  return 0;
  
}
	

 

你可能感兴趣的:(C++,c,linux,C#,gcc)