由上图机制及相关资料 总结如下: ⚫ UNIX IPC :管道、 FIFO 、信号; ⚫ System V IPC :信号量、消息队列、共享内存; ⚫ POSIX IPC :信号量、消息队列、共享内存; ⚫ Socket IPC :基于 Socket 进程间通信 2、管道和 FIFO
2.1无名管道
普通管道 pipe:通常有两种限制,一是单工,数据只能单向传输;二是只能在父子或者兄弟进程间使用,较为局限,实用型不高;
2.2、FIFO· 该管道又称有名管道,去除了普通管道的第二种限制,并且允许在不相关(不是父子或兄
弟关系)的进程间进行通讯,一种更为灵活的方式。可以进行全双工的通讯。创建的管道可以确定好其路径,与路径绑定(当然也可以不用)。 2.3、编程实践//最低层的逻辑还是和无名管道流一样 有一方流出及一方接收 //Api: mkfifo //读端代码 #include// int mkfifo(const char *pathname, mode_t mode); #include #include #include #include int main() { char buf[30]={0}; int nread; if((mkfifo("./file",0666)==-1) && (errno != EEXIST)){ printf("make file errorn"); perror("why"); } int fd = open("./file",O_RDONLY); if(fd<0) { perror("open fd"); } printf("open fifo successn"); while(1){ read(fd,buf,30); printf("read from fifo buf:%sn",buf); sleep(1); } return 0; }
写端代码: #include2.3.1实践结果 2.消息队列 消息队列 消息队列是消息的链表,存放在内核中并由消息队列标识符标识,消息队列克服了信号传递信息少、管 道只能承载无格式字节流以及缓冲区大小受限等缺陷。消息队列包括 POSIX 消息队列和 System V 消息队列。 消息队列是 UNIX 下不同进程之间实现共享资源的一种机制,UNIX 允许不同进程将格式化的数据流以消息队列形式发送给任意进程,有足够权限的进程可以向队列中添加消息,被赋予读权限的进程则可以读走队列中的消息。 笔者测试了一下 2.1编码实践#include // int mkfifo(const char *pathname, mode_t mode); #include #include #include #include #include int main() { int cnt=0; char *str ="massage from fifo."; int fd = open("./file",O_WRONLY); printf("open fifo successn"); while(1){ write(fd,str,strlen(str)); printf("write to file buf: %sn",str); sleep(1); } close(fd); return 0; }
//获取 #include#include #include #include #include #include struct msgbuf{ long mtype;//消息类型 char mtext[128];//massage data }; //ssize_t msgrcv(int msqid, void *msgp, size_t msgsz, long msgtyp, int msgflg); //int msgsnd(int msqid, const void *msgp, size_t msgsz, int msgflg); //int msgget(key_t key, int msgflg); int main() { struct msgbuf readBuf; key_t key; key = ftok(".",'z'); printf("key:%xn",key); int msg_id = msgget(key,IPC_CREAT|0777);//creat msaaage queue fhui id if(msg_id == -1){ printf("get queue failedn"); } msgrcv(msg_id,&readBuf,sizeof(readBuf.mtext),888,0);//sort is 888 printf("get from que:%sn",readBuf.mtext); struct msgbuf sendbuf={988,"thanks for reach"}; msgsnd(msg_id,&sendbuf,strlen(sendbuf.mtext),0); msgctl(msg_id,IPC_RMID,NULL); return 0; }
#include2.2实践结果#include #include #include #include #include struct msgbuf{ long mtype;//消息类型 char mtext[128];//massage data }; //ssize_t msgrcv(int msqid, void *msgp, size_t msgsz, long msgtyp, int msgflg); //int msgsnd(int msqid, const void *msgp, size_t msgsz, int msgflg); //int msgget(key_t key, int msgflg); int main() { struct msgbuf sendBuf={888,"this is massage from queue"}; struct msgbuf readBuf; key_t key; key = ftok(".",'z'); printf("key:%xn",key); int msg_id = msgget(key,IPC_CREAT|0777);//duiliecsu meiyoujiuchuangjian duiliehao if(msg_id == -1){ printf("get queue failedn"); } msgsnd(msg_id,&sendBuf,strlen(sendBuf.mtext),0); printf("send over;n"); msgrcv(msg_id,&readBuf,sizeof(readBuf.mtext),988,0); printf("return from que:%sn",readBuf.mtext); msgctl(msg_id,IPC_RMID,NULL); //send massage return 0; }
发送消息的进程可以一直阻塞 直到有进程读取到消息再退出 不涉及文件操作 管道涉及文件操作。



