LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 11-17-2006, 09:32 AM   #1
scanner
LQ Newbie
 
Registered: Jul 2006
Posts: 28

Rep: Reputation: 15
Post Semaphore vs Signal


Hello all,

Today I came into a strange problem. Here I hope someone can help me with it.

In my codes, I use a semaphore to synchronize both threads. Both threads are in charge of processing incoming data from two seperate port respectively. And they operate a same data structure which is protected by a semaphore mentioned here. When I attempt to input hulk data into a port, the semaphore works out of way during calling sem_wait with a prompt of "Interrupted system call".

I checked it over with "man" and found some clues. Calling sem_wait during blocked process may be terminated by a signal and return to caller. So far, I am totally lost.

Yes, in the same thread as the calling sem_wait, there are signal use such as SIG34. But generally, what relation or effect is between semaphore & signal? What action can be done to control the relation? What else may cause such problem like my case?

Who have some helpful idea/opinion on my problem?

Thanks in advance!!!
 
Old 11-17-2006, 11:45 PM   #2
scanner
LQ Newbie
 
Registered: Jul 2006
Posts: 28

Original Poster
Rep: Reputation: 15
After thinking the issue over again, I've got a new idea as follows.

To check the return value of sem_wait is helpful a lot in some cases.

Any comments/ideas welcome!
 
Old 11-18-2006, 12:08 AM   #3
btmiller
Senior Member
 
Registered: May 2004
Location: In the DC 'burbs
Distribution: Arch, Scientific Linux, Debian, Ubuntu
Posts: 4,066

Rep: Reputation: 295Reputation: 295Reputation: 295
I've been awake going on 18 hours now, so this explanation may be a little fuzzy but basically sometimes it is necessary for the kernel to sometimes interrupt a process sleeping waiting for I/O (e.g. from a network socket or hardware device). In this case, the error code(in errno is set to EINTR to indicate that the system code did not actually fail, but simply had to be interrupted. According to Stevens's Advanced programming in the Unix Environment this happens when a process receives certain signals. It is harmless -- just test for the particular errno and restart the system call if need be. Also, when setting up signal handlers you can use the SA_RESTART option to follow BSD semantics and restart interrupted system calls automatically when possible (it's still probably a good idea to check for them automatically in your code though). I suggest looking ayt man sigaction for more info,
 
Old 11-18-2006, 02:06 AM   #4
scanner
LQ Newbie
 
Registered: Jul 2006
Posts: 28

Original Poster
Rep: Reputation: 15
Thumbs up

btmiller,

Sorry for confusing you.

Above all, my program runs on user space.

Briefly, for my case, I use sem_wait/sem_post to ensure the synchronization of two threads. But something, I think it is a certain signal, sometimes make sem_wait return. So I tend to check the return code of sem_wait to determine if the thread blocked here would go on to step or continue waiting. According to man, sem_wait returns zero on getting expected resource. Here I need your confirmation on my idea. Say, do my idea will work fine? Or is there better idea for my case?

So far, is my problem clearer?

I will appreciate if you provide any general knowledge related to my case.


Last edited by scanner; 11-18-2006 at 02:08 AM.
 
Old 11-18-2006, 01:05 PM   #5
btmiller
Senior Member
 
Registered: May 2004
Location: In the DC 'burbs
Distribution: Arch, Scientific Linux, Debian, Ubuntu
Posts: 4,066

Rep: Reputation: 295Reputation: 295Reputation: 295
I know your code runs in user space, as I stated though, when you process a signal the kernel may need to interrupt an ongoing system call (read section 10.5 of W. Richard Stevens's book that I mentioned above -- he explains this pretty well).

That aside, however, my man page for sem_wait suggests that it always returns 0, when it gets the resource. To be honest, I've never used the POSIX semaphores (I learned to use SysV semaphores and never really had any reason to switch) so I am not clear on all of their semantics. Since sem_wait is a library routine not a system call, perhaps the library itself handles the signal semantics? To be honest I'm not sure. But it seems that you definitely should be checking the return value to make sure it is what is expected.

You might want to ask a mod to move this thread to the programming forum -- the people over there might have somewhat better knowledge of this than I do.
 
  


Reply

Tags
semaphores


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 On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
squid stops due to signal 6 and signal 25 simplyrahul Linux - Software 3 05-28-2011 01:05 AM
Signal Strength Meter shows low signal? falcon56215 Linux - Networking 0 09-15-2006 07:39 AM
Linux kernel module programming(Signal and semaphore) eshwar_ind Programming 1 02-16-2004 12:10 PM
Signal 6: Unknown Signal <=> Error with MAYA4.X under Linux SOLVED!!!! Faeroon Linux - Software 9 05-09-2003 01:57 PM
Signal: 6 (Unknown Signal) - Problems running a program Faeroon Linux - Software 86 01-14-2003 12:08 AM


All times are GMT -5. The time now is 10:40 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration