Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 2 Oct 1998 01:00:47 -0700 (PDT)
From:      Robert Schulhof <rrs@LMI.Net>
To:        current@FreeBSD.ORG
Subject:   thread behavior
Message-ID:  <199810020800.BAA14115@badlans.lanminds.com>

next in thread | raw e-mail | index | archive | help

Hi,

  I am not sure if this is the expected behavior according to the current
IEEE standard, but sleep calls in any thread seem to suspend all
threads.  Also when a thread waits on a semop it seems to put the whole
process to sleep.  Are semaphores thread safe?  I've tried compiling
with cc and with egcs 1.1.  The weird part is that the program will
occasionally work correctly  ( The semop calls, that is.  sleep doesn't
work).  This program behaves as I expect Solaris 2.5.1.  Under FreeBSD
2.2.7-Stable it freezes my terminal :-)  Might be pilot error.


Also, ACE wrappers, which worked before the recent changes to libc_r, now
produces the following in the Semaphore/Threads test:

Fatal error 'Dead thread has resumed' at line ? in file /usr/src/src/lib/libc_r/
uthread/uthread_exit.c (errno = ?)

I get this error about 30% of the time.  I'm just learning how to use
ACE wrappers so I shouldn't attempt to figure that one out. This was after 
cvsup around 9:30 PM PDT 30 Sep.  


Thanks! I know you're busy so ignore unless this helps with the 3.0 Beta
shakeout.  I include the test below. define TEST_SLEEP to test sleep 
in a thread.

#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <pthread.h>

#define NUM_THR 12
#define SEMKEY 0x420ffL
#define PERMS 0666
void hanging_by_a(int *i);
static struct sembuf op_lock[2]={ 0,0,0,0,1,0};   
static struct sembuf op_unlock[1]={0,-1,IPC_NOWAIT};   
main()
{
 int i,semid;
 pthread_t tid[NUM_THR];

  if ( (semid=semget(SEMKEY,1,IPC_CREAT | PERMS)) < 0 ){
      perror("Semget");
      exit(1);
  }
  fprintf(stderr,"Semaphore ID: %d\n",semid);
  /* anchors away ! */
  semop(semid, &op_unlock[0], 1);
  if  ( semop(semid, &op_lock[0], 2) < 0 ){
     perror("lock sem");
     exit(1);
  }
  for (i=0; i < NUM_THR; i++){
     if ( pthread_create(&tid[i], NULL, (void *)hanging_by_a, (void *) &semid) )
       perror("pthread_create");
       fprintf(stderr,"Spawned thread #%d %u\n",i,tid[i]);
  }
  if  ( semop(semid, &op_unlock[0], 1) < 0 ){
     perror("lock sem");
     exit(1);
  }
  for (i=0; i < NUM_THR; i++){
    pthread_join(tid[i], NULL);
    fprintf(stderr,"Reaped thread %d %u\n",i,tid[i]);
  }
}

void hanging_by_a(int *sid)
{
     pthread_t me;

     me = pthread_self();
     fprintf(stderr,"I am tid  %u Semaphore ID %u\n", me,*sid);
     if  ( semop(*sid, &op_lock[0], 2) < 0 ){
          perror("Thread lock sem");
     }
     fprintf(stderr,"Thread %u has the lock\n", me);
#ifdef TEST_SLEEP
     sleep(1);
#endif
     if  ( semop(*sid, &op_unlock[0], 1) < 0 ){
          perror("Thread lock sem");
     }
   fprintf(stderr,"Thread %u freed lock\n", me);
   pthread_exit(NULL);
}

 
 Robert Schulhof
 UNIX System Administrator
 LanMinds Internet. (LMI Net)
 rrs@lmi.net
 http://www.lmi.net
 (510) 843-6389 VOX
 (510) 843-6390 FAX

To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-current" in the body of the message



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?199810020800.BAA14115>