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 we need to 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


©Nokia 2007

Back to top


This material, including documentation and any related computer programs, is protected by copyright controlled by Nokia. All rights are reserved. Copying, including reproducing, storing, adapting or translating, any or all of this material requires the prior written consent of Nokia. This material also contains confidential information, which may not be disclosed to others without the prior written consent of Nokia.

Nokia is a registered trademark of Nokia Corporation. S60 and logo is a trademark of Nokia Corporation. Java and all Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. Other company and product names mentioned herein may be trademarks or tradenames of their respective owners.