ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hi,
I try to use cygwin and linux expecting to have the same result but I discover this "problem".
I try to learn about thread and semaphore and I write this simple program
I compiled it under cygwin and I receve what I expect i.e. in a random way all the three thread lock the semaphore, update the variable and unlock the semaphore. The access to the resource (the 'a' variable) is equally distribuited between the three thread.
The I compiled under Fedora Core 3 exaclty the same program and I noticed that if i.e. Routine 1 passes the semaphore once, then about 300-400 time again use the resource. If another thread is able to gain the resource, then that thread gain it again for other 300-400 times. I expected, as in cygwin, that the access to the resource would be again equally distribuited.
Did I make any mistake?
Could be your use of IPC_NOWAIT. I ran your program with and without IPC_NOWAIT and I got more reasonable behavior without the NOWAIT. Perhaps because with NOWAIT the semaphore release never actually takes place cleanly (i.e. the thread requests to unlock the semaphore, but with IPC_NOWAIT it returns and grabs the semaphore again before the unlock can be processed, thus starving the other threads. However, this is really just a guess on my part, and I may very well be 100% wrong, in which case I too am curious of the reason.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.