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.
Now the problem is - when the program starts executing, it creates the first thread i.e thread_send and starts executing it. But sometimes, thread_read is pre-empted and main thread starts executing which in turn starts thread_receive. Now, if thread_read was pre-empted after wait_for_signal()call, there is no problem. But if it is pre-empted before wait_for_signal()call, their is no signal handler for the same and if thread_receive send a signal to thread_send, the program terminates with "User defined signal 1". Is their any way I can prevent this situation and make sure that the signal is send only after wait_for_signal() has excuted. Is it possible to make the operations
[send_data(); and wait_for_signal();] atomic so that both these operations execute together.?
Preload a signal handler from your main thread before you start any of your sub-threads. Initialize a semaphore also from your main thread before you start any of your sub-threads, make sure that the semaphore is initialized such that the next call to take it will block.
In your global signal handler, re-arm the handler and if the signal is USR1, then give the semaphore.
In your sender thread, send data and take semaphore.
In your reader read data and send signal.
<<<< OR >>>>
Just simply use a semaphore instead of a signal.
Main code creates semaphore, initializes semaphore to cause next take to block
Creates reader and sender threads
Sender thread sends data (unblocks reader thread), takes semaphore (blocks)
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.