You are on page 1of 11

Chapter 14

IPC: Shared Memory


Shared memory is a special range of addresses allocated for one process
that appears in address space of that process.
Once created by a process, other processes can then attach the same
shared memory segment into their own address space.
All processes can then access the shared memory.
If one process writes to the shared memory, the changes immediately
become visible to other processes that are attached to the same shared
memory.
Shared memory DOES NOT provide any synchronization facilities.
Permissions on shared memory are similar to the permissions on files.

shmget()
You create shared memory using the shmget function:
int shmget(key_t key, size_t size, int shmflg);
Notice that key acts very much like a filename. They are used to identify
a shared memory segment.
Similarly, the identifier returned by shmget() is very much like the FILE
* file stream returned by fopen(), in that, it is a value, used by the
process to access the shared file.
This use of a key and identifiers is common to all of the (Sys V style)
IPC facilities.

BUT each IPC facility (Semaphores, Shared Mem, Msg Queues) must
use independent keys and identifiers.
The third parameter, shmflg, consists of nine permission flags that are
used in the same way as the mode flags for creating files.
Simply creating a shared memory segment is NOT ENOUGH;
To access the shared memory, One process may create it and others
must attach to it. This is done with shmat() function:
void *shmat(int shm_id, const void *shm_addr,
int shmflg);
If successful, shmat() returns a pointer to the first byte of shared
memory. On failure 1 is returned.
The third parameter, shmflg, is a set of bitwise flags. One possible value
is SHM_RDONLY, which attaches shared memory as read-only.

int shmdt(const void *shm_addr);


After it has been used, the shmdt() function can be used to detach the
shared memory. As input, shmdt() takes the address returned by
shmat().

#include <sys/shm.h>
SHARED MEMORY
int shmget(key_t key, size_t size, int shmflg);
void *shmat(int shm_id, const void *shm_addr,
int shmflg);
int shmdt(const void *shm_addr);
int shmctl(int shm_id, int cmd,
struct shmid_ds *buf);

int shmctl(int shm_id, int cmd, struct shmid_ds


*buf);
The first parameter, shm_id, is the identifier returned from shmget.
The second parameter, command, is the action to take. It can take
three values:
Command

Description

IPC_STAT

Fills up the fields in the shmid_ds structure to reflect


the values associated with the shared memory.

IPC_SET

Sets the attributes of the shared memory to those


provided in the third argument if the process has
permission to do so.

IPC_RMID

Deletes the shared memory segment.The third


parameter, buf, is a pointer to structure containing the
modes and permissions for the shared memory.

On success, shmctl() returns 0; on failure, 1.

The buf argument is a pointer to a shmid_ds structure, defined in


<sys/shm.h> as follows:
A lot of info about a shared memory is stored in fields of this structure:
struct shmid_ds {
struct ipc_perm shm_perm;
size_t

/* Ownership and permissions */

shm_segsz;

/* Size of segment(bytes) */

time_t

shm_atime;

/* Last attach time */

time_t

shm_dtime;

/* Last detach time */

time_t

shm_ctime;

/* Last change time */

pid_t

shm_cpid;

/* PID of creator */

pid_t

shm_lpid;

shmatt_t

/* PID of last shmat(2)/shmdt(2)*/

shm_nattch;

/* No. of current attaches */

...
};

/* shm_com.h */
#define TEXT_SZ 2048
struct shared_use_st {
int written;
char some_text[TEXT_SZ];
};

/* shm1.c : A Consumer */
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include "shm_com.h"
int main()
{
int running = 1;
void *shared_memory = (void *)0;
struct shared_use_st *shared_stuff;
int shmid;
srand((unsigned int)getpid());
shmid = shmget((key_t)1234,
sizeof(struct shared_use_st),
0666 | IPC_CREAT);
if (shmid == -1) {
fprintf(stderr, "shmget failed\n");
exit(EXIT_FAILURE);
}
/* We now make the shared memory accessible to the
program. */
shared_memory = shmat(shmid, (void *)0, 0);
if (shared_memory == -1) {
fprintf(stderr, "shmat failed\n");
exit(EXIT_FAILURE);
}
printf("Memory attached at %X\n", shared_memory);
shared_stuff = (struct shared_use_st *)shared_memory;
shared_stuff -> written = 0;

while(running) {
if (shared_stuff->written) {
printf("You wrote: %s",
shared_stuff->some_text);
sleep( rand() % 4 );
shared_stuff->written = 0;
if (strncmp(shared_stuff->some_text, "end",
3) == 0)
{
running = 0;
}
}
}
if (shmdt(shared_memory) == -1) {
fprintf(stderr, "shmdt failed\n");
exit(EXIT_FAILURE);
}
if (shmctl(shmid, IPC_RMID, 0) == -1) {
fprintf(stderr, "shmctl(IPC_RMID) failed\n");
exit(EXIT_FAILURE);
}
exit(EXIT_SUCCESS);
}

/* shm2.c: The producer program; allows us to enter


data for consumers. It's very similar to shm1.c */
#include "shm_com.h"
int main()
{
int running = 1;
void *shared_memory = (void *)0;
struct shared_use_st *shared_stuff;
char buffer[BUFSIZ];
int shmid;
shmid = shmget((key_t)1234,
sizeof(struct shared_use_st),
0666 | IPC_CREAT);
if (shmid == -1) {
fprintf(stderr, "shmget failed\n");
exit(EXIT_FAILURE);
}
shared_memory = shmat(shmid, (void *)0, 0);
if (shared_memory == -1) {
fprintf(stderr, "shmat failed\n");
exit(EXIT_FAILURE);
}
printf("Memory attached at %X\n", shared_memory);
shared_stuff = (struct shared_use_st *) shared_memory;
while(running) {
while(shared_stuff->written == 1) {
sleep(1);
printf("waiting for client...\n");
}
printf("Enter some text: ");
fgets(buffer, BUFSIZ, stdin);
strncpy(shared_stuff->some_text, buffer,
TEXT_SZ);

shared_stuff->written = 1;
if (strncmp(buffer, "end", 3) == 0) {
running = 0;
}
}/* End While */
if (shmdt(shared_memory) == -1) {
fprintf(stderr, "shmdt failed\n");
exit(EXIT_FAILURE);
}
exit(EXIT_SUCCESS);
}

$ ./shm1 &
[1] 294
Memory attached at 40017000
$ ./shm2
Memory attached at 40017000
Enter some text: hello
You wrote: hello
waiting for client...
waiting for client...
Enter some text: Linux!
You wrote: Linux!
waiting for client...
waiting for client...
waiting for client...
Enter some text: end
You wrote: end
$

<<<<<-

[C]
[P] consumer has not freed resource.
[P] consumer has not freed resource.
[P] consumer HAS freed resource now!
[C]

You might also like