From: jefflandgraf@my-deja.com
Subject: Problems with semaphores
Date: 1999/09/14 [thread overview]
Message-ID: <7rlfha$evk$1@nnrp1.deja.com> (raw)
I'm having problems with the pthread library using
Redhat 6.0 with kernal 2.2.10 on alpha. The
following simple program creates two threads
which trade semephores back and forth between
themselves. They crash after some time, with one
of three results.
1. Segmentation fault
2. Illegal instruction
3. Both of the threads with the semephore are
suspended and the main thread continues.
(Note. If I replace the semephores with mutex's I
get the same results)
Here's the program:
----------------------------
#include <pthread.h>
#include <stdio.h>
#include <errno.h>
#include <semaphore.h>
sem_t theSem;
void *theThread(void *task)
{
int t = (*((int *)task));
int i = 0;
for(;;)
{
loop1:
int iret = sem_wait(&theSem);
if(iret)
{
if(errno == EINTR)
{
printf("Interupted in task %d\n",t);
goto loop1;
}
else
{
printf("Bad error %d\n",errno);
exit(0);
}
}
// this is just to slow it down a bit
int j=0;
for(j=1;j<30;j++)
{
int k= 5;
k=k+k;
}
i++;
if(i>300000)
{
printf("Task %d is calling\n", t);
i=0;
}
sem_post(&theSem);
}
}
int main()
{
pthread_t tid1,tid2;
int task1 = 1;
int task2 = 2;
sem_init(&theSem, 0, 1);
if(pthread_create(&tid1, NULL, theThread,
&task1))
{
printf("Error creating thread 1\n");
exit(0);
}
if(pthread_create(&tid2, NULL, theThread,
&task2))
{
printf("Error creating thread 2\n");
exit(0);
}
for(;;)
{
sleep(1000);
printf("Main task\n");
}
}
Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.
next reply other threads:[~1999-09-14 4:00 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
1999-09-14 4:00 jefflandgraf [this message]
1999-09-14 4:00 ` Problems with semaphores Einar Floystad Dorum
1999-09-15 4:00 ` Jeff Landgraf
read-only archive
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox