Unix-进程间通信
来源:互联网 发布:可信的淘宝韩妆店铺 编辑:程序博客网 时间:2024/04/28 10:18
1、有名管道间通信
read进程代码:
#include<sys/types.h>#include<sys/stat.h>#include<errno.h>#include<fcntl.h>#include<stdio.h>#include<stdlib.h>#include<string.h>#define FIFO "/tmp/myfifo"int main(int argc , char** argv){char buf_r[100];int fd;int nread;if((mkfifo(FIFO,O_CREAT | O_EXCL)<0)&& (errno!=EEXIST))printf("cannot create fifo\n");printf("prapare for reading bytes \n");memset(buf_r,0,sizeof(buf_r));fd = open(FIFO,O_RDONLY | O_NONBLOCK,0);if(fd == -1){perror("open");exit(1);}while(1){memset(buf_r,0,sizeof(buf_r));if((nread = read(fd,buf_r,100)==-1)){if(errno== EAGAIN)printf("no data \n");}elseprintf("read from FIFO %s \n",buf_r);sleep(1);}pause();}
write 代码:
#include<sys/types.h>#include<sys/stat.h>#include<errno.h>#include<fcntl.h>#include<stdio.h>#include<stdlib.h>#include<string.h>#define FIFO_SERVER "/tmp/myfifo"int main(int argc ,char** argv){int fd;char w_buf[100];int nwrite;fd = open(FIFO_SERVER,O_WRONLY | O_NONBLOCK,0);if(argc == 1){exit(-1);}strcpy(w_buf,argv[1]);if((nwrite = write(fd,w_buf,100))==-1){if(errno == EAGAIN)printf("the fifo is not read\n");}elseprintf("write %s to the fifo\n",w_buf);return 0;}
2、信号量通信
#include<signal.h>#include<stdio.h>#include<stdlib.h>void my_func(int sign_no){if(sign_no == SIGINT)printf("I have get SIGINT\n");else if(sign_no == SIGQUIT)printf("I have get SIGQUIT\n");}void main(){printf("Waiting for signal SIGINT or SIGQUIT\n");signal(SIGINT,my_func);signal(SIGQUIT,my_func);pause();exit(0);}
mysignal.c
运行该文件,在另一个终端中执行
kill -s SIGQUIT 'pid'
这里的pid为mysignal的进程号
通过kill向进程mysignal发送信号。mysignal得到信号后,打印相应的信息。
3、共享内存通信:
共享内存是被多个进程共享的一部分物理内存。共享内存是进程间共享数据的一种最快的方法,一个进程向共享内存区域写入了数据,共享这个区域的所有进程就可以立刻看到其中的内容
#include<stdlib.h>#include<stdio.h>#include<string.h>#include<errno.h>#include<unistd.h>#include<sys/stat.h>#include<sys/types.h>#include<sys/ipc.h>#include<sys/shm.h>int main(int argc , char**argv){int shmid;char *p_addr,*c_addr;if(argc != 2){fprintf(stderr,"Usage:%s \n \a",argv[0]);exit(1);}//创建共享内存对象if((shmid = shmget(IPC_PRIVATE,1024,IPC_CREAT)) == -1){fprintf("create Share Memory Error: %s\n",strerror(errno));exit(1);}//create Processif(0 !=fork())//father Process{ //把共享内存区对象映射到调用进程的地址空间p_addr = shmat(shmid,0,0);memset(p_addr,'\0',1024);strncpy(p_addr,argv[1],1024);wait(NULL);shmdt(p_addr);//断开共享内存连接shmctl(shmid,IPC_RMID,NULL);//IPC_RMID:删除这片共享内存exit(0);}else //child Process{<span style="white-space:pre"></span>sleep(1);c_addr = shmat(shmid,0,0);printf("Client get %s\n",c_addr);<span style="white-space:pre"></span>shmdt(c_addr);//断开共享内存连接exit(0);}}
4、消息队列
#include <sys/types.h>#include <sys/msg.h>#include <unistd.h>#include <string.h>#include <stdio.h>struct msg_buf{ int mtype; char data[255];};int main(){ key_t key; int msgid; int ret; struct msg_buf msgbuf; key = ftok("/tmp/1",'a');//创建key printf("key= [%x]\n",key); msgid = msgget(key,IPC_CREAT|0666);//创建一个新队列或打开一个存在的队列 if(msgid==-1) { printf("create error \n"); } msgbuf.mtype = 10; strcpy(msgbuf.data,"test haha"); ret=msgsnd(msgid,&msgbuf,sizeof(msgbuf.data),IPC_NOWAIT);//数据放到消息队列中 if(ret==-1) { printf("send error\n"); return -1; } memset(&msgbuf,0,sizeof(msgbuf)); ret=msgrcv(msgid,&msgbuf,sizeof(msgbuf.data),10,IPC_NOWAIT);//从消息队列中接收数据 if(ret==-1) { printf("receive error \n"); return -1; } printf("receive : %s\n",msgbuf.data); return 0;}
5、信号量
0 0
- 【Unix】进程间通信
- Unix-进程间通信
- 【UNIX】进程间通信
- Unix环境进程间通信
- Unix IPC进程间通信
- UNIX SOCKET 进程间通信
- Unix IPC进程间通信
- Unix IPC进程间通信
- Unix socket进程间通信
- unix中的进程间通信
- Unix进程间通信编程经验总结
- Unix环境进程间通信(一)
- Unix环境进程间通信(二)
- UNIX 进程间通信有几种方式?
- UNIX 进程间的通信--IPC
- Unix Linux进程间通信方式
- 进程间通信之 Unix Domain Socket
- 进程间通信的例子: socket unix
- POJ 1502_Dijkstra
- CSS3 圆角(border-radius)
- nodejs使用
- 多值赋值
- cocos2d-x(十二)Lua开发飞机大战-7-添加敌机
- Unix-进程间通信
- discuzz的_G变量详解
- 记录:50多行程序中找出多写的一个字母e
- 开源项目cardslib简单介绍和导入eclipse并运行的方法
- 关于java中关闭数据库链接的正确写法
- 第01章 概述(2)
- golang sync WaitGroup
- Pascal之父——Nicklaus Wirth——算法+数据结构=程序
- 11-2-职员薪水