A simple Implementation of Shared Memory in C
Shared Memory is a type of IPC where the two processes share same memory chunk and use it for IPC. One process writes into that memory and other reads it.
After running the Server you can see the attached Shared Memory
samir@linux unixprog> ipcs -m
------ Shared Memory Segments --------
key shmid owner perms bytes nattch status
0x0000162e 65537 vgupta80 666 27 1
After running the client the memory is freed.
------ Shared Memory Segments --------
key shmid owner perms bytes nattch status
0x0000162e 65537 vgupta80 666 27 0
?
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
//SHMServer.C
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 27
void die(char *s)
{
perror(s);
exit(1);
}
int main()
{
char c;
int shmid;
key_t key;
char *shm, *s;
key = 5678;
if ((shmid = shmget(key, MAXSIZE, IPC_CREAT | 0666)) < 0)
die("shmget");
if ((shm = shmat(shmid, NULL, 0)) == (char *) -1)
die("shmat");
/*
* * Put some things into the memory for the
* other process to read.
* */
s = shm;
for (c = 'a'; c <= 'z'; c++)
*s++ = c;
/*
* Wait until the other process
* changes the first character of our memory
* to '*', indicating that it has read what
* we put there.
*/
while (*shm != '*')
sleep(1);
exit(0);
}
?
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
//SHMClient.C
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 27
void die(char *s)
{
perror(s);
exit(1);
}
int main()
{
int shmid;
key_t key;
char *shm, *s;
key = 5678;
if ((shmid = shmget(key, MAXSIZE, 0666)) < 0)
die("shmget");
if ((shm = shmat(shmid, NULL, 0)) == (char *) -1)
die("shmat");
//Now read what the server put in the memory.
for (s = shm; *s != '\0'; s++)
putchar(*s);
putchar('\n');
/*
*Change the first character of the
*segment to '*', indicating we have read
*the segment.
*/
*shm = '*';
exit(0);
}
Shared Memory is a type of IPC where the two processes share same memory chunk and use it for IPC. One process writes into that memory and other reads it.
After running the Server you can see the attached Shared Memory
samir@linux unixprog> ipcs -m
------ Shared Memory Segments --------
key shmid owner perms bytes nattch status
0x0000162e 65537 vgupta80 666 27 1
After running the client the memory is freed.
------ Shared Memory Segments --------
key shmid owner perms bytes nattch status
0x0000162e 65537 vgupta80 666 27 0
?
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
//SHMServer.C
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 27
void die(char *s)
{
perror(s);
exit(1);
}
int main()
{
char c;
int shmid;
key_t key;
char *shm, *s;
key = 5678;
if ((shmid = shmget(key, MAXSIZE, IPC_CREAT | 0666)) < 0)
die("shmget");
if ((shm = shmat(shmid, NULL, 0)) == (char *) -1)
die("shmat");
/*
* * Put some things into the memory for the
* other process to read.
* */
s = shm;
for (c = 'a'; c <= 'z'; c++)
*s++ = c;
/*
* Wait until the other process
* changes the first character of our memory
* to '*', indicating that it has read what
* we put there.
*/
while (*shm != '*')
sleep(1);
exit(0);
}
?
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
//SHMClient.C
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 27
void die(char *s)
{
perror(s);
exit(1);
}
int main()
{
int shmid;
key_t key;
char *shm, *s;
key = 5678;
if ((shmid = shmget(key, MAXSIZE, 0666)) < 0)
die("shmget");
if ((shm = shmat(shmid, NULL, 0)) == (char *) -1)
die("shmat");
//Now read what the server put in the memory.
for (s = shm; *s != '\0'; s++)
putchar(*s);
putchar('\n');
/*
*Change the first character of the
*segment to '*', indicating we have read
*the segment.
*/
*shm = '*';
exit(0);
}
0 Comments