LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-23-2005, 02:18 AM   #1
Kumar
Member
 
Registered: Sep 2003
Location: Pune, India
Distribution: Red Hat
Posts: 106

Rep: Reputation: 15
Atomic operations in User Mode


Hi,
I have written a program which is as follows -

Thread_send()
{
send_data();
wait_for_signal(); /*wait for signal SIGUSR1 using sigwait()*/
read_data_from_buffer();
return;
}

Thread_receive()
{
while(1)
{
wait_for_data_on_socket();
read_data();
copy_data_to_buffer();
send_signal_to_thread(thread_id,SIGUSR1); /*send SIGUSR1*/
}
}


main()
{
start_thread(thread_send);
start_thread(thread_receive);
}

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.?
 
Old 12-23-2005, 05:22 PM   #2
rstewart
Member
 
Registered: Feb 2005
Location: Sunnyvale, CA
Distribution: Ubuntu
Posts: 205

Rep: Reputation: 38
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)

Reader thread blocks on read, reads data, gives semaphore (unblocks sender thread)

Anyway, you get the idea...
 
Old 12-23-2005, 10:04 PM   #3
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
rstewart is absolutely correct - you need "locking", some kind of "mutual exclusion".

Depending on your specific application, you have many choices. Here's a brief tutorial that discusses pthreads mutexes:

http://students.cs.byu.edu/~cs460ta/.../pthreads.html

'Hope that helps .. PSM

PS:
My guess is that you'll probably be able to lose both "wait_for_signal()" and " send_signal_to_thread()".
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
root operations under normal user phsythax Linux - Newbie 11 11-09-2005 03:19 AM
Interface between user mode and kernel mode santosh_pn Linux - Software 1 06-17-2005 07:20 AM
how to open /proc file in RW mode in user mode? cranium2004 Programming 3 03-06-2005 03:03 AM
atomic clock sync salviadud Slackware 8 02-28-2005 11:28 PM
Atomic Clock Sync cjwcash Linux - Software 1 08-06-2003 12:14 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:54 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration