-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathipc.c
More file actions
61 lines (56 loc) · 1.14 KB
/
Copy pathipc.c
File metadata and controls
61 lines (56 loc) · 1.14 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
57
58
59
60
61
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
#include<sys/ipc.h>
#include<sys/shm.h>
#include<sys/types.h>
#define SEGSIZE 100
int main(int argc, char*argv[])
{
int shmid;
key_t key;
char*segptr;
char buff[]="Hello,How are you?";
key=ftok(".",'s');
if((shmid=shmget(key,SEGSIZE,IPC_CREAT|IPC_EXCL|0666))==-1)
{
if((shmid=shmget(key,SEGSIZE,0))==-1)
{
perror("shmget");
exit(1);
}
}
else
{
printf("Creating a new shared memory segment\n");
printf("SHMID : %d\n",shmid);
}
system("ipcs -m");
if((segptr=(char*)shmat(shmid,0,0))==(char *)-1)
{
perror("shmat");
exit(1);
}
printf("Writing data to the shared meomry...\n");
strcpy(segptr,buff);
printf("DONE\n");
printf("Reading data from the shared meomry...\n");
printf("DATA:%s\n",segptr);
printf("DONE\n");
if(shmdt(segptr)==-1)
{
perror("shmdt");
exit(1);
}
printf("Removing shared meomry segment...\n");
if(shmctl(shmid,IPC_RMID,0)==-1)
{
printf("Can't remove shared memory segment...\n ");
}
else
{
printf("Removed successfully.\n");
}
return 0;
}