�༭�ƽ�: |
�������ը��˲���zh����-�ź�
�����ľ�ͨ������˵����������ipc�ķ�ʽͨ���йܵ������������ܵ��������ܵ�������ϣ���с��ź����������洢��socket��streams�ȡ� |
|
���̼�ͨ�ţ�ipc��interprocess communication����ָ�ڲ�ͬ����֮�䴫������ϣ��
���� socket��streams֧�ֲ�ͬ�����ϵ���������ipc��
��linux�е�c���ա��ϊ����
һ���ܵ�
�ܵ���ͨ��ָ�����ܵ����� unix ϵͳipc����ϵ���ʽ��
1���ص㣺
���ǰ�˫���ģ�������ֻ����һ�������������������й̶��ķ��˺�д�ˡ�
��ֻ�����ھ�����ե��ϵ�ľ���֮���ͨ�ţ�ҳ�ǹ��ӽ��̻����ֵܽ���֮�䣩��
�����կ�����һ��������ļ����������ķ�дҳ����ʹ����ͨ��read��write �ⱥ�����������������ͨ���ļ����������������κ��ļ�ϵͳ������ֻ�������ڴ��с�
2��ԭ�ͣ�
1 #include
2 int pipe(int fd[2]); // ����ֵ�����ɹ�����0��ʧ�ܷ���-1
��һ���ܵ�����ʱ�����ᴴ�������ļ���������fd[0]ϊ������fd[1]ϊд��������ͼ��

ҫ�رչܵ�ֻ�轫�������ļ��������رռ��ɡ�
3������
���������еĺܵ�����û���κ��ô������ԣ�ͨ������ pipe �ľ��̽��ŵ��� fork�������ʹ����˸��������ӽ���֮���
ipc ͨ��������ͼ��ʾ��

��ҫ�������ӹ����������ӽ��̣���رո����̵ķ��ˣ�fd[0]�����ӽ��̵�д�ˣ�fd[1]������֮�������ʹ���������ӽ��������̡�
1 #include
2 #include
3
4 int main()
5 {
6 int fd[2]; // �����ļ�������
7 pid_t pid;
8 char buff[20];
9
10 if(pipe(fd) < 0) // �����ܵ�
11 printf("create pipe error!\n");
12
13 if((pid = fork()) < 0) // �����ӽ���
14 printf("fork error!\n");
15 else if(pid > 0) // ������
16 {
17 close(fd[0]); // �رն���
18 write(fd[1], "hello world\n", 12);
19 }
20 else
21 {
22 close(fd[1]); // �ر�д��
23 read(fd[0], buff, 20);
24 printf("%s", buff);
25 }
26
27 return 0;
28 } |
����fifo
fifo��ҳ��ϊ�����ܵ�������һ���ļ����͡�
1���ص�
fifo�������صľ���֮�佻�����ݣ��������ܵ���ͬ��
fifo��·������֮�����������һ�������豸�ļ���ʽ�������ļ�ϵͳ�с�
2��ԭ��
1 #include
2 // ����ֵ���ɹ�����0����������-1
3 int mkfifo(const char *pathname, mode_t mode);
|
���е� mode ������open�����е� mode ��ͬ��һ��������һ�� fifo���Ϳ�����һ����ļ�i/o������������
�� open һ��fifoʱ���ƿ����÷�������־��o_nonblock��������
��û��ָ��o_nonblock��ĭ�ϣ���ֻ�� open ҫ������ij����������ϊд���� fifo�����ƶģ�ֻд
open ҫ������ij����������ϊ����������
��ָ����o_nonblock����ֻ�� open �������ء���ֻд open ���������� -1 ���û�н����ѿ�ϊ������
fifo����errno��enxio��
3������
fifo��ͨ�ŷ�ʽ�������ڽ�����ʹ���ļ����������ݣ�ֻ����fifo�����ļ�ͬʱ���йܵ������ԡ������ݶ���ʱ��fifo�ܵ���ͬʱ������ݣ����ҡ��ƚ��ȳ����������������ʾ��ʹ��
fifo ���� ipc �ĺ��̣�
write_fifo.c
1 #include
2 #include // exit
3 #include // o_wronly
4 #include
5 #include // time
6
7 int main()
8 {
9 int fd;
10 int n, i;
11 char buf[1024];
12 time_t tp;
13
14 printf("i am %d process.\n", getpid());
// ˵������id
15
16 if((fd = open("fifo1", o_wronly))
< 0) // ��д��һ��fifo
17 {
18 perror("open fifo failed");
19 exit(1);
20 }
21
22 for(i=0; i<10; i)
23 {
24 time(&tp); // ȡϵͳ��ǰʱ��
25 n=sprintf(buf,"process %d's time is %s",getpid(),ctime(&tp));
26 printf("send message: %s", buf);
// ��ӡ
27 if(write(fd, buf, n 1) < 0) // д�뵽fifo��
28 {
29 perror("write fifo failed");
30 close(fd);
31 exit(1);
32 }
33 sleep(1); // ����1��
34 }
35
36 close(fd); // �ر�fifo�ļ�
37 return 0;
38 } |
read_fifo.c
1 #include
2 #include
3 #include
4 #include
5 #include
6
7 int main()
8 {
9 int fd;
10 int len;
11 char buf[1024];
12
13 if(mkfifo("fifo1", 0666) < 0 &&
errno!=eexist) // ����fifo�ܵ�
14 perror("create fifo failed");
15
16 if((fd = open("fifo1", o_rdonly))
< 0) // �զ���fifo
17 {
18 perror("open fifo failed");
19 exit(1);
20 }
21
22 while((len = read(fd, buf, 1024)) > 0) //
��ȡfifo�ܵ�
23 printf("read message: %s", buf);
24
25 close(fd); // �ر�fifo�ļ�
26 return 0;
27 } |
�������ն����� gcc �ֱ�����������������ļ������կ������������£�
1 [cheesezh@localhost]$
./write_fifo
2 i am 5954 process.
3 send message: process 5954's time is mon apr
20 12:37:28 2015
4 send message: process 5954's time is mon apr
20 12:37:29 2015
5 send message: process 5954's time is mon apr
20 12:37:30 2015
6 send message: process 5954's time is mon apr
20 12:37:31 2015
7 send message: process 5954's time is mon apr
20 12:37:32 2015
8 send message: process 5954's time is mon apr
20 12:37:33 2015
9 send message: process 5954's time is mon apr
20 12:37:34 2015
10 send message: process 5954's time is mon apr
20 12:37:35 2015
11 send message: process 5954's time is mon apr
20 12:37:36 2015
12 send message: process 5954's time is mon apr
20 12:37:37 2015 |
1 [cheesezh@localhost]$
./read_fifo
2 read message: process 5954's time is mon apr
20 12:37:28 2015
3 read message: process 5954's time is mon apr
20 12:37:29 2015
4 read message: process 5954's time is mon apr
20 12:37:30 2015
5 read message: process 5954's time is mon apr
20 12:37:31 2015
6 read message: process 5954's time is mon apr
20 12:37:32 2015
7 read message: process 5954's time is mon apr
20 12:37:33 2015
8 read message: process 5954's time is mon apr
20 12:37:34 2015
9 read message: process 5954's time is mon apr
20 12:37:35 2015
10 read message: process 5954's time is mon apr
20 12:37:36 2015
11 read message: process 5954's time is mon apr
20 12:37:37 2015 |
�������ӿ�����չ�� �ͻ����̡����������� ͨ�ŵ�ʵ����write_fifo�����������ڿͻ��ˣ����դ���ͻ�����һ������������������ϣ��read_fifo�����ڷ�����������ʱ�����fifo�ķ��ˣ���������ʱ�����������д�����������һ���ؼ��������ǣ�ÿһ���ͻ��˱���Ԥ��֪���������ṩ��fifo�ӿڣ���ͼ��ʾ�����ְ��ţ�

������ϣ����
��ϣ���у�����ϣ�����ӱ���������ں��с�һ����ϣ������һ����ʶ����������id������ʶ��
1���ص�
��ϣ�����������¼�ģ����е���ϣ�����ض��ĸ�ʽ�լ��ض������ȼ���
��ϣ���ж����ڷ�������ս��̡�������ֹʱ����ϣ���м������ݲ����ᱻɾ����
��ϣ���п���ʵ����ϣ�������ѯ,��ϣ��һ��ҫ���ƚ��ȳ��ĵ����ȡ,ҳ������ϣ�����ͷ�ȡ��
2��ԭ��
1 #include
2 // ���������ϣ���у��ɹ����ض���id��ʧ�ܷ���-1
3 int msgget(key_t key, int flag);
4 // ������ϣ���ɹ�����0��ʧ�ܷ���-1
5 int msgsnd(int msqid, const void *ptr, size_t
size, int flag);
6 // ��ȡ��ϣ���ɹ�������ϣ���ݵij��ȣ�ʧ�ܷ���-1
7 int msgrcv(int msqid, void *ptr, size_t size,
long type,int flag);
8 // ������ϣ���у��ɹ�����0��ʧ�ܷ���-1
9 int msgctl(int msqid, int cmd, struct msqid_ds
*buf); |
��������������£�msgget������һ���µ���ϣ���у�
���û�����ֵkey���ӧ����ϣ���у�����flag�а�����ipc_creat��־λ��
key����ϊipc_private��
����msgrcv�ڶ�ȡ��ϣ����ʱ��type���������漸�������
type == 0�����ض����еĵ�һ����ϣ��
type > 0�����ض�������ϣ����ϊ type �ĵ�һ����ϣ��
type < 0�����ض�������ϣ����ֵс�ڻ���� type ����ֵ����ϣ������ж������ȡ����ֵ��с����ϣ��
���կ�����typeֵ�� 0 ʱ�����է��ƚ��ȳ��������ϣ��ҳ���� type �������ȼ���ȩֵ���������ij������ͣ�������google֮��
3������
����д��һ����ʹ����ϣ���н���ipc�����ӣ�����˳���һֱ�ڵȴ��ض����͵���ϣ�����յ������͵���ϣ�ժ�����һ���ض����͵���ϣ��ϊ�������ͻ��˶�ȡ�÷�������ӡ������
msg_server.c
1 #include
2 #include
3 #include
4
5 // ���ڴ���һ��ψһ��key
6 #define msg_file "/etc/passwd"
7
8 // ��ϣ�ṹ
9 struct msg_form {
10 long mtype;
11 char mtext[256];
12 };
13
14 int main()
15 {
16 int msqid;
17 key_t key;
18 struct msg_form msg;
19
20 // ��ȡkeyֵ
21 if((key = ftok(msg_file,'z')) < 0)
22 {
23 perror("ftok error");
24 exit(1);
25 }
26
27 // ��ӡkeyֵ
28 printf("message queue - server key is:
%d.\n", key);
29
30 // ������ϣ����
31 if ((msqid = msgget(key, ipc_creat|0777)) ==
-1)
32 {
33 perror("msgget error");
34 exit(1);
35 }
36
37 // ��ӡ��ϣ����id������id
38 printf("my msqid is: %d.\n", msqid);
39 printf("my pid is: %d.\n", getpid());
40
41 // ѭ����ȡ��ϣ
42 for(;;)
43 {
44 msgrcv(msqid, &msg, 256, 888, 0);// ��������ϊ888�ĵ�һ����ϣ
45 printf("server: receive msg.mtext is:
%s.\n", msg.mtext);
46 printf("server: receive msg.mtype is:
%d.\n", msg.mtype);
47
48 msg.mtype = 999; // �ͻ��˽��յ���ϣ����
49 sprintf(msg.mtext, "hello, i'm server
%d", getpid());
50 msgsnd(msqid, &msg, sizeof(msg.mtext),
0);
51 }
52 return 0;
53 } |
msg_client.c
1 #include
2 #include
3 #include
4
5 // ���ڴ���һ��ψһ��key
6 #define msg_file "/etc/passwd"
7
8 // ��ϣ�ṹ
9 struct msg_form {
10 long mtype;
11 char mtext[256];
12 };
13
14 int main()
15 {
16 int msqid;
17 key_t key;
18 struct msg_form msg;
19
20 // ��ȡkeyֵ
21 if ((key = ftok(msg_file, 'z')) < 0)
22 {
23 perror("ftok error");
24 exit(1);
25 }
26
27 // ��ӡkeyֵ
28 printf("message queue - client key is:
%d.\n", key);
29
30 // ����ϣ����
31 if ((msqid = msgget(key, ipc_creat|0777)) ==
-1)
32 {
33 perror("msgget error");
34 exit(1);
35 }
36
37 // ��ӡ��ϣ����id������id
38 printf("my msqid is: %d.\n", msqid);
39 printf("my pid is: %d.\n", getpid());
40
41 // ������ϣ������ϊ888
42 msg.mtype = 888;
43 sprintf(msg.mtext, "hello, i'm client
%d", getpid());
44 msgsnd(msqid, &msg, sizeof(msg.mtext),
0);
45
46 // ��ȡ����ϊ777����ϣ
47 msgrcv(msqid, &msg, 256, 999, 0);
48 printf("client: receive msg.mtext is:
%s.\n", msg.mtext);
49 printf("client: receive msg.mtype is:
%d.\n", msg.mtype);
50 return 0;
51 } |
�ġ��ź���
�ź�����semaphore�����ѿ����ܹ��� ipc �ṹ��ͬ������һ�����������ź�������ʵ�ֽ��̼�ļ�����ͬ�������������ڴ洢���̼�ͨ�����ݡ�
1���ص�
�ź������ڽ��̼�ͬ������ҫ�ڽ��̼䴫��������ҫ��ϲ����ڴ档
�ź������ڲ���ϵͳ�� pv ������������ź����ij�������ԭ�ӳ�����
ÿ�ζ��ź����� pv �����������ڶ��ź���ֵ�� 1 ��� 1�����ҿ��լӽ�������������
֧���ź����顣
2��ԭ��
����ź�����ֻ��ȡ 0 �� 1 �ı�������ҳ���ź��������һ����ʽ��������ֵ�ź�����binary
semaphore����������ȡ������������ź�������ϊͨ���ź�����
linux �µ��ź�������������ͨ�õ��ź��������ͻ��в�������������һ����һ�ķ�ֵ�ź����ͻ��в�����
1 #include
2 // �������ȡһ���ź����飺���ɹ������ź�����id��ʧ�ܷ���-1
3 int semget(key_t key, int num_sems, int sem_flags);
4 // ���ź�������в������ı��ź�����ֵ���ɹ�����0��ʧ�ܷ���-1
5 int semop(int semid, struct sembuf semoparray[],
size_t numops);
6 // �����ź����������ϣ
7 int semctl(int semid, int sem_num, int cmd,
...); |
��semget�����µ��ź�������ʱ������ָ���������ź����ĸ�������num_sems����ͨ��ϊ1�� ���������һ�����еļ��ϣ���num_semsָ��ϊ
0 ��
��semop�����у�sembuf�ṹ�ķ������£�
1 struct sembuf
2 {
3 short sem_num; // �ź������ж�ӧ����ţ�0��sem_nums-1
4 short sem_op; // �ź���ֵ��һ�β����еĸı���
5 short sem_flg; // ipc_nowait, sem_undo
6 } |
���� sem_op ��һ�β����е��ź����ĸı�����
��sem_op > 0����ʾ�����ͷ���ӧ����դ������ sem_op ��ֵ�ӵ��ź�����ֵ�ϡ�����н����������ߵȴ����ź������������ǡ�
��sem_op < 0������ sem_op �ľ���ֵ����դ��
�����ӧ����դ�����������������ź�����ֵ��ȥsem_op�ľ���ֵ�������ɹ����ء�
����ӧ����դ��������������ʱ�����������sem_flg�йء�
sem_flg ָ��ipc_nowait����semop������������eagain��
sem_flg û��ָ��ipc_nowait�����ź�����semncntֵ��1��ȼ����̹���ֱ���������������
����ӧ����դ���������������ź�����semncntֵ��1�����ź�����ֵ��ȥsem_op�ľ���ֵ���ɹ����أ�
���ź�����ɾ��������smeop��������eidrm��
���̲����źţ������źŵ����������أ�������½����ź�����semncntֵ��1������semop��������eintr
��sem_op == 0����������ֱ���ź�������ӧֵϊ0��
���ź����ѿ�ϊ0�������������ء�
����ź�����ֵ��ϊ0��������sem_flg��������������
sem_flgָ��ipc_nowait�����������eagain��
sem_flgû��ָ��ipc_nowait�����ź�����semncntֵ��1��ȼ����̹���ֱ���������������
�ź���ֵϊ0�����ź�����semzcnt��ֵ��1������semop�ɹ����أ�
���ź�����ɾ��������smeop��������eidrm��
���̲����źţ������źŵ����������أ��ڴ���������ź�����semncntֵ��1������semop��������eintr
��semctl�����е������ж��֣������˵�������õģ�
setval�����ڳ�ʼ���ź���ϊһ����֪��ֵ������ҫ��ֵ��ϊ����semun��val��ա�����ݡ����ź�����һ��ʹ��֮ǰ��ҫ�����ź�����
ipc_rmid��ɾ��һ���ź������ϡ������ɾ���ź���������������ϵͳ�д��ڣ���ʹ�����ѿ��˳��������������´����д˳���ʱ�������⣬�����ź�����һ��������դ��
3������
1 #include
2 #include
3 #include
4
5 // �����壬����semctl��ʼ��
6 union semun
7 {
8 int val; /*for setval*/
9 struct semid_ds *buf;
10 unsigned short *array;
11 };
12
13 // ��ʼ���ź���
14 int init_sem(int sem_id, int value)
15 {
16 union semun tmp;
17 tmp.val = value;
18 if(semctl(sem_id, 0, setval, tmp) == -1)
19 {
20 perror("init semaphore error");
21 return -1;
22 }
23 return 0;
24 }
25
26 // p����:
27 // ���ź���ֵϊ1����ȡ��դ�����ź���ֵ-1
28 // ���ź���ֵϊ0�����̹���ȴ�
29 int sem_p(int sem_id)
30 {
31 struct sembuf sbuf;
32 sbuf.sem_num = 0; /*���*/
33 sbuf.sem_op = -1; /*p����*/
34 sbuf.sem_flg = sem_undo;
35
36 if(semop(sem_id, &sbuf, 1) == -1)
37 {
38 perror("p operation error");
39 return -1;
40 }
41 return 0;
42 }
43
44 // v������
45 // �ͷ���դ�����ź���ֵ 1
46 // ����н������ڹ���ȴ�����������
47 int sem_v(int sem_id)
48 {
49 struct sembuf sbuf;
50 sbuf.sem_num = 0; /*���*/
51 sbuf.sem_op = 1; /*v����*/
52 sbuf.sem_flg = sem_undo;
53
54 if(semop(sem_id, &sbuf, 1) == -1)
55 {
56 perror("v operation error");
57 return -1;
58 }
59 return 0;
60 }
61
62 // ɾ���ź�����
63 int del_sem(int sem_id)
64 {
65 union semun tmp;
66 if(semctl(sem_id, 0, ipc_rmid, tmp) == -1)
67 {
68 perror("delete semaphore error");
69 return -1;
70 }
71 return 0;
72 }
73
74
75 int main()
76 {
77 int sem_id; // �ź�����id
78 key_t key;
79 pid_t pid;
80
81 // ��ȡkeyֵ
82 if((key = ftok(".", 'z')) < 0)
83 {
84 perror("ftok error");
85 exit(1);
86 }
87
88 // �����ź�����������ֻ��һ���ź���
89 if((sem_id = semget(key, 1, ipc_creat|0666))
== -1)
90 {
91 perror("semget error");
92 exit(1);
93 }
94
95 // ��ʼ������ֵ��ϊ0��դ��ռ��
96 init_sem(sem_id, 0);
97
98 if((pid = fork()) == -1)
99 perror("fork error");
100 else if(pid == 0) /*�ӽ���*/
101 {
102 sleep(2);
103 printf("process child: pid=%d\n",
getpid());
104 sem_v(sem_id); /*�ͷ���դ*/
105 }
106 else /*������*/
107 {
108 sem_p(sem_id); /*�ȴ���դ*/
109 printf("process father: pid=%d\n",
getpid());
110 sem_v(sem_id); /*�ͷ���դ*/
111 del_sem(sem_id); /*ɾ���ź�����*/
112 }
113 return 0;
114 } |
�����������������ź��������̻���ִ����ϡ���������ź����ø����̵ȴ��ӽ���ִ�����ժ���ִ�с�
�塢�����ڴ�
�����ڴ棨shared memory����ָ�����������̹���һ�������ĵ洢����
1���ص�
�����ڴ�������һ�� ipc����ϊ������ֱ�ӷ��ڴ���д�ȡ��
��ϊ������̿���ͬʱ������������ҫ����ͬ����
�ź��� �����ڴ�ͨ�������һ��ʹ�ã��ź�������ͬ���թ����ڴ�ķ��ʡ�
2��ԭ��
1 #include
2 // �������ȡһ�������ڴ棺�ɹ����ع����ڴ�id��ʧ�ܷ���-1
3 int shmget(key_t key, size_t size, int flag);
4 // ���ӹ����ڴ浽��ǰ���̵ĵ�ַ�ռ䣺�ɹ�����ָ�����ڴ��ָ�룬ʧ�ܷ���-1
5 void *shmat(int shm_id, const void *addr, int
flag);
6 // �ͽ��빲���ڴ�����ӣ��ɹ�����0��ʧ�ܷ���-1
7 int shmdt(void *addr);
8 // ���ƹ����ڴ�������ϣ���ɹ�����0��ʧ�ܷ���-1
9 int shmctl(int shm_id, int cmd, struct shmid_ds
*buf); |
����shmget��������һ�ι����ڴ�ʱ������ָ���� size�����������һ���ѵ��ڵĺ����ڴ棬��
size ָ��ϊ0 ��
��һ�ι����ڴ汻�����ժ��������ܱ��κν��̷��ʡ�����ʹ��shmat�������ӹù����ڴ浽��ǰ���̵ĵ�ַ�ռ䣬���ӳɹ���ѹ����ڴ�������ӳ�䵽���ý��̵ĵ�ַ�ռ䣬�����ؿռ�һ�����ʡ�
shmdt�����������ͽ�shmat���������ӵġ�ע�⣬�ⲣ���ǵ�ϵͳ��ɾ���ù����ڴ棬ֻ�ǵ�ǰ���̲����ٷ��ʸù����ڴ���ѡ�
shmctl�������զթ����ڴ�ִ�ж��ֲ��������ݲ��� cmd ִ����ӧ�ij��������õ���ipc_rmid����ϵͳ��ɾ���ù����ڴ棩��
3������
����������ӣ�ʹ���ˡ������ڴ� �ź��� ��ϣ���с��������ʵ�ַ�����������ͻ����̼��ͨ�š�
�����ڴ������������ݣ�
�ź�������ͬ����
��ϣ�������� �ڿͻ������˹����ڴ�� ֪ͨ��������ȡ��
server.c
1 #include
2 #include
3 #include // shared memory
4 #include // semaphore
5 #include // message queue
6 #include // memcpy
7
8 // ��ϣ���нṹ
9 struct msg_form {
10 long mtype;
11 char mtext;
12 };
13
14 // �����壬����semctl��ʼ��
15 union semun
16 {
17 int val; /*for setval*/
18 struct semid_ds *buf;
19 unsigned short *array;
20 };
21
22 // ��ʼ���ź���
23 int init_sem(int sem_id, int value)
24 {
25 union semun tmp;
26 tmp.val = value;
27 if(semctl(sem_id, 0, setval, tmp) == -1)
28 {
29 perror("init semaphore error");
30 return -1;
31 }
32 return 0;
33 }
34
35 // p����:
36 // ���ź���ֵϊ1����ȡ��դ�����ź���ֵ-1
37 // ���ź���ֵϊ0�����̹���ȴ�
38 int sem_p(int sem_id)
39 {
40 struct sembuf sbuf;
41 sbuf.sem_num = 0; /*���*/
42 sbuf.sem_op = -1; /*p����*/
43 sbuf.sem_flg = sem_undo;
44
45 if(semop(sem_id, &sbuf, 1) == -1)
46 {
47 perror("p operation error");
48 return -1;
49 }
50 return 0;
51 }
52
53 // v������
54 // �ͷ���դ�����ź���ֵ 1
55 // ����н������ڹ���ȴ�����������
56 int sem_v(int sem_id)
57 {
58 struct sembuf sbuf;
59 sbuf.sem_num = 0; /*���*/
60 sbuf.sem_op = 1; /*v����*/
61 sbuf.sem_flg = sem_undo;
62
63 if(semop(sem_id, &sbuf, 1) == -1)
64 {
65 perror("v operation error");
66 return -1;
67 }
68 return 0;
69 }
70
71 // ɾ���ź�����
72 int del_sem(int sem_id)
73 {
74 union semun tmp;
75 if(semctl(sem_id, 0, ipc_rmid, tmp) == -1)
76 {
77 perror("delete semaphore error");
78 return -1;
79 }
80 return 0;
81 }
82
83 // ����һ���ź�����
84 int creat_sem(key_t key)
85 {
86 int sem_id;
87 if((sem_id = semget(key, 1, ipc_creat|0666))
== -1)
88 {
89 perror("semget error");
90 exit(-1);
91 }
92 init_sem(sem_id, 1); /*��ֵ��ϊ1��դδռ��*/
93 return sem_id;
94 }
95
96
97 int main()
98 {
99 key_t key;
100 int shmid, semid, msqid;
101 char *shm;
102 char data[] = "this is server";
103 struct shmid_ds buf1; /*����ɾ�������ڴ�*/
104 struct msqid_ds buf2; /*����ɾ����ϣ����*/
105 struct msg_form msg; /*��ϣ��������֪ͨ�է������˹����ڴ�*/
106
107 // ��ȡkeyֵ
108 if((key = ftok(".", 'z')) < 0)
109 {
110 perror("ftok error");
111 exit(1);
112 }
113
114 // ���������ڴ�
115 if((shmid = shmget(key, 1024, ipc_creat|0666))
== -1)
116 {
117 perror("create shared memory error");
118 exit(1);
119 }
120
121 // ���ӹ����ڴ�
122 shm = (char*)shmat(shmid, 0, 0);
123 if((int)shm == -1)
124 {
125 perror("attach shared memory error");
126 exit(1);
127 }
128
129
130 // ������ϣ����
131 if ((msqid = msgget(key, ipc_creat|0777))
== -1)
132 {
133 perror("msgget error");
134 exit(1);
135 }
136
137 // �����ź���
138 semid = creat_sem(key);
139
140 // ������
141 while(1)
142 {
143 msgrcv(msqid, &msg, 1, 888, 0); /*��ȡ����ϊ888����ϣ*/
144 if(msg.mtext == 'q') /*quit - ����ѭ��*/
145 break;
146 if(msg.mtext == 'r') /*read - �������ڴ�*/
147 {
148 sem_p(semid);
149 printf("%s\n",shm);
150 sem_v(semid);
151 }
152 }
153
154 // �ͽ�����
155 shmdt(shm);
156
157 /*ɾ�������ڴ桢��ϣ���с��ź���*/
158 shmctl(shmid, ipc_rmid, &buf1);
159 msgctl(msqid, ipc_rmid, &buf2);
160 del_sem(semid);
161 return 0;
162 } |
client.c
1 #include
2 #include
3 #include // shared memory
4 #include // semaphore
5 #include // message queue
6 #include // memcpy
7
8 // ��ϣ���нṹ
9 struct msg_form {
10 long mtype;
11 char mtext;
12 };
13
14 // �����壬����semctl��ʼ��
15 union semun
16 {
17 int val; /*for setval*/
18 struct semid_ds *buf;
19 unsigned short *array;
20 };
21
22 // p����:
23 // ���ź���ֵϊ1����ȡ��դ�����ź���ֵ-1
24 // ���ź���ֵϊ0�����̹���ȴ�
25 int sem_p(int sem_id)
26 {
27 struct sembuf sbuf;
28 sbuf.sem_num = 0; /*���*/
29 sbuf.sem_op = -1; /*p����*/
30 sbuf.sem_flg = sem_undo;
31
32 if(semop(sem_id, &sbuf, 1) == -1)
33 {
34 perror("p operation error");
35 return -1;
36 }
37 return 0;
38 }
39
40 // v������
41 // �ͷ���դ�����ź���ֵ 1
42 // ����н������ڹ���ȴ�����������
43 int sem_v(int sem_id)
44 {
45 struct sembuf sbuf;
46 sbuf.sem_num = 0; /*���*/
47 sbuf.sem_op = 1; /*v����*/
48 sbuf.sem_flg = sem_undo;
49
50 if(semop(sem_id, &sbuf, 1) == -1)
51 {
52 perror("v operation error");
53 return -1;
54 }
55 return 0;
56 }
57
58
59 int main()
60 {
61 key_t key;
62 int shmid, semid, msqid;
63 char *shm;
64 struct msg_form msg;
65 int flag = 1; /*whileѭ������*/
66
67 // ��ȡkeyֵ
68 if((key = ftok(".", 'z')) < 0)
69 {
70 perror("ftok error");
71 exit(1);
72 }
73
74 // ��ȡ�����ڴ�
75 if((shmid = shmget(key, 1024, 0)) == -1)
76 {
77 perror("shmget error");
78 exit(1);
79 }
80
81 // ���ӹ����ڴ�
82 shm = (char*)shmat(shmid, 0, 0);
83 if((int)shm == -1)
84 {
85 perror("attach shared memory error");
86 exit(1);
87 }
88
89 // ������ϣ����
90 if ((msqid = msgget(key, 0)) == -1)
91 {
92 perror("msgget error");
93 exit(1);
94 }
95
96 // ��ȡ�ź���
97 if((semid = semget(key, 0, 0)) == -1)
98 {
99 perror("semget error");
100 exit(1);
101 }
102
103 // ���
104 printf("***************************************\n");
105 printf("* ipc *\n");
106 printf("* input r to send data to server.
*\n");
107 printf("* input q to quit. *\n");
108 printf("***************************************\n");
109
110 while(flag)
111 {
112 char c;
113 printf("please input command: ");
114 scanf("%c", &c);
115 switch(c)
116 {
117 case 'r':
118 printf("data to send: ");
119 sem_p(semid); /*������դ*/
120 scanf("%s", shm);
121 sem_v(semid); /*�ͷ���դ*/
122 /*��ձ����뻺����*/
123 while((c=getchar())!='\n' && c!=eof);
124 msg.mtype = 888;
125 msg.mtext = 'r'; /*������ϣ֪ͨ������������*/
126 msgsnd(msqid, &msg, sizeof(msg.mtext),
0);
127 break;
128 case 'q':
129 msg.mtype = 888;
130 msg.mtext = 'q';
131 msgsnd(msqid, &msg, sizeof(msg.mtext),
0);
132 flag = 0;
133 break;
134 default:
135 printf("wrong input!\n");
136 /*��ձ����뻺����*/
137 while((c=getchar())!='\n' && c!=eof);
138 }
139 }
140
141 // �ͽ�����
142 shmdt(shm);
143
144 return 0;
145 } |
ע�⣺��scanf()�����ַ����ַ���ʱ������������������\n������ÿ�������������ҫ��ձ�����ļ���������������
gcc ��������֧��fflush(stdin)����ֻ�dz�c����չ������������ʹ�������������
1 while((c=getchar())!='\n'
&& c!=eof); |
ԭ�����ӣ�https://www.cnblogs.com/cheesezh/p/5264465.html
|