POSIX共享内存实现进程间通信

1、进程a:

 1 /*
 2  ============================================================================
 3  Name        : timer.c
 4  Author      : liuguanghui
 5  Version     :
 6  Copyright   : Your copyright notice
 7  Description : Hello World in C, Ansi-style
 8  ============================================================================
 9  */
10 
11 #include <stdio.h>
12 #include <fcntl.h>
13 #include <stdlib.h>
14 #include <unistd.h>
15 #include <sys/mman.h>
16 #include <sys/stat.h>
17 #include <sys/types.h>
18 #include <errno.h>
19 #include <semaphore.h>
20 #include <pthread.h>
21 
22 //利用select函数完成一个定时器的功能;
23 void setTimer(int seconds, int microseconds)
24 {
25     struct timeval temp;
26     temp.tv_sec = seconds;
27     temp.tv_usec = microseconds;
28     select(0, NULL, NULL, NULL, &temp);
29     return ;
30 }
31 
32 struct shm_data
33 {
34     int data_int;
35     double data_double;
36 };
37 
38 int main()
39 {
40     int  fd;
41     struct shm_data *ptr;
42     shm_unlink("shm1");
43     if((fd = shm_open("shm1", O_RDWR | O_CREAT | O_EXCL, (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH))) == -1)
44     {
45         perror("shm_open error");
46         exit(-1);
47     }
48     ftruncate(fd,sizeof( struct shm_data));      //格式化;
49     if((ptr = mmap(NULL,sizeof(struct shm_data), PROT_READ | PROT_WRITE,MAP_SHARED,fd,0)) == MAP_FAILED)
50     {
51         perror("mmap error");
52         exit(-1);
53     }
54     close(fd);
55 
56 
57     sem_t* a;
58     int er=0;
59     sem_unlink("sem_test"); //若内存中已经存在名为“sem_test”的信号量,则将其删除;
60     // 创建一个名为"sem_test"的信号量,并将其值初始化为1,a为sem_t型指针,指向该信号量;
61     a=sem_open("sem_test", O_CREAT ,(S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH), 1);
62     if(NULL==a)
63     {
64         printf("sem_open failure!\n");
65     }
66     //将该信号量赋为1,且为进程间共享型信号量;
67     sem_init(a,1,1);
68 
69     //获取当前信号量的值;
70     sem_getvalue(a, &er);
71     while(1)
72     {
73          if(er==0)
74          {
75              sem_post(a);
76          }
77          setTimer(1,0);
78          sem_getvalue(a, &er);
79         ptr->data_int++;
80         ptr->data_double = 3.0 * ptr->data_int ;
81 
82     }
83     return 0;
84 }

2、进程b:

 1 /*
 2  ============================================================================
 3  Name        : test.c
 4  Author      : liuguanghui
 5  Version     :
 6  Copyright   : Your copyright notice
 7  Description : Hello World in C, Ansi-style
 8  ============================================================================
 9  */
10 
11 #include <stdio.h>
12 #include <fcntl.h>
13 #include <stdlib.h>
14 #include <unistd.h>
15 #include <sys/mman.h>
16 #include <sys/stat.h>
17 #include <sys/types.h>
18 #include <errno.h>
19 #include <semaphore.h>
20 #include <pthread.h>
21 
22 struct shm_data
23 {
24     int data_int;
25     double data_double;
26 };
27 int main()
28 {
29     int  fd;
30     struct shm_data *ptr;
31 
32     if((fd = shm_open("shm1", O_RDWR, (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH))) == -1)
33     {
34         perror("shm_open error");
35         exit(-1);
36     }
37     if((ptr = mmap(NULL,sizeof(struct shm_data), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0)) == MAP_FAILED)
38     {
39         perror("mmap error");
40         exit(-1);
41     }
42     close(fd);
43 
44     sem_t* x;
45     //sem_open()函数第2个参数为0,表示打开已存在的名为"sem_test"的信号量;
46     x=sem_open("sem_test" , 0, (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH), 1);
47     if(NULL==x)
48     {
49         printf("sem_open failure!\n");
50     }
51     int i=0;
52 
53     while(1)
54     {
55       sem_wait(x);         //当进程 a 完成sem_post动作时,该进程开始继续执行;
56       printf("i = %d\n",i++);
57       printf("%d,  %f\n", ptr->data_int,ptr->data_double);
58     }
59     return 0;
60 }

 

posted on 2018-02-28 20:11  忠诚的卫士  阅读(236)  评论(0编辑  收藏  举报

导航