From mboxrd@z Thu Jan 1 00:00:00 1970 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 111c93,99dabc73a31eb7ce,start X-Google-Attributes: gid111c93,public From: jefflandgraf@my-deja.com Subject: Problems with semaphores Date: 1999/09/14 Message-ID: <7rlfha$evk$1@nnrp1.deja.com>#1/1 X-Deja-AN: 524854099 X-Http-Proxy: 1.0 x34.deja.com:80 (Squid/1.1.22) for client 130.199.88.119 Organization: Deja.com - Share what you know. Learn what you don't. X-Article-Creation-Date: Tue Sep 14 12:18:54 1999 GMT Newsgroups: comp.os.linux.alpha X-Http-User-Agent: Mozilla/4.5 [en] (WinNT; I) 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 #include #include #include 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.