标签:进程间通信
#include<stdio.h> #include<unistd.h> #include<errno.h> #include<string.h> int main() { int _pipe[2]; int ret = pipe(_pipe); if(ret == -1 ) { printf("create pipe failed!\n"); return 2; } pid_t id = fork(); if(id < 0) { printf("fork error!\n"); return 0; } else if(id == 0) //child { close(_pipe[1]); char mesg[1000]; int i = 0; while(i < 5) { memset(mesg, '\0', sizeof(mesg)); read(_pipe[0], mesg, sizeof(mesg)); printf("%s\n",mesg); i++; } } else//father { close(_pipe[0]); int j = 0; while(j < 5) { char *_mesg_c = "i am you father!!!"; write(_pipe[1], _mesg_c, strlen(_mesg_c)+1); sleep(2); j++; } } return 1; }
#include<stdio.h> #include<unistd.h> #include<errno.h> #include<string.h> #include<sys/wait.h> int main() { int _pipe[2]; int ret = pipe(_pipe); if(ret == -1) { printf("create pipe error! errno code is: %d\n",errno); return 1; } pid_t id = fork(); if(id < 0) { printf("fork error!\n"); return 2; } else if(id == 0)//child { close(_pipe[0]); int i = 0; char *_mesg_c = NULL; while(i < 10) { _mesg_c = "i am child!"; write(_pipe[1], _mesg_c, strlen(_mesg_c)+1); sleep(1); i++; } close(_pipe[1]); } else//father { close(_pipe[1]); char _mesg[1000]; int j = 0; while(j < 100) { memset(_mesg, '\0', sizeof(_mesg)); int ret = read(_pipe[0], _mesg, sizeof(_mesg)); printf("%s, code is: %d\n",_mesg,ret); j++; } if(waitpid(id, NULL, 0) < 0) return 3; return 0; } }2、如果有指向管道写端的文件描述符没关闭(管道写端的引用计数大于0),而持有管道写进程也没有向管道中写数据,这时有进程从管道读端读数据,那么管道中剩余的数据都被读 取后,再次read会阻塞,直到管道中有数据可读了才读取数据并返回。
#include<stdio.h> #include<unistd.h> #include<errno.h> #include<string.h> #include<sys/wait.h> int main() { int _pipe[2]; int ret = pipe(_pipe); if(ret == -1) { printf("create pipe error! errno code is: %d\n",errno); return 1; } pid_t id = fork(); if(id < 0) { printf("fork error!\n"); return 2; } else if(id == 0)//child { close(_pipe[0]); int i = 0; char *_mesg_c = NULL; while(i < 20) { if(i < 10) { _mesg_c = "i am child!"; write(_pipe[1], _mesg_c, strlen(_mesg_c)+1); } sleep(1); i++; if(i >= 14) { _mesg_c = "i am child!"; write(_pipe[1], _mesg_c, strlen(_mesg_c)+1); } } close(_pipe[1]); } else//father { close(_pipe[1]); char _mesg[1000]; int j = 0; while(j < 20) { memset(_mesg, '\0', sizeof(_mesg)); int ret = read(_pipe[0], _mesg, sizeof(_mesg)); printf("%s, code is: %d\n",_mesg,ret); j++; } if(waitpid(id, NULL, 0) < 0) return 3; } <span style="white-space:pre"> </span>return 0; }
#include<stdio.h> #include<unistd.h> #include<errno.h> #include<string.h> #include<sys/wait.h> int main() { int _pipe[2]; int ret = pipe(_pipe); if(ret == -1) { printf("create pipe error! errno code is: %d\n",errno); return 1; } pid_t id = fork(); if(id < 0) { printf("fork error!\n"); return 2; } else if(id == 0)//child { close(_pipe[0]); int i = 0; char *_mesg_c = NULL; while(i < 20) { if(i < 10) { _mesg_c = "i am child!"; write(_pipe[1], _mesg_c, strlen(_mesg_c)+1); } sleep(1); i++; } } else//father { close(_pipe[1]); char _mesg[1000]; int j = 0; while(j < 3) { memset(_mesg, '\0', sizeof(_mesg)); int ret = read(_pipe[0], _mesg, sizeof(_mesg)); printf("%s, code is: %d\n",_mesg,ret); j++; } close(_pipe[0]); sleep(10); if(waitpid(id, NULL, 0) < 0) return 3; } return 0; }4. 如果有指向管道读端的文件描述符没关闭(管道读端的引用计数大于0),而持有管道读端 进程也没有从管道中读数据,这时有进程向管道写端写数据,那么在管道被写满时再 次write会阻塞,直到管道中有空位置了才写入数据并返回。
#include<stdio.h> #include<sys/types.h> #include<sys/stat.h> #include<unistd.h> #include<fcntl.h> #include<string.h> #define _PATH_ "/tmp/file.tmp" #define _SIZE_ 100 int main() { int ret = mkfifo(_PATH_, 0777); if( ret < -1) { printf(" mkfile error!\n"); return 1; } int fd = open(_PATH_, O_WRONLY); if(fd < 0) { printf("open error!\n"); } char buf[_SIZE_]; memset(buf, '\0', sizeof(buf)); while(1) { fflush(stdin); scanf("%s",buf); int ret = write(fd, buf, sizeof(buf)+1); if(ret < 0 ) { printf("write error!\n"); break; } if(strncmp(buf, "quit", 4) == 0) { break; } close(fd); return 0; } }
#include<stdio.h> #include<sys/types.h> #include<sys/stat.h> #include<unistd.h> #include<fcntl.h> #include<string.h> #define _PATH_ "/tmp/file.tmp" #define _SIZE_ 100 int main() { int fd = open(_PATH_, O_RDONLY); if(fd < 0) { printf("open file error!\n"); return 1; } char buf[_SIZE_]; memset(buf, '\0', sizeof(buf)); while(1) { fflush(stdout); int ret = read(fd, buf, sizeof(buf)); if(ret <= 0 ) { printf("read end or error!\n"); break; } printf("%s\n",buf); // fflush(stdout); //sleep(2); if(strncmp(buf, "quit", 4) == 0) { break; } } close(fd); return 0; }
标签:进程间通信
原文地址:http://blog.csdn.net/lc331257229/article/details/46532657