进程间通信
我们知道,在进程运行期间,每个进程都是相互独立的,而有的时候,我们需要将多个一个进程的数据传输给另外一个进程,或者实现多个进程共享同一份资源,又或者是一个进程发消息给另外一个进程,此时。就需要进行多个进程之间的通信,本文将在linux操作系统下简介进程间通信的基本概念以及方法。
*管道通信(分匿名管道和命名管道)
*System V IPC(主要有三种方式:消息队列,共享内存,信号量)
管道通信
#include
2 #include
3 #include
4 #include
5 int main()
6 {
7
8 int fd[2];
9 int len;
10 int ret=pipe(fd);
11 if(ret!=0)
12 {
13 perror("pipe");
14 exit(1);
15 }
16 pid_t pid=fork();
17 if(pid==-1)
18 {
19 printf("fork");
20 exit(1);
21 }
22 if(pid==0)
23 {//child
24 close(fd[0]);
25 write(fd[1],"hello",5);
26 close(fd[1]);
27 exit(1);
28 }
29 close(fd[1]);
30 char buf[10]={0};
31 read(fd[0],buf,10);
32 printf("buf is:%s",buf);
33 return 0;
34 }
#include
2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
8 int main()
9 {
10 int wfd=open("mypipe",O_WRONLY);
11 if(wfd<0)
12 {
13 perror("open");
14 exit(1);
15 }
16 char buf[1024];
17 while(1)
18 {
19 buf[0]=0;
20 printf("please Enter:");
21 fflush(stdout);
22 ssize_t s=read(0,buf,sizeof(buf)-1);
23 if(s>0)
24 {
25 buf[s]=0;
26 write(wfd,buf,strlen(buf));
27
28 }
29 else if(s==0)
30 exit(0);
31
32 }
33 close(wfd);
34 return 0;
35 }
1 #include
2 #include
3 #include
4 #include
5 #include
6 #include
7 int main()
1 #include
2 #include
3 #include
4 #include
5 #include
6 #include
7 int main()
8 {
9 if(mkfifo("mypipe",0644)<0)
10 {
11 perror("mkfifo");
12 exit(0);
13 }
14 int rfd=open("mypipe",O_RDONLY);
15 if(rfd<0)
16 {
17 perror("open");
18 exit(1);
19 }
20 char buf[1024];
21 while(1)
22 {
23 buf[0]=0;
24 printf("please wait ...\n");
25 ssize_t s=read(rfd,buf,sizeof(buf)-1);
26 if(s>0)
27 {
28
29 buf[s-1]=0;
30 printf("client say:%s\n",buf);
31
32 }
33 else if(s==0)
34 {
35 printf("client quit,please quie now\n");
36 exit(1);
37 }
38 else {
39 exit(1);
40 }
41
42 }
43 close(rfd);
44 return 0;
1 #ifndef _COMM_H_
2 #define _COMM_H_
3 #include
4 #include
5 #include
6 #include
7 #include
8 #include
9 #define PATHNAME "./"
10 #define PROJ_ID 0x666
11 #define SERVER_TYPE 1
12 #define CLIENT_TYPE 2
13 struct msgbuf{
14
15 long mtype;
16 char mtext[1024];
17 };
18 int creatMsgQueuq();
19 int getMsgQueuq();
20 int destroyMsgQueu(int msgid);
21 int sendMsg(int msgid,int who,char*msg);
22 int recvMsg(int msgid,int recvType,char out[]);
23 #endif
1 #include"comm.h"
2 static int commMsgQueue(int flag)
3 {
4
5 key_t _key=ftok(PATHNAME,PROJ_ID);
6 if(_key<0)
7 {
8 perror("ftok");
9 return -1;
10 }
11 int msgid=msgget(_key,flag);
12 if(msgid<0)
13 {
14 perror("msgget");
15 return -1;
16 }
17 return msgid;
18 }
19 int createMsgQueue()
20 {
21
22 return commMsgQueue(IPC_CREAT|IPC_EXCL|0666);
23 }
24 int getMsgQueue()
25 {
26
27 return commMsgQueue(IPC_CREAT);
28 }
29 int destoryMsgQueue(int msgid)
30 {
31 if(msgctl(msgid,IPC_RMID,NULL)<0)
32 {
33 perror("msgctl");
34 return -1;
35 }
36 return 0;
37
38 }
39 int sendMsg(int msgid,int who,char *msg)
40 {
41 struct msgbuf buf;
42 buf.mtype=who;
43 strcpy(buf.mtext,msg);
44 if(msgsnd(msgid,(void *)&buf,sizeof(buf.mtext),0)<0)
45 {
46 perror("msgsnd");
47 return -1;
48 }
49
50 return 0;
51 }
52 int recvMsg(int msgid,int recvType,char out[])
53 {
54 struct msgbuf buf;
55 if(msgrcv(msgid,(void*)&buf,sizeof(buf.mtext),recvType,0)<0)
56 {
57 perror("msgrcv");
58 return -1;
59 }
60 strcpy(out,buf.mtext);
61 return 0;
62 }
1 #include"comm.h"
2 int main()
3 {
4 int msgid=createMsgQueue();
5 char buf[1024];
6 while(1)
7 {
8 buf[0]=0;
9 printf("please Enter:");
10 fflush(stdout);
11 ssize_t s=read(0,buf,sizeof(buf));
12 if(s>0)
13 {
14 buf[s-1]=0;
15 sendMsg(msgid,CLIENT_TYPE,buf);
16 printf("send done,wait recv..\n");
17 }
18 recvMsg(msgid,SERVER_TYPE,buf);
19 printf("server# %s\n",buf);
20 }
21 return 0;
22 }
~
1 #include"comm.h"
2 int main()
3 {
4
5 int msgid=createMsgQueue();
6 char buf[1024];
7 while(1)
8 {
9 buf[0]=0;
10 recvMsg(msgid,CLIENT_TYPE,buf);
11 printf("client#%s",buf);
12 printf("please enter #");
13 fflush(stdout);
14 ssize_t s=read(0,buf,sizeof(buf));
15 if(s>0)
16 {
17
18 buf[s-1]=0;
19 sendMsg(msgid,SERVER_TYPE,buf);
20 printf("send done,wait recv ...\n");
21 }
22 }
23 destroyMsgQueue(msgid);
24 return 0;
25
26 }
1 #ifndef _COMM_H_
2 #define _COMM_H_
3 #include
4 #include
5 #include
6 #include
7 #define PATHNAME "./"
8 #define PROJ_ID 0x6666
9 union semun
10 {
11 int val;
12 struct semid_ds *buf;
13 unsigned short *array;
14 struct seminfo *__buf;
15
16 };
17 int createSemSet(int nums);
18 int initSem(int semid,int nums,int initVal);
19 int getSemSet(int nums);
20 int P(int semid, int who);
21 int V(int semid, int who);
22 int destorySemSet(int semid);
23 #endif
1 #include"comm.h"
2 static int commSemSet(int nums,int flags)
3 {
4 key_t _key=ftok(PATHNAME,PROJ_ID);
5 if(_key<0)
6 {
7 perror("ftok");
8 return -1;
9 }
10 int semid=semget(_key,nums,flags);
11 if(semid<0)
12 {
13 printf("semget");
14 return -1;
15 }
16 return semid;
17 }
18 int createSemSet(int nums)
19 {
20 return commSemSet(nums,IPC_CREAT|IPC_EXCL|0666);
21
22 }
23 int getSemSet(int nums)
24 {
25 return commSemSet( nums,IPC_CREAT);
26 }
27 int initSem(int semid,int nums,int initval)
28 {
29 union semun _un;
30 _un.val=initval;
31 if(semctl(semid,nums,SETVAL,_un)<0)
32 {
33 perror("semctl");
34 return -1;
35 }
36 return 0;
37 }
38 static int commPV(int semid,int who,int op)
39 {
40 struct sembuf _sf;
41 _sf.sem_num=who;
42 _sf.sem_op=op;
43 _sf.sem_flg=0;
44 if(semop(semid,&_sf,1)<0)
45 {
46 perror("semop");
47 return -1;
48 }
49 return 0;
50 }
51 int P( int semid,int who)
52 {
53
54 return commPV(semid,who,-1);
55 }
56 int V(int semid,int who)
57 {
58 return commPV(semid,who,1);
59
60 }
61 int destroySemSet(int semid)
62 {
63
64 if(semctl(semid,0,IPC_RMID)<0)
65 {
66 perror("semctl");
67 return -1;
68 }
69 }
2 int main()
3 {
4 int semid=createSemSet(1);
5 initSem(semid,0,1);
6 pid_t id=fork();
7 if(id==0)
8 {
9 //child
10 int _semid=getSemSet(0);
11 while(1)
12 {
13 P(_semid,0);
14 printf("A");
15 fflush(stdout);
16 usleep(123456);
17 printf("A");
18 fflush(stdout);
19 usleep(321456);
20 V(_semid,0);
21 }
22 }
23 else
24 {
25 while(1)
26 {
27 P(semid,0);
28 printf("B");
29 fflush(stdout);
30 usleep(223456);
31 printf("B");
32 fflush(stdout);
33 usleep(123456);
34 V(semid,0);
35 }
36 wait(NULL);
37 }
38 destroySemSet(semid);
39 return 0;
40 }