oujia
oujia

Reputation: 31

SEM_UNDO - how does it work?

I know that SEM_UNDO flag undo operations after process terminates but I want to know how to prove it in my program.

Example:

Semaphore A (process 1) has value = 1;
Semaphore A (process 1) wait.
Semaphore A (process 2) is decrementing (WITH FLAG = SEM_UNDO).
Sempahore A (process 1) has value = 0;
Process 2 end.
semaphore a (process 1) has value = 1 ? (undo decrementing)

Upvotes: 1

Views: 4807

Answers (1)

Mert Mertce
Mert Mertce

Reputation: 1634

  1. Create and initialize semaphore to 1.
  2. Let process A wait for semval = 0 (without IPC_NOWAIT) in a while loop
  3. Let process B set the same semaphore value to 0 with SEM_UNDO and exits.

i) You will see that process A would get semaphore 1 time and then again wait. (Because the process B exited and set semaphore value again to 1.)

ii) Now, change step 3 and instead of exit immediately, put sleep(some_time). In this case you will see that process A is getting semaphore various times continuously, until someone again set the semaphore value other than 0. Because process B does not exit and can not set back to 1.

Upvotes: 3

Related Questions