Synchronization between processes

From Nokia Developer Wiki
Jump to: navigation, search
Article Metadata
Created: spark55 (20 Sep 2007)
Last edited: hamishwillee (27 Jul 2012)

One way to provide synchronization between processes is using semaphore. Here, we should use semaphore to communicate the state of the processes.

In Category:Symbian C++, we achieve this using RSemaphore, RFastLock, RMutex, RCriticalSection, etc.

In Open C, the steps involved in using semaphore to synchronize are as follows:

  • Create a semaphore using semget.
  • When a process wants to synchronize with some other process, the first process can create the semaphore and set the semval with some condition.
  • Once the other process has finished its task, satisfy the condition for which the other process is waiting for.
  • Delete the created semaphore.
#include <sys/sem.h>
#define SEM_KEY 1000
int main()
int semid = semget(SEM_KEY, 1, IPC_CREAT);
int ret = semctl(semid, 0, SETVAL, 1);
struct sembuf st = {0, 0, 0};
ret = semop(semid, &st, 1); // process -1 is blocked over here.
ret = semctl(semid, 1, IPC_RMID);
return 0;

To communicate between the threads, we can use pthread_mutex for synchronization.

Note:There can be many ways of achieving this functionality;So, code snippets of other ways to do this are welcome.


This page was last modified on 27 July 2012, at 07:16.
78 page views in the last 30 days.