-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path18. IPC using Shared memory.txt
56 lines (54 loc) · 1.15 KB
/
18. IPC using Shared memory.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
//Shared Memory – Sever Program
#include <stdlib.h>
#include <sys/shm.h>
#include <stdio.h>
main()
{
char c;
int shmid;
char *shm, *s;
if ((shmid = shmget((key_t)2525, 27, IPC_CREAT | 0666)) < 0)
{
printf("Error");
exit(1);
}
if ((shm = shmat(shmid, NULL, 0)) == NULL)
{
printf("Error");
exit(0);
}
s=shm;
//Server writes a to z in the shared memory segment
for(c='a';c<='z';c++)
*s++=c;
*s='\0';
//Server waits till client reads and changes first letter to ‘*’
while(*shm!='*')
sleep(1);
}
-------------------------------------------------------------------------
//Shared Memory – Client Program
#include<stdio.h>
#include<stdlib.h>
#include<sys/shm.h>
int main()
{
int shmid;
char *shm,*s;
if((shmid=shmget((key_t)2525,27,0666))<0)
{
printf("error");
exit(0);
}
if((shm=shmat(shmid,NULL,0))==NULL)
{
printf("Error");
exit(0);
}
//Client reads the shared memory
for(s=shm;*s!='\0';s++)
putchar(*s);
putchar('\n');
//Client changes the first letter to ‘*’
*shm='*';
}