当前位置:   article > 正文

Linux C/C++ 进程通信(管道、消息队列、共享存储)_linux c++ 共享内存通信

linux c++ 共享内存通信

Linux作业:有P1,P2,P3三个进程,P1和P2负责从键盘接收字符串,均发送给P3,P3接收到字符串,根据发送方分别显示”P3 received *** from P1(或P2)" ;分别用管道通信,消息队列和共享存储三种通信方式实现。

管道

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<unistd.h>
  4. #include<string.h>
  5. int main(void){
  6.     int fd1[2],fd2[2];
  7.     
  8.     if(pipe(fd1) == -1){
  9.         perror("pipe1");
  10.         exit(EXIT_FAILURE);
  11.     }
  12.     
  13.     if(pipe(fd2) == -1){
  14.         perror("pipe2");
  15.         exit(EXIT_FAILURE);
  16.     }
  17.     
  18.     if(fork() == 0){
  19.         close(fd1[0]);//关闭P1读端
  20.         close(fd2[0]);//关闭P2读端
  21.         close(fd2[1]);//关闭P2写端
  22.         
  23.         char buffer[100];
  24.         //读取输入的内容
  25.         printf("Input in P1:");
  26.         scanf("%s",buffer);
  27.         write(fd1[1],buffer,strlen(buffer)+1);    
  28.         exit(EXIT_SUCCESS);
  29.     }
  30.     wait(NULL);
  31.     if(fork() == 0){
  32.         close(fd1[0]);
  33.         close(fd1[1]);
  34.         close(fd2[0]);
  35.         
  36.         char buffer[100];
  37.         printf("Input in P2:");
  38.         scanf("%s",buffer);
  39.         write(fd2[1],buffer,strlen(buffer)+1);    
  40.         exit(EXIT_SUCCESS);
  41.     }
  42.     wait(NULL);
  43.     //关闭写通道
  44.     close(fd1[1]);
  45.     close(fd2[1]);
  46.    
  47.     char buffer[100];
  48.     int read_bytes;
  49.     while(1){
  50.         if((read_bytes = read(fd1[0],buffer,100))>0){
  51.             printf("P3 received %s from P1\n",buffer);
  52.         }
  53.         if((read_bytes = read(fd2[0],buffer,100))>0){
  54.             printf("P3 received %s from P2\n",buffer);
  55.         }
  56.     }
  57.         
  58.     return 0;    
  59. }

消息队列

 P1:

  1. #include <sys/types.h>
  2. #include <sys/ipc.h>
  3. #include <sys/msg.h>
  4. #include <stdlib.h>
  5. #include <stdio.h>
  6. #include <unistd.h>
  7. #include <signal.h>
  8. #include <string.h>
  9. #define MSGKEY 1307
  10. #define text_size 1024
  11. struct msgform {
  12.     long mtype;
  13.     char mtext[text_size];
  14. };
  15. int main(void) {
  16.     struct msgform msg;
  17.     int i,msgqid;
  18.     // 获取消息队列标识符
  19.     if ((msgqid = msgget(MSGKEY, 0777)) == -1) {
  20.         perror("msgget");
  21.         exit(EXIT_FAILURE);
  22.     }
  23.     while(1){
  24.         printf("Input in P1:");
  25.         scanf("%s",msg.mtext);
  26.         msg.mtype = 2;
  27.         msgsnd(msgqid, &msg, strlen(msg.mtext) + 10);
  28.     }
  29.     return 0;
  30. }

P2:

  1. #include <sys/types.h>
  2. #include <sys/ipc.h>
  3. #include <sys/msg.h>
  4. #include <stdlib.h>
  5. #include <stdio.h>
  6. #include <unistd.h>
  7. #include <signal.h>
  8. #include <string.h>
  9. #define MSGKEY 1307
  10. #define text_size 1024
  11. struct msgform {
  12.     long mtype;
  13.     char mtext[text_size];
  14. };
  15. int main(void) {
  16.     struct msgform msg;
  17.     int i,msgqid;
  18.     
  19.     // 获取消息队列标识符
  20.     if ((msgqid = msgget(MSGKEY, 0777)) == -1) {
  21.         perror("msgget");
  22.         exit(EXIT_FAILURE);
  23.     }
  24.     while(1){
  25.         printf("Input in P2:");
  26.         scanf("%s",msg.mtext);
  27.         msg.mtype = 2;
  28.         msgsnd(msgqid, &msg, strlen(msg.mtext) + 10);
  29.     }
  30.     
  31.     return 0;
  32. }

P3:

  1. #include <sys/types.h>
  2. #include <sys/ipc.h>
  3. #include <sys/msg.h>
  4. #include <stdlib.h>
  5. #include <stdio.h>
  6. #include <unistd.h>
  7. #include <signal.h>
  8. #include <string.h>
  9. #define MSGKEY 1307
  10. #define text_size 1024
  11. struct msgform {
  12.     long mtype;
  13.     char mtext[text_size];
  14. };
  15. int msgqid;
  16. void cleanup() {
  17.     msgctl(msgqid, IPC_RMID, 0);
  18.     exit(0);
  19. }
  20. int main(void) {
  21.     struct msgform msg;
  22.     int i;
  23.     pid_t pid3;
  24.     
  25.     for (i = 0; i < 23; i++) {
  26.         signal(i, cleanup);
  27.     }
  28.     // 创建消息队列
  29.     if ((msgqid = msgget(MSGKEY, 0777 | IPC_CREAT)) == -1) {
  30.         perror("msgget");
  31.         exit(EXIT_FAILURE);
  32.     }
  33.     if ((pid3 = fork()) == -1) {
  34.         perror("pid3");
  35.         exit(EXIT_FAILURE);
  36.     } else if (pid3 == 0) {
  37.         for (;;) {
  38.             msgrcv(msgqid, &msg, text_size, 10);
  39.             printf("P3 received %s from P1\n", msg.mtext);
  40.         }
  41.         exit(EXIT_SUCCESS);
  42.     } else {
  43.         if ((pid3 = fork()) == -1) {
  44.             perror("pid3");
  45.             exit(EXIT_FAILURE);
  46.         } else if (pid3 == 0) {
  47.             for (;;) {
  48.                 msgrcv(msgqid, &msg, text_size, 20);
  49.                 printf("P3 received %s from P2\n", msg.mtext);
  50.             }
  51.             exit(EXIT_SUCCESS);
  52.         }
  53.         // 父进程等待子进程执行完毕
  54.         wait(NULL);
  55.         return 0;
  56.     }
  57. }

共享存储:

  1. #include<unistd.h>
  2. #include<stdio.h>
  3. #include<stdlib.h>
  4. #include<sys/ipc.h>
  5. #include<sys/shm.h>
  6. #include<string.h>
  7. #define MAXSIZE 1024
  8. int main(void){
  9.     pid_t pid1,pid2,pid3;
  10.     int shmid_1,shmid_2;
  11.     char *shmptr_1,*shmptr_2;
  12.     char input[MAXSIZE];
  13.     
  14.     // 创建共享内存
  15.     if((shmid_1 = shmget(IPC_PRIVATE,MAXSIZE,IPC_CREAT|0666))==-1){
  16.         perror("shmget_1");
  17.         exit(EXIT_FAILURE);
  18.     }
  19.     if((shmid_2 = shmget(IPC_PRIVATE,MAXSIZE,IPC_CREAT|0666))==-1){
  20.         perror("shmget_2");
  21.         exit(EXIT_FAILURE);
  22.     }
  23.     
  24.     //    将共享内存段连接到当前进程的地址空间
  25.     if((shmptr_1= (char*)shmat(shmid_1,0,0)) == (char*)-1){
  26.         perror("shmat_1");
  27.         exit(EXIT_FAILURE);
  28.     }
  29.     if((shmptr_2 = (char*)shmat(shmid_2,0,0)) == (char*)-1){
  30.         perror("shmat_2");
  31.         exit(EXIT_FAILURE);
  32.     }
  33.     
  34.     //    P1
  35.     if((pid1 = fork()) ==-1 ){
  36.         perror("pid1");
  37.         exit(EXIT_FAILURE);
  38.     }
  39.     else if (pid1 == 0){
  40.         printf("Input in P1:");
  41.         scanf("%s",shmptr_1);
  42.         
  43.         exit(EXIT_SUCCESS);
  44.     }//P2
  45.     else{
  46.         wait(NULL);
  47.         if((pid2 = fork()) ==-1 ){
  48.             perror("pid2");
  49.             exit(EXIT_FAILURE);
  50.         }
  51.         else if (pid2 == 0){
  52.             printf("Input in P2:");
  53.             scanf("%s",shmptr_2);
  54.             
  55.             exit(EXIT_SUCCESS);
  56.         }
  57.     }
  58.     // 等待子进程结束
  59.     wait(NULL);    
  60.     wait(NULL);
  61.     
  62.     printf("P3 received %s from P1\n",shmptr_1);
  63.     printf("P3 received %s from P2\n",shmptr_2);
  64.     
  65.     // 分离共享内存
  66.     shmdt(shmptr_1);
  67.     shmdt(shmptr_2);
  68.     
  69.     // 删除共享内存段
  70.     shmctl(shmid_1, IPC_RMID, NULL);
  71.     shmctl(shmid_2, IPC_RMID, NULL);
  72.     
  73.     return 0;
  74. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/神奇cpp/article/detail/837647
推荐阅读
相关标签
  

闽ICP备14008679号