8.5/用有名管道做两程序的随时通信(进程)及消息队列的随时通信(线程),共享内存的存储和读取

有名管道A进程

 1 #include <stdio.h>
 2 #include <sys/types.h>
 3 #include <sys/stat.h>
 4 #include <errno.h>
 5 #include <fcntl.h>
 6 #include <string.h>
 7 #include <unistd.h>
 8 #include <stdlib.h>
 9 #include <sys/wait.h>
10 #include <signal.h>
11 int main(int argc, const char *argv[])
12 {
13     if(mkfifo("./fifo",0664)<0)
14     {
15         if(errno!=17)
16         {
17             perror("mkfifo");
18             return -1;
19         }
20     }
21     if(mkfifo("./fifo2",0664)<0)
22     {
23         if(errno!=17)
24         {
25             perror("mkfifo");
26             return -1;
27         }
28     }
29 
30     ssize_t res=0;
31     ssize_t bes=0;
32     char str[20]="";
33     char buf[20]="";
34     pid_t pid=fork();
35     if(pid > 0)
36     {
37         int fd=open("./fifo",O_RDONLY);
38         if(fd<0)
39         {
40             perror("open");
41             return -1;
42         }
43         printf("open success\n");
44         while(1)
45         {
46             bzero(buf,sizeof(buf));
47             bes=read(fd,buf,sizeof(buf));            
48             if(bes<0)
49             {
50                 perror("read");
51                 return -1;
52             }
53             else if(bes==0)                                         
54             {
55                 printf("B进程退出\n");
56                 break;
 57             }
 58             if(strcasecmp(buf,"quit")==0)
 59             {
 60                 int tokill= kill(pid,9);
 61                 if(kill<0)
 62                 {
 63                     perror("kill");
 64                     printf("杀掉子进程失败\n");
 65                 }
 66                 printf("收到quit,并通知子进程退出成功\n");
 67                 close(fd);
 68                 break;
 69             }
 70             printf("A进程接收%ld数据:%s\n",bes,buf);
 71         }
 72     }
 73     else if(pid == 0)
 74     {
 75 
 76         int fp=open("./fifo2",O_WRONLY);
 77         if(fp<0)
 78         {
 79             perror("open");
 80             return -1;
 81         }
 82         printf("open success\n");
 83 
 84         while(1)
 85         {
 86 
 87             //  printf("A进程写入:");
 88             bzero(str,sizeof(str));
 89             fgets(str,sizeof(str),stdin);
 90             str[strlen(str)-1]='\0';
 91             res=write(fp,str,sizeof(str));
 92             if(res<0)
 93             {
 94                 perror("write");
 95                 return -1;
 96             }
 97             if(strcasecmp(str,"quit")==0)
 98             {
 99                 close(fp);
100                 break;
101             }
102             //      printf("A进程写入%ld数据\n",res);
103         }
104     }
105     return 0;
106 }
~                                                                       
~                                                                       

 B进程代码

     1	#include <stdio.h>
     2	#include <sys/types.h>
     3	#include <sys/stat.h>
     4	#include <errno.h>
     5	#include <fcntl.h>
     6	#include <string.h>
     7	#include <unistd.h>
     8	#include <signal.h>
     9	int main(int argc, const char *argv[])
    10	{
    11	
    12		if(mkfifo("./fifo",0664)<0)
    13		{
    14			if(errno!=17)
    15			{
    16				perror("mkfifo");
    17				return -1;
    18			}
    19		}
    20		if(mkfifo("./fifo2",0664)<0)
    21		{
    22			if(errno!=17)
    23			{
    24				perror("mkfifo");
    25				return -1;
    26			}
    27		}
    28	
    29		ssize_t res=0;
    30		ssize_t bes=0;
    31		char str[20]="";
    32		char buf[20]="";
    33		pid_t pid=fork();
    34		if(pid==0)
    35		{
    36			int fp=open("./fifo",O_WRONLY);
    37			if(fp<0)
    38			{
    39				perror("open");
    40				return -1;
    41			}
    42			printf("open success\n");
    43			while(1)
    44			{
    45				//	printf("B进程写入:");
    46				bzero(buf,sizeof(buf));
    47				fgets(buf,sizeof(buf),stdin);
    48				buf[strlen(buf)-1]='\0';
    49				bes=write(fp,buf,sizeof(buf));
    50				if(bes<0)
    51				{
    52					perror("write");
    53					return -1;
    54				}
    55				if(strcasecmp(buf,"quit")==0)
    56				{
    57					close(fp);
    58					break;
    59				}
    60				//	printf("B进程写入%ld数据\n",bes);
    61			}
    62		}
    63		else if(pid>0)
    64		{
    65			int fd=open("./fifo2",O_RDONLY);
    66			if(fd<0)
    67			{
    68				perror("open");
    69				return -1;
    70			}
    71			printf("open success\n");
    72	
    73			while(1)
    74			{
    75				res=read(fd,str,sizeof(str));
    76				if(res<0)
    77				{
    78					perror("read");
    79					return -1;
    80				}
    81				else if(res==0)
    82				{
    83					printf("A进程退出\n");
    84					break;
    85				}
    86				if(strcasecmp(str,"quit")==0)
    87				{
    88					int tokill = kill(pid,9);
    89					if(tokill<0)
    90					{
    91						perror("kill");
    92						printf("通知子进程退出失败\n");
    93					}
    94					printf("收到quit,并通知子进程退出成功\n");
    95					close(fd);
    96					break;
    97				}
    98	
    99				printf("B进程读取%ld数据:%s\n",res,str);
   100			}
   101		}
   102	
   103		return 0;
   104	}

消息队列的通信

A程序代码

     1	#include <stdio.h>
     2	#include <sys/types.h>
     3	#include <sys/ipc.h>
     4	#include <sys/msg.h>
     5	#include <stdlib.h>
     6	#include <string.h>
     7	#include <pthread.h>
     8	
     9	typedef struct
    10	{
    11		long mtype;
    12		char mtext[128];
    13	}msgbuf;
    14	
    15	
    16	void* torcv(void* arg)
    17	{
    18		int queid=*(int*)arg;
    19		ssize_t res = 0;
    20		msgbuf rcv;
    21	
    22		while(1)
    23		{
    24			 //阻塞方式读取,IPC_NOWAIT是非阻塞
    25			res = msgrcv(queid,&rcv,sizeof(rcv.mtext),2,0);
    26			if(res<0)
    27			{
    28				perror("msgrcv");
    29				return NULL;
    30			}
    31			if(strcmp(rcv.mtext,"quit")==0)
    32			{
    33				printf("收到quit\n");
    34				exit(0);
    35			}
    36	
    37			printf("收到%ld个数据,%s\n",res,rcv.mtext);
    38		}
    39		pthread_exit(NULL);
    40	}
    41	
    42	void* tosnd(void* arg)
    43	{
    44		int queid=*(int*)arg;
    45		msgbuf snd;
    46		snd.mtype=1;
    47		while(1)
    48		{
    49			//  printf("输入消息包的类型>>>>");
    50			//  scanf(" %ld",&snd.mtype);//消息类型
    51			//  getchar();
    52			//printf("输入数据(退出请输入quit)>>>>");
    53			fgets(snd.mtext,sizeof(snd.mtext),stdin);
    54			snd.mtext[strlen(snd.mtext)-1] = 0;
    55			//阻塞方式发送,消息队列满,阻塞
    56			if(msgsnd(queid,&snd,sizeof(snd.mtext),0)<0)
    57			{
    58				perror("msgsnd");
    59				return NULL;
    60			}
    61			//printf("消息包发送成功\n");
    62			if(strcasecmp(snd.mtext,"quit")==0)
    63				exit(0);
    64		}
    65		pthread_exit(NULL);
    66	}
    67	
    68	int main(int argc, const char *argv[])
    69	{
    70		key_t key=ftok("./",2);
    71		if(key<0)
    72		{
    73			perror("ftok");
    74			return -1;
    75		}
    76	
    77		int queid=msgget(key,IPC_CREAT|0777);
    78		if(queid<0)
    79		{
    80			perror("msgget");
    81			return -1;
    82		}
    83		printf("msgget success\n");
    84	
    85	
    86		 pthread_t tid1,tid2;
    87		 if(pthread_create(&tid1,NULL,torcv,(void*)&queid)!=0)
    88		 {
    89			 perror("pthread_create");
    90			 return -1;
    91		 }
    92		 if(pthread_create(&tid2,NULL,tosnd,(void*)&queid)!=0)
    93		 {
    94			 perror("pthread_create");
    95			 return -1;
    96		 }
    97		 pthread_join(tid1,NULL);
    98		 pthread_join(tid2,NULL);
    99	
   100	    
   101		system("ipcs -q");
   102	
   103		return 0;
   104	}

 B代码(除了改一下消息包类型什么都没变)

     1	#include <stdio.h>
     2	#include <sys/types.h>
     3	#include <sys/ipc.h>
     4	#include <sys/msg.h>
     5	#include <stdlib.h>
     6	#include <string.h>
     7	#include <pthread.h>
     8	
     9	typedef struct
    10	{
    11		long mtype;
    12		char mtext[128];
    13	}msgbuf;
    14	
    15	
    16	void* torcv(void* arg)
    17	{
    18		int queid=*(int*)arg;
    19		ssize_t res = 0;
    20		msgbuf rcv;
    21	
    22		while(1)
    23		{
    24			 //阻塞方式读取,IPC_NOWAIT是非阻塞
    25			res = msgrcv(queid,&rcv,sizeof(rcv.mtext),1,0);
    26			if(res<0)
    27			{
    28				perror("msgrcv");
    29				return NULL;
    30			}
    31			if(strcmp(rcv.mtext,"quit")==0)
    32			{
    33				printf("收到quit\n");
    34				exit(0);
    35			}
    36	
    37			printf("收到%ld个数据,%s\n",res,rcv.mtext);
    38		}
    39		pthread_exit(NULL);
    40	}
    41	
    42	void* tosnd(void* arg)
    43	{
    44		int queid=*(int*)arg;
    45		msgbuf snd;
    46		snd.mtype=2;
    47		while(1)
    48		{
    49			//  printf("输入消息包的类型>>>>");
    50			//  scanf(" %ld",&snd.mtype);//消息类型
    51			//  getchar();
    52			//printf("输入数据(退出请输入quit)>>>>");
    53			fgets(snd.mtext,sizeof(snd.mtext),stdin);
    54			snd.mtext[strlen(snd.mtext)-1] = 0;
    55			//阻塞方式发送,消息队列满,阻塞
    56			if(msgsnd(queid,&snd,sizeof(snd.mtext),0)<0)
    57			{
    58				perror("msgsnd");
    59				return NULL;
    60			}
    61			//printf("消息包发送成功\n");
    62			if(strcasecmp(snd.mtext,"quit")==0)
    63				exit(0);
    64		}
    65		pthread_exit(NULL);
    66	}
    67	
    68	int main(int argc, const char *argv[])
    69	{
    70		key_t key=ftok("./",2);
    71		if(key<0)
    72		{
    73			perror("ftok");
    74			return -1;
    75		}
    76	
    77		int queid=msgget(key,IPC_CREAT|0777);
    78		if(queid<0)
    79		{
    80			perror("msgget");
    81			return -1;
    82		}
    83		printf("msgget success\n");
    84	
    85	
    86		 pthread_t tid1,tid2;
    87		 if(pthread_create(&tid1,NULL,torcv,(void*)&queid)!=0)
    88		 {
    89			 perror("pthread_create");
    90			 return -1;
    91		 }
    92		 if(pthread_create(&tid2,NULL,tosnd,(void*)&queid)!=0)
    93		 {
    94			 perror("pthread_create");
    95			 return -1;
    96		 }
    97		 pthread_join(tid1,NULL);
    98		 pthread_join(tid2,NULL);
    99	
   100	    
   101		system("ipcs -q");
   102	
   103		return 0;
   104	}

 共享内存的写入和读取(简单)

1. A进程写入一个整型,在该整型后,写入一个字符串
2. B进程将共享内存中的整型以及字符串读取出来;
 

A

     1	#include <stdio.h>
     2	#include <sys/types.h>
     3	#include <sys/ipc.h>
     4	#include <sys/shm.h>
     5	#include <string.h>
     6	
     7	int main(int argc, const char *argv[])
     8	{
     9		key_t key=ftok("./",1);
    10		if(ftok<0)
    11		{
    12			perror("ftok");
    13			return -1;
    14		}
    15	
    16		int shmid=shmget(key,128,IPC_CREAT|0664);
    17		if(shmid<0)
    18		{
    19			perror("shmget");
    20			return -1;
    21		}
    22		printf("创建共享内存成功\n");
    23	
    24		void* shmaddr=shmat(shmid,NULL,0);
    25		if((void*)shmaddr<0)
    26		{
    27			perror("shmaddr");
    28			return -1;
    29		}
    30	
    31	      int* pa=(int*)shmaddr;
    32		  *pa=4;
    33		  pa+=1;
    34		  char* parr=(char*)pa;
    35		  strcpy(parr,"hallo world");
    36	
    37		
    38		return 0;
    39	}

B

     1	#include <stdio.h>
     2	#include <sys/types.h>
     3	#include <sys/ipc.h>
     4	#include <sys/shm.h>
     5	#include <string.h>
     6	
     7	int main(int argc, const char *argv[])
     8	{
     9		key_t key=ftok("./",1);
    10		if(ftok<0)
    11		{
    12			perror("ftok");
    13			return -1;
    14		}
    15	
    16		int shmid=shmget(key,128,IPC_CREAT|0664);
    17		if(shmid<0)
    18		{
    19			perror("shmget");
    20			return -1;
    21		}
    22		printf("创建共享内存成功\n");
    23	
    24		void* shmaddr=shmat(shmid,NULL,0);
    25		if((void*)shmaddr<0)
    26		{
    27			perror("shmaddr");
    28			return -1;
    29		}
    30	
    31	      int* pa=(int*)shmaddr;
    32		  printf("%d\n",*pa);
    33		 // int tmp=*pa;
    34		 // printf("%d\n",tmp);
    35		  pa+=1;
    36		  char* parr=(char*)pa;
    37		  printf("%s\n",parr);
    38	
    39		
    40		return 0;
    41	}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值