Shared Memory 
& 
Semaphore 
By 
Venkata Durga Prasad. B 
Email: venkata.prasad@vvdntech.in
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.
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.
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.
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.
Attachment:(cont'd...) 
 On success shmat() returns a pointer to the first byte of shared 
memory. 
 On failure, it returns -1.
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.
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.
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.
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.
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.
Types of semaphore: 
There are two types of semaphores. They are 
 Unnamed semaphore 
 Named semaphore
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
Any 
queries? 
???

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

  • 1.
    Shared Memory & Semaphore By Venkata Durga Prasad. B Email: venkata.prasad@vvdntech.in
  • 2.
    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.
  • 3.
    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.
  • 4.
    Creation:(Cont'd..)  OnSuccess 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.
  • 5.
    Attachment:  Toenable 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.
  • 6.
    Attachment:(cont'd...)  Onsuccess shmat() returns a pointer to the first byte of shared memory.  On failure, it returns -1.
  • 7.
    Detachment:  Theshmdt 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.
  • 8.
    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.
  • 9.
    Controlling:(cont'd...)  IPC_STATSets 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.
  • 10.
    NOTE:  Ifyou 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.
  • 11.
    Semaphore  Asemaphore 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.
  • 12.
    Types of semaphore: There are two types of semaphores. They are  Unnamed semaphore  Named semaphore
  • 13.
    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.
  • 14.
    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.
  • 15.
    Controlling:  intsem_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.
  • 16.
    Controlling:(cont'd...)  intsem_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.
  • 17.
    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.
  • 18.
    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.
  • 19.
    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.
  • 20.
    Creation:(cont'd...)  Themode 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.
  • 21.
    Destroy:  intsem_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.
  • 22.
    NOTE:  Programsusing the POSIX semaphores API must be compiled with cc -lrt to link against the real-time library.  Otherwise compile with cc -pthread.
  • 23.