进程间通信
来源:互联网 发布:nba百视通网络电视 编辑:程序博客网 时间:2024/05/29 14:31
在linux中实现共享内存方式的进程间通信
上面代码都在
linux:Ubuntu12.04
gcc:4.6.3 上运行通过。
分别有两个程序运行read.c和write.c
write.c从终端输入到全局定义的结构体shm_t的buf[]数组中
read.c从buf[]中读取并输出、打印
下面为read.c文件
#include<stdio.h>#include<stdlib.h>#include<sys/types.h>#include<sys/ipc.h>#include<sys/shm.h>#include<errno.h>#include<unistd.h>#include<signal.h>//包含的头文件#include<string.h>//利用共享内存进行进程间的通信typedef struct{ pid_t pid; char buf[100];//定义的结构体}shm_t;void fun(int signum){ //空操作}int main(int argc, const char *argv[]){ key_t key; int shmid; pid_t pid; shm_t *p;//recive the signal from the write //then turn to pause() signal(SIGUSR1,fun);//init the shmid memory key = ftok(".",'a');//key_t ftok(const char *pathname, int proj_id) define the key value if((shmid = shmget(key,100,0664 | IPC_CREAT | IPC_EXCL)) == -1){ if(errno == EEXIST){ shmid = shmget(key,100,0664); //int shmget(key_t key, size_t size, int shmflg); //shmflag follows IPC_CREAT IPC_EXCL p = (shm_t *)shmat(shmid,NULL,0);//有必要进行强转,在我运行时不强转会出问题 //the purpose of this step send //the pid of process pid = p->pid; p->pid = getpid();//get the current pidnum kill(pid,SIGUSR1);//send signal to write } else{ perror("shget"); exit(1); } } //当shimd不存在,首次执行时 else{ p = (shm_t *)shmat(shmid,NULL,0); p->pid = getpid(); pause();//wait for signal from write pid = p->pid;//change the struct's pid number } while(1){ //程序阻塞在这里等待write发送信号 pause(); //when enter the quit,will quit from the progress if(strncmp(p->buf,"quit",4) == 0){ break;//breakout the while(1) } printf("buf is %s",p->buf); //send signal the write kill(pid,SIGUSR1); }//destroy the shmid shmdt(p); shmctl(shmid,IPC_RMID,NULL); return 0;}
接下来是write.c文件,用于向共享内存进行写内容
**#include<>;;;has the same headfiles as read.c has**int main(int argc, const char *argv[]){ key_t key; shm_t *p; int shmid; pid_t pid; signal(SIGUSR1,fun);//same function as read do//init the key = ftok(".",'a'); if((shmid = shmget(key,sizeof(shm_t),0664 | IPC_CREAT|IPC_EXCL)) == -1){ if(errno == EEXIST){ shmid = shmget(key,100,0664); p = (shm_t *)shmat(shmid,NULL,0); pid = p->pid; p->pid = getpid(); kill(pid,SIGUSR1); } else{ perror("shmget"); exit(1); } } else{ p = (shm_t *)shmat(shmid,NULL,0); p->pid = getpid(); pause(); pid = p->pid; } while(1){ //read the charaters from terminal fgets(p->buf,100,stdin); kill(pid,SIGUSR1);//send the signal to read if(strncmp(p->buf,"quit",4) == 0) break;//breakout while() pause();//wait the signal from read } return 0;}
进程间通信的方式有很多,管道:有名管道、无名管道 ,信号,共享内存,消息队列,信号灯等,还有套接字,会在网络编程里出现比较多。
0 0
- 进程&进程间通信
- 信号通信(进程间通信)
- 进程间通信-管道通信
- 进程间通信--管道通信
- [进程通信] 进程间通信 之 管道
- 进程与进程间通信
- 【IO进程】进程间通信
- 进程间通信
- 进程间通信方式
- 进程间通信IPC
- 进程间通信
- 操作系统:进程间通信
- 进程间的通信
- 进程间通信
- 进程间通信IPC
- 进程间通信
- 进程间通信总结
- 进程间管道通信
- 黑马程序员--c语言关键字,标示符,注释,数据,常量,变量介绍
- LintCode-最大子数组差
- Java Socket应用(二)——java中InetAddress的应用
- JQ高级事件之事件委托
- windows下创建软链
- 进程间通信
- [SQL SERVER] 如何linked server連接Oracle (Oracle Provider for OLE DB)
- JavaSE笔记之<继承深入理解>
- 关于网络性能调优
- mac linux上安装python,opencv等的记录
- 调试LATTICE 的SGMII的调试。
- iOS 周刊 第一期
- Maximal Square算法详解
- js 变量 赋予默认值