1、具有亲缘关系的进程,使用FIFO通信
父进程向管道写入数据,子进程从管道中读出数据
code:
#include<sys/types.h> #include<sys/stat.h> #include<fcntl.h> #include<stdlib.h> #include<stdio.h> #include<unistd.h> #define FIFO "/tmp/fifo" int main() { pid_t pid; char buffer[80]; int fd; unlink(FIFO); //如果文件存在,先删除此文件 mkfifo(FIFO,0744); //创建管道 if((pid=fork())>0){ // parent char s[]="Hello pipe"; fd=open(FIFO,O_WRONLY); printf("fathe with pid of %d write data :%s/n",getpid(),s); //sleep(2); write(fd,s,sizeof(s)); close(fd); exit(EXIT_SUCCESS); } else if(pid==0){ // child //sleep(2); fd=open(FIFO,O_RDONLY); //sleep(2); read(fd,buffer,80); printf("child with pid of %d read data :%s/n",getpid(),buffer); close(fd); exit(EXIT_SUCCESS); } else{ perror("fork"); exit(EXIT_FAILURE); } return 0; }
结果:
fathe with pid of 2750 write data :Hello pipe
child with pid of 2751 read data :Hello pipe
PS:注意,在使用有名管道时,一定要使用两个进程分别打开其读端和写端,open操作
2、非亲缘关系进程,使用有名通道(FIFO)通信
codes:
发送数据进程:
root@ubuntu:/code/chap8# cat test5_input.c #include<unistd.h> #include<stdlib.h> #include<stdio.h> #include<string.h> #include<fcntl.h> #include<limits.h> #include<sys/types.h> #include<sys/stat.h> #define FIFO_NAME "/tmp/my_fifo" int main() { int pipe_fd; int res; char buffer[]="|Hello this a test about pipe|"; if(access(FIFO_NAME,F_OK)==-1){ //如果文件不存在 res=mkfifo(FIFO_NAME,0766); if(res!=0){ fprintf(stderr,"can't create fifo %s/n",FIFO_NAME); exit(EXIT_FAILURE); } } printf("Process %d:open it/n",getpid()); pipe_fd=open(FIFO_NAME,O_WRONLY); printf("the file descriptor is %d/n",pipe_fd); if(pipe_fd==-1){ exit(EXIT_FAILURE); } else{ //write res=write(pipe_fd,buffer,sizeof(buffer)); if(res==-1){ fprintf(stderr,"error on write/n"); exit(EXIT_FAILURE); } else{ printf("write %s with %d bytes /n",buffer,res); } } close(pipe_fd); printf("Process %d:finished/n",getpid()); return 0; }
读取数据进程:
root@ubuntu:/code/chap8# cat test5_input.c #include<unistd.h> #include<stdlib.h> #include<stdio.h> #include<string.h> #include<fcntl.h> #include<limits.h> #include<sys/types.h> #include<sys/stat.h> #define FIFO_NAME "/tmp/my_fifo" int main() { int pipe_fd; int res; char buffer[]="|Hello this a test about pipe|"; if(access(FIFO_NAME,F_OK)==-1){ //如果文件不存在 res=mkfifo(FIFO_NAME,0766); if(res!=0){ fprintf(stderr,"can't create fifo %s/n",FIFO_NAME); exit(EXIT_FAILURE); } } printf("Process %d:open it/n",getpid()); pipe_fd=open(FIFO_NAME,O_WRONLY); printf("the file descriptor is %d/n",pipe_fd); if(pipe_fd==-1){ exit(EXIT_FAILURE); } else{ //write res=write(pipe_fd,buffer,sizeof(buffer)); if(res==-1){ fprintf(stderr,"error on write/n"); exit(EXIT_FAILURE); } else{ printf("write %s with %d bytes /n",buffer,res); } } close(pipe_fd); printf("Process %d:finished/n",getpid()); return 0; }
结果:
发送:
root@ubuntu:/code/chap8 # ./run5_input
Process 2016:open it
the file descriptor is 3
write |Hello this a test about pipe| with 31 bytes
Process 2016:finished
读取:
root@ubuntu:/code/chap8 # ./run5_output
Process 2017: open fifo
the file descriptor is 3
the data read is |Hello this a test about pipe| with 31 bytes
Process 2017:finished