Linux下共享内存+信号量实现


 

sem1.c

 1 #include"unistd.h"
 2 #include"string.h"
 3 #include"stdio.h"
 4 #include"stdlib.h"
 5 #include"linux/types.h"
 6 #include"linux/sem.h"
 7 #include"linux/ipc.h"
 8 #include"linux/shm.h"
 9 #include"error.h"
10 int main()
11 {
12     key_t sem_key1=10086,sem_key2=10087,shm_key=10088;
13     int mutexid1,mutexid2;
14     int shmid=0,count=0,*addr,j=0,temp=0;
15     union semun arg1,arg2;
16     struct sembuf P,V;
17     /* define  P   V operation*/
18     P.sem_num=0;
19     P.sem_op=-1;
20     P.sem_flg=SEM_UNDO;
21     V.sem_num=0;
22     V.sem_op=1;
23     V.sem_flg=SEM_UNDO;
24     arg1.val=4;
25     arg2.val=0;
26 
27 
28     mutexid1=semget(sem_key1,1,0666|IPC_CREAT);
29     mutexid2=semget(sem_key2,1,0666|IPC_CREAT);
30     semctl(mutexid1,0,SETVAL,arg1);
31     semctl(mutexid2,0,SETVAL,arg2);
32 
33 
34     shmid=shmget(shm_key,sizeof(int)*7,IPC_CREAT|0600);
35     addr=(int *)shmat(shmid,NULL,0);//init share memory
36     *(addr)=3;
37     *(addr+1)=3;
38     *(addr+2)=0;
39     shmdt(addr);
40     printf("         TIPS: iput\"-1\" to stop input num.\n\n");
41     while(1)
42     {
43 
44 
45         /**************************************************/
46 
47         semop(mutexid1,&P,1);
48 
49 
50         shmid=shmget(shm_key,sizeof(int)*7,IPC_CREAT|0600);
51         addr=(int *)shmat(shmid,NULL,0);//init share memory
52         j=*(addr+1);
53         count=*(addr+2);
54         if(j==7)j=3;
55 
56         printf("Please input data:  ");
57         scanf("%d",&temp);
58         printf("\n");
59         *(addr+j)=temp;
60         printf("%d   wrrite sucess.\n\n",*(addr+j));
61         if(*(addr+j)==-1)
62         {
63             shmdt(addr);
64             semop(mutexid2,&V,1);
65             return 0;
66         }
67         j++;
68         count++;
69         *(addr+1)=j;
70         *(addr+2)=count;
71         shmdt(addr);
72 
73 
74         semop(mutexid2,&V,1);
75 
76     /**************************************************/
77     }
78     return 0;
79 }

sem2.c

 1 #include"unistd.h"
 2 #include"stdio.h"
 3 #include"stdlib.h"
 4 #include"linux/types.h"
 5 #include"linux/sem.h"
 6 #include"linux/ipc.h"
 7 #include"linux/shm.h"
 8 #include"error.h"
 9 int main()
10 {
11     key_t sem_key1=10086,sem_key2=10087,shm_key=10088;
12     int mutexid1,mutexid2;
13     int shmid=0,count=0,*addr,i=0;
14     union semun arg;    
15     struct sembuf P,V;
16     /* define  P   V operation*/
17     P.sem_num=0;
18     P.sem_op=-1;
19     P.sem_flg=SEM_UNDO;
20     V.sem_num=0;
21     V.sem_op=1;
22     V.sem_flg=SEM_UNDO;
23 
24 
25     mutexid1=semget(sem_key1,1,0666|IPC_CREAT);
26     mutexid2=semget(sem_key2,1,0666|IPC_CREAT);
27 
28 
29 
30 
31 
32     while(1)
33     {
34 
35 
36 
37     /***********************************************/        
38 
39     semop(mutexid2,&P,1);
40 
41     shmid=shmget(shm_key,sizeof(int)*7,IPC_CREAT|0600);
42     addr=(int *)shmat(shmid,NULL,0);//init share memory
43     i=*(addr);
44     count=*(addr+2);
45     if(i==7)i=3;
46 
47 
48     printf("Read  data....   ");
49     printf("%d\n",*(addr+i));
50     if(*(addr+i)==-1)
51     {    
52         shmdt(addr);
53         shmctl(shmid,IPC_RMID,NULL);
54         semctl(mutexid1,0,IPC_RMID,0);
55         semctl(mutexid2,0,IPC_RMID,0);
56         return 0;
57     }
58     i++;
59     count--;
60     *(addr)=i;
61     *(addr+2)=count;
62     shmdt(addr);
63     
64     semop(mutexid1,&V,1);
65 }
66 /***********************************************/        
67     return 0;
68 }


在gcc下需要注意Linux和sys两个头文件位置的选择,否则会出现错误。

相关内容

    暂无相关文章