进程间通信具体实现代码(两个程序间互发信息)
2017-12-10 19:19
232 查看
无名管道
有名管道fiffowrite
fiforead:
消息队列:
共享内存:
共享内存——结构体:
两个程序间互发信息:
#include <stdio.h> #include <stdlib.h> #include <sys/wait.h> #include <sys/types.h> int main() { int ret; pid_t pid; int count = 0; int fd[2] = {0}; ret = pipe(fd); if(-1 == ret) { perror("pipe"); exit(1); } pid = fork(); if(-1 == pid) { perror("fork"); exit(1); } else if (0 == pid) { sleep(1); count ++; printf("Child Process : count = %d\n",count); ret = write(fd[1],&count,sizeof(count)); if(-1 == ret) { perror("write"); exit(1); } } else { ret = read(fd[0],&count,sizeof(count)); if(-1 == ret) { perror("read"); exit(1); } count++; printf("Parent Process : count = %d\n",count); waitpid(pid,NULL,0); } }
有名管道fiffowrite
#include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <string.h> int main() { int fd,ret; char buf[100] = {0}; fd = open("fifo.tmp",O_WRONLY); if(-1 == fd) { perror("open"); exit(1); } while(1) { scanf("%s",buf); ret = write(fd,buf,strlen(buf)); if(-1 == ret) { perror("write"); exit(1); } if(!strncmp(buf,"bye",3)) { break; } } return 0; }
fiforead:
#include <stdio.h> #include <stdlib.h> #include <sys/stat.h> #include <sys/types.h> #include <fcntl.h> #include <unistd.h> #include <string.h> int main() { int ret,fd; char buf[100] = {0}; ret = mkfifo("fifo.tmp",O_CREAT|O_EXCL); if(-1 == ret) { perror("mkfifo"); exit(1); } fd = open("fifo.tmp",O_RDONLY); if(-1 == fd) { perror("open"); exit(1); } while(1) { ret = read(fd,buf,sizeof(buf)); if(-1 == ret) { perror("read"); exit(1); } if(!strncmp(buf,"bye",3)) { break; } printf("Read From Fifo : %s\n",buf); memset(buf,0,sizeof(buf)); unlink("fifo.tmp"); } return 0; }
消息队列:
#include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <sys/msg.h> #include <sys/ipc.h> #include <string.h> #include <stdlib.h> #define MSGKEY 1234 struct msgbuf { long mtype; char mtext[100]; }; int main() { int msgid,ret; struct msgbuf buf; msgid = msgget(MSGKEY,IPC_CREAT|IPC_EXCL); if(-1 == msgid) { perror("msgget"); exit(1); } while(1) { memset(&buf,0,sizeof(buf)); scanf("%s",buf.mtext); buf.mtype = 1; ret = msgsnd(msgid,&buf,sizeof(buf) - sizeof(long),0); if(-1 == ret) { perror("msgsnd"); exit(1); } if(!strncmp("bye",buf.mtext,3)) { break; } } msgctl(msgid,IPC_RMID,NULL); return 0; }
#include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <sys/msg.h> #include <sys/ipc.h> #include <string.h> #include <stdlib.h> #define MSGKEY 1234 struct msgbuf { long mtype; char mtext[100]; }; int main() { int msgid,ret; struct msgbuf buf; msgid = msgget(MSGKEY,0); if(-1 == msgid) { perror("msgget"); exit(1); } while(1) { memset(&buf,0,sizeof(buf)); buf.mtype = 1; ret = msgrcv(msgid,&buf,sizeof(buf.mtext),1,0); if(-1 == ret) { perror("msgsnd"); exit(1); } if(!strncmp("bye",buf.mtext,3)) { break; } printf("Receive From Message : %s\n",buf.mtext); } return 0; }
共享内存:
#include <stdio.h> #include <sys/ipc.h> #include <sys/shm.h> #include <sys/types.h> #include <string.h> #include <stdlib.h> #define SHMKEY 1234 #define SHMSIZE 4096 int main() { int shmid,ret; void *shmaddr; shmid = shmget(SHMKEY, SHMSIZE, IPC_CREAT | IPC_EXCL); if(-1 == shmid) { perror("shmget"); exit(1); } shmaddr = shmat(shmid,NULL,0); if((void *)-1 == shmaddr) { perror("shmat"); exit(1); } strcpy(shmaddr,"chenzheshizhu"); sleep(5); ret = shmdt(shmaddr); if(-1 == ret) { perror("shmdt"); exit(1); } shmctl(shmid, IPC_RMID, NULL); return 0; }
#include <stdio.h> #include <sys/ipc.h> #include <sys/shm.h> #include <sys/types.h> #include <string.h> #include <stdlib.h> #define SHMKEY 1234 #define SHMSIZE 4096 int main() { int shmid,ret; void *shmaddr; char buf[100] = {0}; shmid = shmget(SHMKEY, SHMSIZE,0); if(-1 == shmid) { perror("shmget"); exit(1); } shmaddr = shmat(shmid,NULL,0); if((void *)-1 == shmaddr) { perror("shmat"); exit(1); } strcpy(buf,shmaddr); printf("Read From sharememory : %s\n",buf); ret = shmdt(shmaddr); if(-1 == ret) { perror("shmdt"); exit(1); } return 0; }
共享内存——结构体:
#include <stdio.h> #include <sys/ipc.h> #include <sys/shm.h> #include <sys/types.h> #include <string.h> #include <stdlib.h> #include <sys/sem.h> #define SHMKEY 1234 #define SHMSIZE 4096 #define SEMKEY 1234 union semun { int val; struct semid_ds *buf; unsigned short *array; struct seminfo *__buf; }; int sem_p(int id) { struct sembuf buf; int ret; buf.sem_num = 0; buf.sem_op = -1; buf.sem_flg = SEM_UNDO; ret = semop(id,&buf,1); if(-1 == ret) { perror("semop"); return 1; } return 0; } int sem_v(int id) { struct sembuf buf; int ret; buf.sem_num = 0; buf.sem_op = 1; buf.sem_flg = SEM_UNDO; ret = semop(id,&buf,1); if(-1 == ret) { perror("semop"); return 1; } return 0; } int main() { int shmid,ret,semid; void *shmaddr; int count = 0; int tmp; union semun union_sem; shmid = shmget(SHMKEY, SHMSIZE, IPC_CREAT | IPC_EXCL); if(-1 == shmid) { perror("shmget"); exit(1); } shmaddr = shmat(shmid,NULL,0); if((void *)-1 == shmaddr) { perror("shmat"); exit(1); } semid = semget(SEMKEY,1,IPC_CREAT | IPC_EXCL); if(-1 == semid) { perror("semget"); exit(1); } union_sem.val = 1; ret = semctl(semid,0,SETVAL,union_sem); *(int *)shmaddr = count; while(1) { sem_p(semid); tmp = *(int *)shmaddr; if(tmp > 1000) { break; } printf("Process 1 : count = %d\n",tmp); usleep(10); tmp++; *(int *)shmaddr = tmp; sem_v(semid); } ret = shmdt(shmaddr); if(-1 == ret) { perror("shmdt"); exit(1); } sleep(1); shmctl(shmid, IPC_RMID, NULL); semctl(semid,0,IPC_RMID); return 0; }
#include <stdio.h> #include <sys/ipc.h> #include <sys/shm.h> #include <sys/types.h> #include <string.h> #include <stdlib.h> #include <sys/sem.h> #define SHMKEY 1234 #define SHMSIZE 4096 #define SEMKEY 1234 union semun { int val; struct semid_ds *buf; unsigned short *array; struct seminfo *__buf; }; int sem_p(int id) { struct sembuf buf; int ret; buf.sem_num = 0; buf.sem_op = -1; buf.sem_flg = SEM_UNDO; ret = semop(id,&buf,1); if(-1 == ret) { perror("semop"); return 1; } return 0; } int sem_v(int id) { struct sembuf buf; int ret; buf.sem_num = 0; buf.sem_op = 1; buf.sem_flg = SEM_UNDO; ret = semop(id,&buf,1); if(-1 == ret) { perror("semop"); return 1; } return 0; } int main() { int shmid,ret,semid; void *shmaddr; int count = 0; int tmp; shmid = shmget(SHMKEY, SHMSIZE, 0); if(-1 == shmid) { perror("shmget"); exit(1); } shmaddr = shmat(shmid,NULL,0); if((void *)-1 == shmaddr) { perror("shmat"); exit(1); } semid = semget(SEMKEY,1,0); if(-1 == semid) { perror("semget"); exit(1); } *(int *)shmaddr = count; while(1) { sem_p(semid); tmp = *(int *)shmaddr; if(tmp > 1000) { break; } printf("Process 2 : count = %d\n",tmp); usleep(10); tmp++; *(int *)shmaddr = tmp; sem_v(semid); } ret = shmdt(shmaddr); if(-1 == ret) { perror("shmdt"); exit(1); } return 0; }
两个程序间互发信息:
#include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <sys/msg.h> #include <sys/ipc.h> #include <string.h> #include <stdlib.h> #include <signal.h> #define MSGKEY 1234 struct msgbuf { long mtype; char mtext[100]; }; int main() { int msgid,ret; struct msgbuf buf; pid_t pid; msgid = msgget(MSGKEY,IPC_CREAT|IPC_EXCL); if(-1 == msgid) { perror("msgget"); exit(1); } pid = fork(); if(-1 == pid) { perror("fork"); exit(1); } else if(0 == pid) { while(1) { memset(&buf,0,sizeof(buf)); scanf("%s",buf.mtext); buf.mtype = 1; ret = msgsnd(msgid,&buf,sizeof(buf) - sizeof(long),0); if(-1 == ret) { perror("msgsnd"); exit(1); } if(!strncmp("bye",buf.mtext,3)) { buf.mtype = 2; ret = msgsnd(msgid,&buf,sizeof(buf) - sizeof(long),0); if(-1 == ret) { perror("msgsnd"); exit(1); } break; } } } else { while(1) { memset(&buf,0,sizeof(buf)); ret = msgrcv(msgid,&buf,sizeof(buf.mtext),2,0); if(-1 == ret) { perror("msgrcv"); exit(1); } if(!strncmp(buf.mtext,"bye",3)) { kill(pid,SIGKILL); break; } printf("\t\t\t%s\n",buf.mtext); } waitpid(pid,NULL,0); } msgctl(msgid, IPC_RMID, NULL); return 0; }
#include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <sys/msg.h> #include <sys/ipc.h> #include <string.h> #include <stdlib.h> #include <signal.h> #define MSGKEY 1234 struct msgbuf { long mtype; char mtext[100]; }; int main() { int msgid,ret; struct msgbuf buf; pid_t pid; msgid = msgget(MSGKEY, 0); if (-1 == msgid) { perror("msgget"); exit(1); } pid = fork(); if(-1 == pid) { perror("fork"); exit(1); } else if(0 == pid) { while(1) { memset(&buf,0,sizeof(buf)); scanf("%s",buf.mtext); buf.mtype = 2; ret = msgsnd(msgid,&buf,sizeof(buf.mtext),0); if(-1 == ret) { perror("msgsnd"); exit(1); } if(!strncmp("bye",buf.mtext,3)) { buf.mtype = 1; ret = msgsnd(msgid,&buf,sizeof(buf.mtext),0); if(-1 == ret) { perror("msgsnd"); exit(1); } break; } } } else { while(1) { memset(&buf,0,sizeof(buf)); ret = msgrcv(msgid,&buf,sizeof(buf.mtext),1,0); if(-1 == ret) { perror("msgrcv"); exit(1); } if(!strncmp(buf.mtext,"bye",3)) { kill(pid,SIGKILL); break; } printf("\t\t\t%s\n",buf.mtext); } waitpid(pid,NULL,0); } return 0; }
相关文章推荐
- php 批量替换程序的具体实现代码
- C程序实现在lcd 上全屏写 blue 色 及获取fb信息 ----》linux 应用层代码
- 自动添加VS 2008 代码文件版权信息 注释,用小程序实现更改
- 微信小程序Java登录流程(ssm实现具体功能和加解密隐私信息问题解决方案)
- 微信小程序Java登录流程(ssm实现具体功能和加解密隐私信息问题解决方案)
- Android获取手机配置信息具体实现代码
- linux c开发: 程序崩溃时保存堆栈信息并解析具体代码行
- php 批量替换程序的具体实现代码
- C#实现通过程序自动抓取远程Web网页信息的代码
- php 批量替换程序的具体实现代码
- 本文给出了一种方便实用的解决大文件的读取、存储等处理的方法,并结合相关程序代码对具体的实现过程进行了介绍
- 求二叉树中两个节点的最近公共节点的C程序实现代码
- android 分享功能,实现分享的程序与进行分享信息的代码
- C#实现通过程序自动抓取远程Web网页信息的代码
- linux c开发: 程序崩溃时保存堆栈信息并解析具体代码行
- 删除两个升序正数组中重复元素的C程序实现代码
- php 实现信息采集(网页内容抓取)程序代码
- 如何在C#中实现两个程序的通信?