父代码
:
1 #include <stdio.h>
2 #include <sys/types.h>
3 #include <sys/ipc.h>
4 #include <sys/msg.h>
5 #include <string.h>
6 struct msgbuf
7 {
8 long mtype;
9 char mtext[128];
0 };
1 int main(int argc, const char *argv[])
2 {
3
4 key_t key=ftok("/home/ubuntu/",1);
5 if(key<0)
6 {
7 printf("失败\n");
8 return -1;
9 }
0 int msqid=msgget(key,IPC_CREAT|0777);
1 if(msqid<0)
2 {
3 printf("失败\n");
4 return -1;
5 }
6 struct msgbuf uu;
7 while(1)
8 {
9 //1类型用来发送
0 printf("请输入你的数据类型(本机发送类型为1)>>");
1 scanf("%ld",&uu.mtype);
2 if(strcmp(uu.mtext,"quit")==0)
3 break;
4 printf("请输入你要发送的数据>>");
5 scanf("%s",uu.mtext);
6 msgsnd( msqid, &uu,sizeof(uu.mtext), 0);
7 //2类型用来读取
8 msgrcv( msqid,&uu,sizeof(uu.mtext), 2,0);
9 if(strcmp(uu.mtext,"quit")==0)
0 {
1 printf("退出\n");
2 break;
3 }
4 printf("子说:%s\n",uu.mtext);
5 }
6 msgctl(msqid, IPC_RMID,NULL);
7 return 0;
8 }
子代码
1 #include <stdio.h>
2 #include <sys/types.h>
3 #include <sys/ipc.h>
4 #include <sys/msg.h>
5 #include <string.h>
6 #include <sys/types.h>
7 #include <sys/ipc.h>
8 #include <sys/msg.h>
9
10 struct msgbuf
11 {
12 long mtype;
13 char mtext[128];
14 };
15 int main(int argc, const char *argv[])
16 {
17
18 key_t key=ftok("/home/ubuntu/",1);
19 if(key<0)
20 {
21 printf("失败\n");
22 return -1;
23 }
24 int msqid=msgget(key,IPC_CREAT|0777);
25 if(msqid<0)
26 {
27 printf("失败\n");
28 return -1;
29 }
30 struct msgbuf uu;
31 while(1)
32 {
33 //1类型用来读取
34 msgrcv(msqid,&uu,sizeof(uu.mtext), 1,0);
35 if(strcmp(uu.mtext,"quit")==0)
36 {
37 printf("退出\n");
38 break;
39 }
40 printf("父说:%s\n",uu.mtext);
41
42 //2类型用来发送
43 printf("请输入你的数据类型(本机发送数据类型为2)>>");
44 scanf("%ld",&uu.mtype);
45 if(strcmp(uu.mtext,"quit")==0)
46 break;
47 printf("请输入你要发送的数据>>");
48 scanf("%s",uu.mtext);
49 msgsnd( msqid, &uu,sizeof(uu.mtext), 0);
50 }
51 msgctl(msqid, IPC_RMID,NULL);
52
53
54 return 0;
55 }
~
~
~
运行效果:
有名管道
1 #include <stdio.h>
2 #include <sys/types.h>
3 #include <sys/stat.h>
4 #include <sys/stat.h>
5 #include <fcntl.h>
6 #include <unistd.h>
7 #include <error.h>
8 #include <errno.h>
9 #include <string.h>
10 int main(int argc, const char *argv[])
11 {
12 if(mkfifo("./fifo",0777)<0)
13 {
14 if(errno != 17)
15 {
16 perror("44");
17 return -1;
18 }
19 }
20 if(mkfifo("./fifo1",0777)<0)
21 {
22 if(errno != 17)
23 {
24 perror("44");
25 return -1;
26 }
27 }
28
29 int fp=open("./fifo", O_RDONLY);
30 int fp1=open("./fifo1", O_WRONLY);
31 if(fp<0||fp1<0)
32 {
33 printf("失败2\n");
34 return -1;
35
36 }
37 ssize_t res;
38 while(1)
39 {
40 //老弟收
41 char temp[128];
42 res=read(fp,temp,sizeof(temp));
43 if(res==0)
44 {
45 printf("老大走了,你也别等了.\n");
46 break;
47 }
48 if(strcmp(temp,"quit")==0)
49 {
50 break;
51 }
52 printf("老大说:%s\n",temp);
53 //老弟发
54 char tem[128];
55 bzero(tem,sizeof(tem));
56 fgets(tem,sizeof(tem),stdin);
57 tem[strlen(tem)-1]='\0';
58 write(fp1,tem,sizeof(tem));
59 if(strcmp(tem,"quit")==0)
60 {
61 break;
62 }
63 }
64 close(fp);
65 close(fp1);
66
67 return 0;
68 }
~
~
子
1 #include <stdio.h>
2 #include <sys/types.h>
3 #include <sys/stat.h>
4 #include <sys/stat.h>
5 #include <fcntl.h>
6 #include <unistd.h>
7 #include <error.h>
8 #include <errno.h>
9 #include <string.h>
10
11 int main(int argc, const char *argv[])
12 {
13 if(mkfifo("./fifo",0777)<0)
14 {
15 if(errno != 17)
16 {
17 perror("44");
18 return -1;
19 }
20 }
21 if(mkfifo("./fifo1",0777)<0)
22 {
23 if(errno != 17)
24 {
25 perror("44");
26 return -1;
27 }
28 }
29
30 int fp=open("./fifo", O_WRONLY);
31 int fp1=open("./fifo1", O_RDONLY);
32 if(fp<0)
33 {
34 printf("失败2\n");
35 return -1;
36
37 }
38 ssize_t res;
39 while(1)
40 {
41 //老大发
42 char temp[128];
43 bzero(temp,sizeof(temp));
44 fgets(temp,sizeof(temp),stdin);
45 temp[strlen(temp)-1]='\0';
46 write(fp,temp,sizeof(temp));
47 if(strcmp(temp,"quit")==0)
48 {
49 break;
50 }
51 //老大收
52 char tem[128];
53 res=read(fp1,tem,sizeof(tem));
54 if(strcmp(tem,"quit")==0)
55 {
56 break;
57 }
58
59 if(res==0)
60 {
61 printf("老弟走了,你也别等了.\n");
62 break;
63 }
64
65 printf("老弟说:%s\n",tem);
66 }
67 close(fp);
68 close(fp1);
69 return 0;
70 }
~
~
~
~
~
运行效果