Review your favorite Linux distribution.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 05-06-2007, 08:22 PM   #1
LQ Newbie
Registered: Apr 2004
Posts: 5

Rep: Reputation: 0
Semaphores Problems


I am using a simple sunchronization between threads, one compute, the second send dependency to other processors, and the thread received dependency from other processors, I am using MPI to send/receive between processors, and in each process, these three threads to do the inner computation of the parts done by each one,

The problem is in the threads syncronization, I use semaphores, I wait on the sending thread till there is a dependency that needs to be sent, and on the receiving thread till the computation require a dependency and need to check the MPI to receive it, I have three semaphores, one between the computation and sending, one between the computation and receiving, and one between the receiving and the computation to notify that it received something to be checked,

I initialize the three semaphores as :
if (sem_init(&icSem, 0, 0) != 0 ) { 
printf ("Error Initializing Semephore icSem, Exiting\n"); getSemError (); 
and post like that:

if (sem_post(&rcSem) != 0) { 
printf ("Error post Semephore rcSem, Exiting\n"); 
getSemError (); 
return NULL; 

and wait like this:

if (sem_wait(&dsSem) != 0) { 
printf ("Error waiting on Semephore dsSem, Exiting\n"); getSemError (); 
return NULL; 

in the getSemError () I read the error and type the message as in the man files,

then I get this in the run time:
The call was interrupted by a signal handler. when I wait on the computation thread for the receiving thread to receive a dependency that I need to resume computing,and same problem happen when I wait in the sending thread on another semaphore, it fails to wait with the same error, practically on all sem_wait calls,

and when I read the value of the semaphore, it sometimes exceeds 1, I read values like 2 and 3 as well, while on every sem_post, there is a previously called sem_wait that once receives it should work on it,

I used semaphores, because I thought semaphores signals remain in memory, and order of calls don't affect execution, I previously tried pthread conditions (pthread_cond_wait / pthread_cond_signal), and it didn't work, and I learned that if I signal and nothing is waiting, the signal will disappear, and semaphores solved this problem, as I can wait either before or after the post signal, and the wait will block only if the semaphore value is zero, otherwise (i.e. a post was already received) it will decrement and come back

now, this understanding doesn't seem working either with semaphore,

I appreciate any help in this issue, as it has taken so much time from me, and I am afraid I might be in a wrong direction completely,

I appreciate a redirection to another forum (preferebly by the developers of the semaphores library for ANSI C in linux and/or pthreads library) or other concurrency gurus,

Thank you very much,


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
binary semaphores dzhigit Programming 4 05-25-2005 08:57 PM
Semaphores and IPC gsu_790 Programming 2 09-29-2004 09:29 AM
What is Semaphores? imsajjadali Red Hat 3 04-23-2004 01:26 AM
Semaphores help mojozoox Programming 1 01-07-2004 08:41 AM
semaphores charles_sjc Linux - Software 2 12-06-2003 12:15 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:47 PM.

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