Threads, Processes, IPC, and Synchronization |
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:
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;
©Nokia 2007 |