23
Shared Memory & Semaphore By Venkata Durga Prasad. B Email: [email protected]

Shared memory and semaphore? And how to use them? An explanation about those system calls

Embed Size (px)

DESCRIPTION

This presentation will give what is shared memory, how to create using system calls, when multiple threads are accessing the same shared memory, how to synchronize them with a semaphore.

Citation preview

Page 1: Shared memory and semaphore? And how to use them? An explanation about those system calls

Shared Memory &

Semaphore

By

Venkata Durga Prasad. BEmail: [email protected]

Page 2: Shared memory and semaphore? And how to use them? An explanation about those system calls

Shared Memory It allows two unrelated processes to access the same logical

memory.

Shared memory is a special range of addresses that is created by IPC for one process and appears in the address space of that process.

Other processes can then “attach” the same shared memory segment into their own address space.

Page 3: Shared memory and semaphore? And how to use them? An explanation about those system calls

Creation:

shmget() is used to obtain access to a shared memory segment.

int shmget(key_t key, size_t size, int shmflg);

The key argument is a access value associated with the semaphore ID.

The size argument is the size in bytes of the requested shared memory.

The shmflg argument specifies the initial access permissions and creation control flags.

The flag uses IPC_CREAT | 0666 to set permission.

Page 4: Shared memory and semaphore? And how to use them? An explanation about those system calls

Creation:(Cont'd..)

On Success it returns the shared memory segment ID. On failure, it returns -1

This call is also used to get the ID of an existing shared segment.

Page 5: Shared memory and semaphore? And how to use them? An explanation about those system calls

Attachment:

To enable access to the shared memory, you must attach it to the address space of a process.

void *shmat(int shm_id, const void *shm_addr, int shmflg);

shm_id is the shared memory ID returned by shmget().

The second parameter, shm_addr, is the address at which the shared memory is to be attached to the current process.

The third parameter, shmflg, is a bitwise flag.

SHM_RDONLY, which makes the attached memory read-only.

Page 6: Shared memory and semaphore? And how to use them? An explanation about those system calls

Attachment:(cont'd...)

On success shmat() returns a pointer to the first byte of shared memory.

On failure, it returns -1.

Page 7: Shared memory and semaphore? And how to use them? An explanation about those system calls

Detachment:

The shmdt function detaches the shared memory from the current process. It takes a pointer to the address returned by shmat.

Int shmdt(shm_ptr);

On success shmdt() returns 0,

On error -1 is returned.

Page 8: Shared memory and semaphore? And how to use them? An explanation about those system calls

Controlling:

shmctl() performs the control operation specified by cmd on the shared memory segment whose identifier is given in shmid.

int shmctl(int shmid, 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.

Page 9: Shared memory and semaphore? And how to use them? An explanation about those system calls

Controlling:(cont'd...)

IPC_STAT Sets the data in the shmid_ds structure to reflect the values associated with the shared memory.

IPC_SET Sets the values associated with the shared memory to those provided in the shmid_ds data structure, if the process has permission to do so.

IPC_RMID Deletes the shared memory segment.

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

On success, it returns 0, on failure, –1.

Page 10: Shared memory and semaphore? And how to use them? An explanation about those system calls

NOTE:

If you did not remove your shared memory segments they will be in the system forever. This will degrade the system performance.

Use the ipcs command to check if you have shared memory segments left in the system.

Use the ipcrm command to remove your shared memory segments.

Page 11: Shared memory and semaphore? And how to use them? An explanation about those system calls

Semaphore

A semaphore is a special type of variable that can be incremented or decremented,

But crucial access to the variable is guaranteed to be atomic, even in a multi- threaded program.

Page 12: Shared memory and semaphore? And how to use them? An explanation about those system calls

Types of semaphore:

There are two types of semaphores. They are

Unnamed semaphore

Named semaphore

Page 13: Shared memory and semaphore? And how to use them? An explanation about those system calls

1.unnamed semaphore:

The semaphore is placed in a region of memory that is shared between multiple threads.

This semaphore also termed as binary semaphore.

a binary semaphore that takes only values 0 or 1.

Binary semaphores are used to protect a piece of code so that only one thread of execution can run it at any one time.

Page 14: Shared memory and semaphore? And how to use them? An explanation about those system calls

Initialization:

sem_init() initializes the unnamed semaphore at the address pointed to by sem. The value argument specifies the initial value for the semaphore.

int sem_init(sem_t *sem, int pshared, unsigned int value);

The pshared parameter controls the type of semaphore.

If the value of pshared is 0, the semaphore is local to the current process.

Otherwise, the semaphore may be shared between processes.

Page 15: Shared memory and semaphore? And how to use them? An explanation about those system calls

Controlling:

int sem_wait(sem_t *sem);

sem_wait() decrements (locks) the semaphore pointed to by sem.

If the semaphore's value is greater than zero, then the decrement proceeds, and the function returns, immediately.

If the semaphore currently has the value zero, then the call blocks until either it becomes possible to perform the decrement.

On success it returns 0, and on failure -1.

Page 16: Shared memory and semaphore? And how to use them? An explanation about those system calls

Controlling:(cont'd...)

int sem_post(sem_t * sem);

sem_post() increments (unlocks) the semaphore pointed to by sem.

If the semaphore's value consequently becomes greater than zero,

Then another process or thread blocked in a sem_wait call will be woken up and proceed to lock the semaphore.

On success it returns 0, and on failure -1.

Page 17: Shared memory and semaphore? And how to use them? An explanation about those system calls

Destroy( ):

int sem_destroy(sem_t *sem);

sem_destroy() destroys the unnamed semaphore at the address pointed to by sem.

Only a semaphore that has been initialized by sem_init should be destroyed using sem_destroy().

Destroying a semaphore that other processes or threads are currently blocked on (in sem_wait) produces undefined behavior.

sem_destroy() returns 0 on success, on error, -1 is returned.

Page 18: Shared memory and semaphore? And how to use them? An explanation about those system calls

2.Named semaphore:

A named semaphore is identified by a name of the form /some-name, consisting of an initial slash, followed by one or more characters, none of which are slashes.

Two processes can operate on the same named semaphore by passing the same name to sem_open.

Page 19: Shared memory and semaphore? And how to use them? An explanation about those system calls

Creation:

sem_t *sem_open(const char *name, int oflag, mode_t mode, unsigned int value);

sem_open() creates a new POSIX semaphore or opens an existing semaphore.

The semaphore is identified by name.

The oflag argument specifies flags that control the operation of the call.

If O_CREAT is specified in oflag, then the semaphore is created if it does not already exist.

Page 20: Shared memory and semaphore? And how to use them? An explanation about those system calls

Creation:(cont'd...)

The mode argument specifies the permissions to be placed on the new semaphore.

The value argument specifies the initial value for the new semaphore.

If O_CREAT is specified, and a semaphore with the given name already exists, then mode and value are ignored.

On success, sem_open() returns the address of the new semaphore.

On error, sem_open() returns SEM_FAILED.

Page 21: Shared memory and semaphore? And how to use them? An explanation about those system calls

Destroy:

int sem_unlink(const char *name);

sem_unlink() removes the named semaphore referred to by name.

On success sem_unlink() returns 0.

On error, -1 is returned.

Page 22: Shared memory and semaphore? And how to use them? An explanation about those system calls

NOTE:

Programs using the POSIX semaphores API must be compiled with cc -lrt to link against the real-time library.

Otherwise compile with cc -pthread.

Page 23: Shared memory and semaphore? And how to use them? An explanation about those system calls

Anyqueries?

???