semaphore program
Hi All
I running one simple semaphore application. I have 2 threads , where one thread is waiting for sem post to happen so I have put sem_wait & other thread is doing sem_post .When I come keeping delay (sleep of 1 sec) I find scheduling is happening properly .But without delay I am finding it strange behaviour i.e. scheduling is not in the proper way as it was when I kept delay of 1 second. I feel sem_wait should have been blocked & the print "after wait" should only execute when sem post happens I have kept both program as well as output also . Can anyone tell me why is that so i.e its not scheduling properly . Or I have done something wrong in application. Code:
#include <unistd.h> /* Symbolic Constants */ Code:
OUTPUT: |
you can not assume that the output on the console keep the order in which the events happen.
if you whant this you need a own syncronisation mechanism. printf only helps you to not mix the paralell output of the text 'after post' and 'after wait' to something like 'aaffter te rpposotst' (with one new line somewhere in between) if you want to see that start_routine1 realy waits, put a getchar() in front of sem_post than you can see that nothing happens until you hit return. |
As a start, add fflush(stdout) after every printf.
Plus, you should state what do you really what to achieve with this. If you want to synchronize your threads I suggest you use mutex variables. |
All times are GMT -5. The time now is 02:21 AM. |