S60 Open C
Threads, Processes, IPC, and Synchronization

Threads, Processes, IPC, and Synchronization

Table of Contents

How to synchronize between processes
Example
Important note

 


How to synchronize between processes

In order to synchronize the task between processes the user must have some mechanism to communicate the state of the processes. The user can use the following pattern to achieve the same using semaphore:

  • 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 semaphore.

 


Example

Process 1:

#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;
}

Process 2:

#include <sys/sem.h>
#define SEM_KEY 1000
int main()
{
   int semid = semget(SEM_KEY, 1, IPC_CREAT);
   struct sembuf st = {0, -1, 0};
   TInt ret = semop(semid, &st, 1); // unblocking the process 1
   return 0;

 


Important note

Use pthread_mutex for synchronization if, pthread_create is used for creating threads.

Symbian C++ also provides the same type of functionality using RSemaphore, RFastLock, RMutex, RCriticalSection, and so on.

Give feedback of this section


Back to top


Copyright ©2008 Nokia Corporation. All rights reserved. This documentation can be used in the connection with this Product to help and support the user.