Usenet comp.os.linux.alpha (1997-2013)
 help / color / mirror / Atom feed
* Problems with semaphores
@ 1999-09-14  4:00 jefflandgraf
  1999-09-14  4:00 ` Einar Floystad Dorum
  0 siblings, 1 reply; 3+ messages in thread
From: jefflandgraf @ 1999-09-14  4:00 UTC (permalink / 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.



^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~1999-09-15  4:00 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
1999-09-14  4:00 Problems with semaphores jefflandgraf
1999-09-14  4:00 ` Einar Floystad Dorum
1999-09-15  4:00   ` Jeff Landgraf

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox