xml地图|网站地图|网站标签 [设为首页] [加入收藏]

智能硬件

当前位置:美高梅游戏网站 > 智能硬件 > 进程间共享内存(信号量实现同步)

进程间共享内存(信号量实现同步)

来源:http://www.gd-chuangmei.com 作者:美高梅游戏网站 时间:2019-09-03 16:12

 图片 1

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <unistd.h>
 4 #include <sys/types.h>
 5 #include <sys/shm.h>
 6 #include <sys/ipc.h>
 7 #include <sys/sem.h>
 8 #include <string.h>
 9 int main(int argc, char* argv[])
10 {
11   int running = 1;
12   int shid;
13   int semid;
14   int value;
15   int read_num;
16   FILE *stream;
17   char* sharem=NULL;
18   struct sembuf sem_b;
19   sem_b.sem_num = 0;
20   sem_b.sem_flg = SEM_UNDO;
21                                                            
22   if((semid = semget((key_t)123456,1,0666|IPC_CREAT))==-1)
23     {
24       perror("semget");
25       exit(EXIT_FAILURE);
26     }
27   if(semctl(semid,0,SETVAL,0)==-1)
28     {
29       printf("sem init error");
30       if(semctl(semid,0,IPC_RMID,0)!=0)
31     {
32       perror("semctl");
33       exit(EXIT_FAILURE);
34     }
35       exit(EXIT_FAILURE);
36     }
37   shid = shmget((key_t)654321,(size_t)30,0600|IPC_CREAT);
38   if(shid == -1)
39     {
40       perror("shmget");
41       exit(EXIT_FAILURE);
42     }
43   sharem = shmat(shid,NULL,0);
44   if(sharem==NULL)
45     {
46       perror("shmat");
47       exit(EXIT_FAILURE);
48     }
49    stream = fopen("data.in","r");
50   while(running)
51     {
52       // printf("write data operaten");
53       // while((semctl(semid,0,GETVAL))==1);
54       if((value=semctl(semid,0,GETVAL))==0)
55     {
56                                                                
57     // if((read_num = fread(sharem,20,1,stream))==0)
58     //          running--;
59           if(fscanf(stream,"%s",sharem)!=EOF)
60         {
61                printf("%sn",sharem);
62            sem_b.sem_op = 1;
63            if(semop(semid,&sem_b,1)==-1)
64              {
65                 printf("errorn");
66                 exit(EXIT_FAILURE);
67                  }
68         }
69           else   break;
70     }
71     }
72   printf("send completedn");
73   fclose(stream);
74   shmdt(sharem);
75   return 0;
76 }

sem1.c

以上是sender进程。

 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.nn");
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.nn",*(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 }
 1 /*receiver.c*/
 2 #include <stdio.h>
 3 #include <stdlib.h>
 4 #include <unistd.h>
 5 #include <sys/types.h>
 6 #include <sys/shm.h>
 7 #include <sys/ipc.h>
 8 #include <sys/sem.h>
 9 #include <string.h>
10 int main(int argc, char *argv[])
11 {
12   int running = 1;
13   char *shm_p = NULL;
14   int shmid;
15   int value;
16   int semid;
17   FILE *stream;
18                                                     
19   struct sembuf sem_b;
20   sem_b.sem_num=0;
21   sem_b.sem_flg=SEM_UNDO;
22                                                     
23   semid=semget((key_t)123456,1,0666|IPC_CREAT);
24   shmid = shmget((key_t)654321,(size_t)30,0600|IPC_CREAT);
25   shm_p = shmat(shmid,NULL,0);
26   printf("read data operaten");
27   //stream = fopen("data.out","w+");
28   char buffer[50];
29   while(running)
30     {
31       while((value=semctl(semid,0,GETVAL))==1)
32     {
33       stream = fopen("data.out","a+");
34       sem_b.sem_op = -1;
35       if(semop(semid,&sem_b,1)==-1)
36         {
37           printf("errorn");
38           exit(EXIT_FAILURE);
39         }
40       strcpy(buffer,shm_p);
41       printf("%sn",buffer);
42       //fwrite(shm_p,20,1,stream);
43       fprintf(stream,"%sn",buffer);
44       fclose(stream);
45     }
46       if(strcmp(shm_p,"end")==0)
47     running--;
48     }
49   shmdt(shm_p);
50   if(shmctl(shmid,IPC_RMID,0)!=0)
51     {
52       perror("shmctl");
53       exit(EXIT_FAILURE);
54     }
55   if(semctl(semid,0,IPC_RMID,0)!=0)
56     {
57       perror("semctl");
58       exit(EXIT_FAILURE);
59     }
60   return 0;
61 }

sem2.c

以上是receiver进程。

本文由美高梅游戏网站发布于智能硬件,转载请注明出处:进程间共享内存(信号量实现同步)

关键词:

上一篇:Ubuntu 11.04 安装 LNMP环境

下一篇:没有了