I have 3 processes which need to be synchronized. Process one does something then wakes process two and sleeps, which does something then wakes process three and sleeps, which d
Don't blame ubuntu or any other distro on it :-) What is certainly more important here is the version of gcc you are using, 32 or 64 bit etc, how many cores your system has. So please give a bit more information. But looking through your code I found several places that could just bring you unexpected behavior:
it starts with the start, casting int
in void*
back and forth, you are
looking for trouble. Use uintptr_t
for that if you must, but here you
have no excuse to just pass real
pointers to the values. &(int){ 1 }
and some saner casting would do the trick for C99.
ts.tv_nsec = (tv.tv_usec + 500000)
is another trouble spot. The right hand side might be of a different width then the left hand side. Do
ts.tv_nsec = tv.tv_usec;
ts.tv_nsec += 500000;
The sem family of functions are not interrupt safe. Such interrupts may e.g be triggered by IO, since you are doing printf etc. Checking the return value for -1
or so is not sufficient but in such a case you should check errno
and decide if you want to retry. Then you'd have to do the recalculation of the remaining time and stuff like that if you want to be precise. Then man page for sem_timedwait
has a list of the different error codes that might occur and their reasons.
You also conclude things from values that
you get via sem_getvalue
. In a
multi-threading / multi-process / multi-processor
environment your thread might have
been unscheduled between the return
from sem_timedwait
and
sem_getvalue
. Basically you can't
deduce anything from that, the variable
is just incidentally at the value
that you observe. Don't draw conclusions from that.