NarayanLal Menariya August30,2018
Synchronization of shared memory using Semaphore
Using semaphores synchronization can beprovided between two or moreprocess. Here
i am going to writea codewhich will clear that how a semaphorecan be used with
shared memory to providesynchronization between writing processand reading
process.
The following codewrites structurein shared memory and read it back. Oncethe writing
process will writefirst messageinto shared memory receiver process will read it
immediately and will wait for second messageto arriveinto shared memory. Receiver
process will be in sleep statetill thenew data will arrivein shared memory. Just like
producer-consumer problem.
This code is written by meonly and not copied from anywhere.
Code : writeIn_sharedMemory_UsingSemaphore.c
1. /*
2. Narayan Lal Menariya:August30,2018
3. * */
4. #include<sys/sem.h>
5. #include<stdio.h>
6. #include<sys/shm.h>
7. #include<sys/ipc.h>
8. #include<string.h>
9. #include "file.h"
10.
11. int counter;
12.
13.
14. struct student
15. {
16. intid;
17. char name[20];
18. }stdobj;
19.
20. int main()
21. {
22. intret;
23. char choice;
24. char msg[20];
25. struct student*ptr;
26.
27. //declaringkeyforsemaphore
28. key_tsemKey =1;
29. //creatingkeyforsharedmemory
30. key_tshmKey =100;
31.
32. //requestingkernel toreturnsemaphore memoryid
33. intsemid = semget(semKey,1,IPC_CREAT|0666);//semkey,no.of sem, flg|permission
34.
35. if(semid==-1)
36. perror("semget");
37. else
38. {
39. //ACQUIRINGSEMAPHORE:semval =1 meanssemaphore isavailable
40. ret = semctl(semid,0, SETVAL, 1);//semid,semaphore number,setvalue =1
41. if(ret== -1)
42. perror("smctl");
43. else
44. {
45. //creatingsemaphore structure
46. struct sembuf x = {0,-1,0}; //semaphore number,decrementoperation,
IPC_NOWAIT:processshouldrelease semexplicitly
47.
48. //performlockingoperationon
49. ret= semop(semid,&x,1);
50. if(ret== -1) perror("semop");
51. else {
printf("semaphorelocked n");
52.
53. //requestingkernel toallocate sharedmemoryandreturnshmid
54. intshmid= shmget(shmKey,sizeof(stdobj), IPC_CREAT|0666);
55.
56. //attachingtosharedmemory
57. ptr = (struct student*)shmat(shmid,NULL,0);
58.
59. if(ptr)
60. {
61. printf("Attachedsuccessfullyn");
62. printf("Doyouwantto write insharedmemory1. y:yes,2.n:non");
63. while(1)
64. {
65. scanf("%c",&choice);
66. if(choice =='y')
67. {
68. //ptr = &stdobj;
69. printf("enterstudentid:n");
70. scanf("%d",&stdobj.id);
71. printf("enterstudentname :n");
72. scanf("%s",&stdobj.name);
73.
74. printf("name :%sn",stdobj.name);
75. printf("id:%dn",stdobj.id);
76.
77. (*ptr).id= stdobj.id;
78. strcpy((*ptr).name,stdobj.name);
79.
80.
81. printf("nDatawrittensuccessfullyn");
82. ptr++;
83. printf("writechoice y/n:");
84. }
85. if(choice =='n')
86. {
87.
88. break;
89. }
90.
91. }
92.
93. //deattachingfrommemory
94. shmdt(ptr);
95. printf("deattachedsuccessfullyn");
96. //releasingsemaphore
97. semctl(semid,0,SETVAL,1);
98.
99. }
100. else
101. perror("shmat");
102. }
103. }
104. }
105. return 0;
106. }
Code :Read_UsingSemaphore.c
1. /*
2. Narayan Lal Menariya:August30,2018
3. * */
4. #include<sys/sem.h>
5. #include<stdio.h>
6. #include<sys/shm.h>
7. #include<sys/ipc.h>
8. #include<string.h>
9.
10.
11. struct student
12. {
13. intid;
14. char name[20];
15. }stdobj;
16.
17. int main()
18. {
19. intret;
20. char choice;
21. char msg[20];
22. struct student*ptr;
23.
24. //declaringkeyforsemaphore
25. key_tsemKey =1;
26. //creatingkeyforsharedmemory
27. key_tshmKey =100;
28.
29. //requestingkernel to returnsemaphore memoryid
30. intsemid = semget(semKey,1,IPC_CREAT|0666);//semkey,no.of sem, flg|permission
31.
32. if(semid==-1)
33. perror("semget");
34. else
35. {
36. struct sembuf x = {0,-1,0}; //semaphore number,decrementoperation,IPC_NOWAIT
37. //performlockingoperationon
38. ret= semop(semid,&x,1);
39. if(ret== -1) perror("semop");
40. else {
printf("semaphorelocked n");
41.
42. //requestingkernel toallocate sharedmemoryandreturnshmid
43. intshmid= shmget(shmKey,sizeof(stdobj), IPC_CREAT|0666);
44.
45. //attachingtosharedmemory
46. ptr = (struct student*)shmat(shmid,NULL,0);
47.
48. if(ptr)
49. {
50. printf("Attachedsuccessfullyn");
51. while(1)
52. {
53. while((*ptr).id!=0)
54. {
55.
56. printf("studentid:%dn",(*ptr).id);
57. printf("studentname :%sn",(*ptr).name);
58.
59. printf("Datareadsuccessfullyn");
60. ptr++;
61. }
62. }
63. //deattachingfrommemory
64. shmdt(ptr);
65. printf("deattachedsuccessfullyn");
66. //release explicitly
67. semctl(semid,0,SETVAL,1);
68. }
69. else
70. perror("shmat");
71. }
72. }
73. return 0;
74. }
If you haveany doubt than feel free to ask.
Thanks.

Synchronization of shared memory using semaphores

  • 1.
    NarayanLal Menariya August30,2018 Synchronizationof shared memory using Semaphore Using semaphores synchronization can beprovided between two or moreprocess. Here i am going to writea codewhich will clear that how a semaphorecan be used with shared memory to providesynchronization between writing processand reading process. The following codewrites structurein shared memory and read it back. Oncethe writing process will writefirst messageinto shared memory receiver process will read it immediately and will wait for second messageto arriveinto shared memory. Receiver process will be in sleep statetill thenew data will arrivein shared memory. Just like producer-consumer problem. This code is written by meonly and not copied from anywhere. Code : writeIn_sharedMemory_UsingSemaphore.c 1. /* 2. Narayan Lal Menariya:August30,2018 3. * */ 4. #include<sys/sem.h> 5. #include<stdio.h> 6. #include<sys/shm.h> 7. #include<sys/ipc.h> 8. #include<string.h> 9. #include "file.h" 10. 11. int counter; 12. 13. 14. struct student 15. { 16. intid; 17. char name[20]; 18. }stdobj; 19. 20. int main() 21. { 22. intret; 23. char choice; 24. char msg[20]; 25. struct student*ptr; 26. 27. //declaringkeyforsemaphore 28. key_tsemKey =1; 29. //creatingkeyforsharedmemory 30. key_tshmKey =100;
  • 2.
    31. 32. //requestingkernel toreturnsemaphorememoryid 33. intsemid = semget(semKey,1,IPC_CREAT|0666);//semkey,no.of sem, flg|permission 34. 35. if(semid==-1) 36. perror("semget"); 37. else 38. { 39. //ACQUIRINGSEMAPHORE:semval =1 meanssemaphore isavailable 40. ret = semctl(semid,0, SETVAL, 1);//semid,semaphore number,setvalue =1 41. if(ret== -1) 42. perror("smctl"); 43. else 44. { 45. //creatingsemaphore structure 46. struct sembuf x = {0,-1,0}; //semaphore number,decrementoperation, IPC_NOWAIT:processshouldrelease semexplicitly 47. 48. //performlockingoperationon 49. ret= semop(semid,&x,1); 50. if(ret== -1) perror("semop"); 51. else { printf("semaphorelocked n"); 52. 53. //requestingkernel toallocate sharedmemoryandreturnshmid 54. intshmid= shmget(shmKey,sizeof(stdobj), IPC_CREAT|0666); 55. 56. //attachingtosharedmemory 57. ptr = (struct student*)shmat(shmid,NULL,0); 58. 59. if(ptr) 60. { 61. printf("Attachedsuccessfullyn"); 62. printf("Doyouwantto write insharedmemory1. y:yes,2.n:non"); 63. while(1) 64. { 65. scanf("%c",&choice); 66. if(choice =='y') 67. { 68. //ptr = &stdobj; 69. printf("enterstudentid:n"); 70. scanf("%d",&stdobj.id); 71. printf("enterstudentname :n"); 72. scanf("%s",&stdobj.name); 73. 74. printf("name :%sn",stdobj.name); 75. printf("id:%dn",stdobj.id); 76. 77. (*ptr).id= stdobj.id; 78. strcpy((*ptr).name,stdobj.name); 79.
  • 3.
    80. 81. printf("nDatawrittensuccessfullyn"); 82. ptr++; 83.printf("writechoice y/n:"); 84. } 85. if(choice =='n') 86. { 87. 88. break; 89. } 90. 91. } 92. 93. //deattachingfrommemory 94. shmdt(ptr); 95. printf("deattachedsuccessfullyn"); 96. //releasingsemaphore 97. semctl(semid,0,SETVAL,1); 98. 99. } 100. else 101. perror("shmat"); 102. } 103. } 104. } 105. return 0; 106. } Code :Read_UsingSemaphore.c 1. /* 2. Narayan Lal Menariya:August30,2018 3. * */ 4. #include<sys/sem.h> 5. #include<stdio.h> 6. #include<sys/shm.h> 7. #include<sys/ipc.h> 8. #include<string.h> 9. 10. 11. struct student 12. { 13. intid; 14. char name[20]; 15. }stdobj; 16. 17. int main() 18. { 19. intret; 20. char choice; 21. char msg[20]; 22. struct student*ptr;
  • 4.
    23. 24. //declaringkeyforsemaphore 25. key_tsemKey=1; 26. //creatingkeyforsharedmemory 27. key_tshmKey =100; 28. 29. //requestingkernel to returnsemaphore memoryid 30. intsemid = semget(semKey,1,IPC_CREAT|0666);//semkey,no.of sem, flg|permission 31. 32. if(semid==-1) 33. perror("semget"); 34. else 35. { 36. struct sembuf x = {0,-1,0}; //semaphore number,decrementoperation,IPC_NOWAIT 37. //performlockingoperationon 38. ret= semop(semid,&x,1); 39. if(ret== -1) perror("semop"); 40. else { printf("semaphorelocked n"); 41. 42. //requestingkernel toallocate sharedmemoryandreturnshmid 43. intshmid= shmget(shmKey,sizeof(stdobj), IPC_CREAT|0666); 44. 45. //attachingtosharedmemory 46. ptr = (struct student*)shmat(shmid,NULL,0); 47. 48. if(ptr) 49. { 50. printf("Attachedsuccessfullyn"); 51. while(1) 52. { 53. while((*ptr).id!=0) 54. { 55. 56. printf("studentid:%dn",(*ptr).id); 57. printf("studentname :%sn",(*ptr).name); 58. 59. printf("Datareadsuccessfullyn"); 60. ptr++; 61. } 62. } 63. //deattachingfrommemory 64. shmdt(ptr); 65. printf("deattachedsuccessfullyn"); 66. //release explicitly 67. semctl(semid,0,SETVAL,1); 68. } 69. else 70. perror("shmat"); 71. } 72. }
  • 5.
    73. return 0; 74.} If you haveany doubt than feel free to ask. Thanks.