View Full Version: signals and threads

C++ Learning Community > C++ for Linux > signals and threads


Title: signals and threads


C-Man - October 19, 2005 09:52 AM (GMT)
CODE

#include <pthread.h>
#include <stdio.h>
#include <signal.h>
#include <unistd.h>


void * foo (void *);
void bar (int);

int main (int argc,char ** argv) {
   
   pthread_t f_tid;
   
   bar (SIGALRM);
   
   if (pthread_create (&f_tid,NULL,&foo,"Billy boy") == 0) {        
       for (;;) {
           sleep (1);                        
           if (pthread_kill (f_tid,SIGALRM) != 0) {
               printf ("error : can't send signal, aborting.\n");
               break;
           }
       }
   }
   return 0;  
}

void * foo (void * p) {
   int i = 0;
           
   for (;;) {
       pause ();        
       printf ("%d) Good morning %s!\n",++i,p != NULL ? (char *)p : "foo");
   }
   return NULL;  
}

void bar (int sn) {

   signal (SIGALRM,bar);
}


why does pause unblock only once O.o ?

myork - October 19, 2005 01:26 PM (GMT)
Works fine for me on the following RH box:

CODE
uname -a
Linux <machineName> 2.4.9-21smp #1 SMP Thu Jan 17 14:01:48 EST 2002 i686 unknown



Copied code into: sig.cpp
Changed the pthread_create() line so that the string is cast to void
CODE
   if (pthread_create (&f_tid,NULL,&foo,(void*)"Billy boy") == 0)

Added a printf() to bar
CODE
   printf("Setting signal %d\n",sn);

Neither of these things should affect the code.

CODE
> g++ sig.cpp -lpthread
> ./a.out
Setting signal 14
Setting signal 14
1) Good morning Billy boy!
Setting signal 14
2) Good morning Billy boy!
Setting signal 14
3) Good morning Billy boy!
Setting signal 14
4) Good morning Billy boy!
Setting signal 14
5) Good morning Billy boy!
Setting signal 14
6) Good morning Billy boy!

C-Man - October 19, 2005 05:21 PM (GMT)
i tested it with cygwin , must be a bug ><

myork - October 19, 2005 06:15 PM (GMT)


Windows version of signals is very subtly different from UNIX.
Porting code between the too is very tricky at best.

C-Man - October 19, 2005 07:46 PM (GMT)
yeah i was going to get some linux or BSD when my CD-RW is replaced
so uhh what do you think i should get ?

myork - October 19, 2005 08:13 PM (GMT)

Linux is moving a lot faster as there are more people developing for it.
But BSD is more stable as they seem to be more rigorus (though i have no actual metrics for that just hersay) about validating checkins.




Hosted for free by InvisionFree