-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathshm.c
More file actions
56 lines (51 loc) · 1.04 KB
/
Copy pathshm.c
File metadata and controls
56 lines (51 loc) · 1.04 KB
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
#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
int main(int argc,char *argv[])
{
int shmid;
key_t key=100;
int count=0;
char *shm;
int pid;
while(1){
int size = 9999;
if((shmid = shmget(key,size,IPC_CREAT|0777))<0){
perror("shmget");
exit(1);
}
shm = shmat(shmid,NULL,0);
strcpy(shm,"11111");
printf("shm=[%s]\n",shm);
if((pid=fork())>0){
strcpy(shm,"22222");
printf("[parent]shm=[%s]\n",shm);
sleep(2);
printf("[parent]shm=[%s]\n",shm);
}else if(pid==0){
sleep(1);
printf("[child]shm=[%s]\n",shm);
strcpy(shm,"33333");
}else{
perror("fork:");
exit(1);
}
exit(1);
#if 0
strcpy(shm,"11111");
printf("shm=[%s]\n",shm);
shmctl(shmid,IPC_RMID,NULL);
strcpy(shm,"22222");
printf("shm=[%s]\n",shm);
shmdt(shm);
strcpy(shm,"33333");
printf("shm=[%s]\n",shm);
#endif
printf("[%d] key=[%d],shmid=[%d]\n",count,key,shmid);
count++;
key++;
exit(1);
}
return 0;
}