4
  1. What is difference between semaphore and a shared memory ?
  2. can I use semaphores between two different processes or it must be used only in threads of a same process ?
Waqas
  • 191
  • 3
  • 4
  • 13
  • [Semaphore](http://en.wikipedia.org/wiki/Semaphore_(programming)) [Shared Memory](http://en.wikipedia.org/wiki/Shared_memory) – Arnab Nandy Nov 16 '14 at 17:06

2 Answers2

3

Yes, you can use semaphore between two different processes

Choose a name for your semaphore

#define SNAME "/mysem"

Use sem_open with O_CREAT in the process that creates them

sem_t *sem = sem_open(SNAME, O_CREAT, 0644, 3); /* Initial value is 3. */

Open semaphores in the other processes

sem_t *sem = sem_open(SEM_NAME, 0); /* Open a preexisting semaphore. */

Reference

Community
  • 1
  • 1
Arnab Nandy
  • 6,472
  • 5
  • 44
  • 50
3

The semaphore is a system for synchronization 2 or plus process to access a shared resource.

The shared memory is a system for sharing a piece of memory between 2 o plus process, on the shared memory is possible write or read data to and from a process.

For example with a semaphore is possible to manage the access to shared memory for avoid the read o write non synchronized from 2 process.

Bye