一、非命名管道
非命名管道适用于父子进程之间通信。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
| #include <string.h> #include <sys/types.h>
int main() { pid_t result; int r_num; int pipe_fd[2]; char buf_r[100],buf_w[100]; memset(buf_r,0,sizeof(buf_r));
if(pipe(pipe_fd)<0) { printf("create pipe fail"); return -1; } result = fork(); if(result < 0) { printf("create sub procedure fail"); exit(0); } else if(result == 0) { close(pipe_fd[1]); if((r_num = read(pipe_fd[0],buf_r,100)) > 0) printf("sub procedure %d cha, %s \n",r_num,buf_r); close(pipe_fd[0]); exit(0); } else { close(pipe_fd[0]); printf("please input str \n"); scanf("%s",buf_w); if(write(pipe_fd[1],buf_w,strlen(buf_w))!= -1) printf("father write: %s\n",buf_w); close(pipe_fd[1]); waitpid(result,NULL,0); exit(0); } }
|
二、命名管道
进程间通信之FIFO,在阻塞模式下,只有当读和写模式都打开时才返回,否则一直阻塞;
非阻塞模式下,当读端没打开,则打开写端无效,返回错误。
1端
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
| #include <stdio.h> #include <unistd.h> #include <fcntl.h> #include <string.h> #include <stdlib.h> #include <sys/select.h> #include <sys/types.h> #include <sys/stat.h> #include <errno.h>
int main() { int i,rfd,wfd,len=0,fd_in; char str[32]; int flag,stdinflag; fd_set write_fd,read_fd; struct timeval net_timer; rfd=open("fifo2",O_RDONLY); wfd=open("fifo1",O_WRONLY); if(rfd<=0||wfd<=0) { printf("not created!\n"); mkfifo("fifo1",S_IWUSR|S_IRUSR|S_IRGRP|S_IROTH); mkfifo("fifo2",S_IWUSR|S_IRUSR|S_IRGRP|S_IROTH); wfd=open("fifo1",O_WRONLY); rfd=open("fifo2",O_RDONLY); } printf("this is 1!\n"); while(1) { FD_ZERO(&read_fd); FD_SET(rfd,&read_fd); FD_SET(fileno(stdin),&read_fd); net_timer.tv_sec=5; net_timer.tv_usec=0; memset(str,0,sizeof(str)); if(i=select(rfd+1,&read_fd,NULL,NULL,&net_timer)<=0) continue; if(FD_ISSET(rfd,&read_fd)) { read(rfd,str,sizeof(str)); printf("--------------\n"); printf("2:%s\n",str); } if(FD_ISSET(fileno(stdin),&read_fd)) { printf("--------------\n"); fgets(str,sizeof(str),stdin); len=write(wfd,str,strlen(str)); } } close(rfd); close(wfd); }
|
2端
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
| #include <stdio.h> #include <unistd.h> #include <fcntl.h> #include <string.h> #include <stdlib.h> #include <sys/select.h> #include <sys/types.h> #include <sys/stat.h> #include <errno.h>
int main() { int i,rfd,wfd,len=0,fd_in; char str[32]; int flag,stdinflag; fd_set write_fd,read_fd; struct timeval net_timer; wfd=open("fifo2",O_WRONLY); rfd=open("fifo1",O_RDONLY); if(rfd<=0||wfd<=0) { printf("not created!\n"); mkfifo("fifo1",S_IWUSR|S_IRUSR|S_IRGRP|S_IROTH); mkfifo("fifo2",S_IWUSR|S_IRUSR|S_IRGRP|S_IROTH); rfd=open("fifo1",O_RDONLY); wfd=open("fifo2",O_WRONLY); } printf("this is 2!\n"); while(1) { FD_ZERO(&read_fd); FD_SET(rfd,&read_fd); FD_SET(fileno(stdin),&read_fd); net_timer.tv_sec=5; net_timer.tv_usec=0; memset(str,0,sizeof(str)); if(i=select(rfd+1,&read_fd,NULL,NULL,&net_timer)<=0) continue; if(FD_ISSET(rfd,&read_fd)) { read(rfd,str,sizeof(str)); printf("--------------\n"); printf("2:%s\n",str); } if(FD_ISSET(fileno(stdin),&read_fd)) { printf("--------------\n"); fgets(str,sizeof(str),stdin); len=write(wfd,str,strlen(str)); } } close(rfd); close(wfd); }
|