用共享内存实现倒置和打印/利用消息队列实现两个进程间通信

1、用共享内存实现倒置和打印

程序代码

程序1:

  1 #include<stdio.h>
  2 #include<sys/types.h>
  3 #include<sys/ipc.h>
  4 #include<sys/shm.h>
  5 #include<stdlib.h>
  6 #include<string.h>
  7 
  8 
  9 
 10 int main(int argc, const char *argv[])
 11 {
 12     key_t key = ftok("./",'a');
 13     if(key < 0)
 14     {
 15         perror("ftok");
 16         return -1;
 17     }
 18     printf("key = %#x\n",key);
 19 
 20     //创建获取共享内存的shmid
 21     int shmid = shmget(key,32,IPC_CREAT|0664);
 22     if(shmid < 0)
 23     {
 24         perror("shmget");
 25         return -1;                                                                                                
 26     }
 27 
 28     printf("shmid = %d\n",shmid);
 29 
 30     //将共享内存映射到用户空间中
 31     void *addr = shmat(shmid,NULL,0);
 32     if((void*)-1 == addr)
 33     {
 34         perror("shmat");
 35         return -1;
 36     }
 37 
 38     char buf[]="1234567";
 39 
 40     *(char *)addr=1;
 41     strcpy((char *)addr+1,buf);
 42     while(1)
 43     {
 44         if(1 == *(char *)addr)
 45         {
 46             int i=1,j=strlen((char *)addr+1);
 47             char count=1;
 48             while(i<j)
 49             {
 50                 count = *((char *)addr +i);
 51                 *((char *)addr+i) = *((char *)addr+j);
 52                 *((char *)addr+j) = count;
 53                 i++;
 54                 j--;
 55             }
 56             *(char *)addr = 2;
 57         }
 58     }
 59 
 60     return 0;
 61 }

程序2

  1 #include<stdio.h>
  2 #include<sys/types.h>
  3 #include<sys/ipc.h>
  4 #include<sys/shm.h>
  5 #include<stdlib.h>
  6 #include<string.h>
  7 
  8 
  9 
 10 int main(int argc, const char *argv[])
 11 {
 12     key_t key = ftok("./",'a');
 13     if(key < 0)
 14     {
 15         perror("ftok");
 16         return -1;
 17     }
 18     printf("key = %#x\n",key);
 19 
 20     //创建获取共享内存的shmid
 21     int shmid = shmget(key,32,IPC_CREAT|0664);
 22     if(shmid < 0)
 23     {
 24         perror("shmget");
 25         return -1;
 26     }
 27 
 28     printf("shmid = %d\n",shmid);
 29 
 30     //将共享内存映射到用户空间中
 31     void *addr = shmat(shmid,NULL,0);
 32     if((void*)-1 == addr)
 33     {
 34         perror("shmat");
 35     }
 36     printf("addr=%p\n",addr);
 37 
 38     while(1)
 39     {
 40         if(2 == *(char *)addr)
 41         {
 42             printf("%s\n",(char *)addr+1);
 43             *((char *)addr) = 1;
 44         }                                                                                                                                                                                                                                            
 45     }
 46 
 47     return 0;
 48 }
~                                      

运行结果:

ubuntu@ubuntu:作业$ gcc 01_in_shm.c -o w
ubuntu@ubuntu:作业$ ./w
key = 0x610128e4
shmid = 52
^C
ubuntu@ubuntu:作业$ 
 

 1234567
7654321
1234567
7654321
1234567
7654321
1234567
^C

 2、利用消息队列实现两个进程间通信

(应该创建进程来进行通信的,这样的话容易实现一个终端输入一个quit,四个进程全部退出)

  1 #include<stdio.h>
  2 #include<sys/types.h>
  3 #include<unistd.h>
  4 #include<pthread.h>
  5 #include<sys/ipc.h>
  6 #include<sys/msg.h>
  7 #include<string.h>
  8 struct msgbuf
  9 {
 10     long mtype;
 11     char mtext[128];
 12 };
 13 
 14 
 15 void *callBack(void *arv)
 16 {
 17     key_t key;
 18     key= ftok("../",1);
 19     if(key == -1)
 20     {
 21         perror("ftok");
 22         return NULL ;
 23     }
 24 
 25     int msgid = msgget(key,IPC_CREAT|0664);
 26     if(msgid == -1)                                                                                                                                                                                                                                                                                                                                                                                                                                               
 27     {
 28         perror("msgget");
 29         return NULL;
 30     }
 31 
 32     struct msgbuf rcv;
 33     while(1)
 34     {
 35         if(msgrcv(msgid,&rcv,sizeof(rcv.mtext),0,0) < 0)
 36         {
 37             perror("msgrcv");
 38             break;
 39         }
 40 
 41         if(strcasecmp(rcv.mtext,"quit") == 0)
 42         {
 43             printf("对端进程退出\n");
 44             break;
 45         }
 46         printf("%ld : %s\n",rcv.mtype,rcv.mtext);
 47 
 48     }
 49 }
 50 
 51 int main(int argc, const char *argv[])
 52 {
 53     pthread_t cpid;
 54     pthread_create(&cpid,NULL,callBack,NULL);
 55 
 56     key_t key;
 57     key= ftok("./",1);
 58     if(key == -1)
 59     {
 60         perror("ftok");
 61         return -1;
 62     }
 63 
 64     int msgid = msgget(key,IPC_CREAT|0664);
 65     if(msgid == -1)
 66     {
 67         perror("msgget");
 68         return -1;
 69     }
 70 
 71     struct msgbuf snd;
 72     while(1)
 73     {
 74         //打包
 75         printf("请输入消息类型>>>");
 76         scanf("%ld",&snd.mtype);
 77         getchar();
 78 
 79         printf("请输入消息内容>>>");
 80         scanf("%s",snd.mtext);
 81         getchar();
 82 
 83 
 84         //发送数据包到消息队列中
 85         if(msgsnd(msgid,&snd,sizeof(snd.mtext),0) < 0)
 86         {
 87             perror("msgsnd");
 88             return -1;
 89         }
 90 
 91         if(strcasecmp(snd.mtext,"quit") == 0)
 92         {
 93             printf("程序准备退出\n");
 94 
 95             break;
 96         }
 97 
 98 
 99         printf("发送成功\n");
100 
101     }
102 
103 
104 
105     return 0;
106 }
  1 #include<stdio.h>
  2 #include<sys/types.h>
  3 #include<unistd.h>
  4 #include<pthread.h>
  5 #include<sys/ipc.h>
  6 #include<sys/msg.h>
  7 #include<string.h>
  8 struct msgbuf
  9 {
 10     long mtype;
 11     char mtext[128];
 12 };
 13 
 14 
 15 void *callBack(void *arv)
 16 {
 17     key_t key;
 18     key= ftok("./",1);
 19     if(key == -1)
 20     {
 21         perror("ftok");
 22         return NULL;
 23     }
 24 
 25     int msgid = msgget(key,IPC_CREAT|0664);
 26     if(msgid == -1)
 27     {
 28         perror("msgget");
 29         return NULL ;
 30     }
 31 
 32     struct msgbuf rcv;
 33     while(1)
 34     {
 35         if(msgrcv(msgid,&rcv,sizeof(rcv.mtext),0,0) < 0)
 36         {
 37             perror("msgrcv");
 38             break;
 39         }
 40         printf("%ld : %s\n",rcv.mtype,rcv.mtext);
 41 
 42         if(strcasecmp(rcv.mtext,"quit") == 0)
 43         {
 44             printf("对端进程退出\n");
 45             break;
 46         }
 47                                                                                                                                                                                                                                                                                                                   
 48     }
 49 }
 50 
 51 int main(int argc, const char *argv[])
 52 {
 53     pthread_t cpid;
 54     pthread_create(&cpid,NULL,callBack,NULL);
 55 
 56     key_t key;
 57     key= ftok("../",1);
 58     if(key == -1)
 59     {
 60         perror("ftok");
 61         return -1;
 62     }
 63 
 64     int msgid = msgget(key,IPC_CREAT|0664);
 65     if(msgid == -1)
 66     {
 67         perror("msgget");
 68         return -1;
 69     }
 70 
 71     struct msgbuf snd;
 72     while(1)
 73     {
 74         //打包
 75         printf("请输入消息类型>>>");
 76         scanf("%ld",&snd.mtype);
 77         getchar();
 78 
 79         printf("请输入消息内容>>>");
 80         scanf("%s",snd.mtext);
 81         getchar();
 82 
 83 
 84         //发送数据包到消息队列中
 85         if(msgsnd(msgid,&snd,sizeof(snd.mtext),0) < 0)
 86         {
 87             perror("msgsnd");
 88             return -1;
 89         }
 90 
 91         if(strcasecmp(snd.mtext,"quit") == 0)
 92         {
 93             printf("程序准备退出\n");
 94 
 95             break;
 96         }
 97 
 98 
 99         printf("发送成功\n");
100 
101     }
102 
103 
104 
105     return 0;
106 }
~                             

请输入消息内容>>>qwdasd
发送成功
请输入消息类型>>>quit
请输入消息内容>>>发送成功
请输入消息类型>>>quit
请输入消息内容>>>发送成功
请输入消息类型>>>2 : uit
对端进程退出
2
请输入消息内容>>>quit
程序准备退出
 

 ntu@ubuntu:作业$ ./b
请输入消息类型>>>2  
请输入消息内容>>>asd
发送成功
请输入消息类型>>>3 : qwdasd
3 : uit
3 : uit
quit
请输入消息内容>>>发送成功
请输入消息类型>>>2
请输入消息内容>>>quit
程序准备退出

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

malingshu404

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值