c - How to synchronise two processes using a semaphore -


i have been asked synchronize 2 processes share integer in shared memory. synchronization done semaphore.

the first process start incrementing integer 2 in steps of 2 10, when should block because integer 0 mod 5, permitting second process continue incrementing same integer 3 time, , stop when integer 0 mod 5; loop way. until 100

i wrote code , unsure whether write v(s) , p(s) successfully.

first process

#define keys 100 #define key 200  main() {     int s, idm, *n = 0;      idm = shmget(key, sizeof(int), ipc - creat | 0666);     creat - sem(keys);     init - sem(idm, 0, 0);     n = (int *)shmat(idm, 0, 0);     while (*n < 100)     {         *n += 2;         printf(% d, *n);         if (*n % 5 == 0)         {             v(s);             p(s);         }         shmdt(n);     }   

second process

#define keys 100 #define key 200  main() {     int s, idm, *n = 0;     idm = shmget(key, sizeof(int), ipc - excl | 0666);     creat - sem(keys);     init - sem(idm, 0, 0);     n = (int *)shmat(idm, 0, 0);     p(s);     while (*n < 100)     {         *n += 3;         printf(% d, *n);         if (*n % 5 == 0)         {             v(s);             p(s);         }         shmdt(n);         smctl(idm, ipc - rmid, 0);     } 


Comments

Popular posts from this blog

facebook - android ACTION_SEND to share with specific application only -

python - Creating a new virtualenv gives a permissions error -

javascript - cocos2d-js draw circle not instantly -