Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calling System function from PThread

Status
Not open for further replies.

ThinkILostMyHead

Programmer
Nov 12, 2007
3
0
0
Hello All,

I am creating a c program that uses Pthreads and posix Semaphores. In my thread I am accessing shared memory and copying the needed data and then executing a given command via System() and putting it to sleep after words. When I do this with one thread it works fine but if I use more than one thread I get this error "sem_wait error: Interrupted system call". If I remove the system() and Sleep() and I do not recive this error and I can tell that I am getting the correct data from the shared memory. FYI I am using Semaphores to protect shared memory and to ensure there is data to read.

I guess my question is what causes this type of error and how to prevent it?

Please Help!

Thanks
 
Sounds like quite a few problems here.

fork() and exec() from threads can be problematic if mutexes are involved. See the man page for pthread_atfork(). Another possible issue is that with the creation of the new process any previously obtained semaphore or shared memory handles are going to be invalidated. Sort of sounds like your issue. As a general rule blocking (sleeping) in a thread when holding or releasing a lock of any sort (whether sysV/posix or pthread specific) is asking for a race condition if the whole operation sequence is non-atomic..this could also be contributing to your problem. Post a code section and I'll try to be more specific.
 
I have stipped down my test function that I create threads based on. shared is my shared memory element.

Thanks for any help or advice.



void *Test(void *arg)
{
int i;

for ( ; ; )
{
Sem_wait(&shared.nstored);
Sem_wait(&shared.mutex);

i = shared.temp;
shared.temp = shared.temp + 1;

Sem_post(&shared.mutex);
Sem_post(&shared.nempty);

while(1)
{
if(i == 1)
{
system("ps");
}else
{
system("pwd");
}
sleep (1);

}
}
}
 
Also I tried to remove

Sem_wait(&shared.nstored);
Sem_post(&shared.nempty);

from the prevoius post and it did run. This does not make sense to me.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top