nptl and signals

Davide Rossetti davide.rossetti at roma1.infn.it
Wed Jul 13 19:43:51 UTC 2005


divij bhatt wrote:

>Hi,
>   I have problem regarding nptl in multi-threading environment.I am
>creating three threads and each thread is calling a functions in which I
>am setting the time for each thread using setitimer.On expiration of
>that timer SIGALRM is called which calls a signal handler function in
>which I am setting the flag and when control return to the main program
>I am checking that flag if it is one then I am sending the packet and
>again make that flag 0.I am using thread specific data and use set
>specific and getspecific to read the threads data.
>But the problem is that in signal handler instead of 3 there are 4
>threads running(4th one suppose to be main thread) which disturbs the
>timing of other threads.Which results in to the packet drooping.Also the
>control is not returning to the main program So kindly help me out,I am
>also attaching the copy of source code.
>
>Thanks in Advance
>Divij
>
>  
>
>------------------------------------------------------------------------
>
>#include<pthread.h>
>#include<stdio.h>
>#include<sys/types.h>
>#include<sys/socket.h>
>#include<netdb.h>
>#include<string.h>
>#include<arpa/inet.h>
>#include<unistd.h>
>#include<sys/times.h>
>#include<semaphore.h>
>#include<sys/time.h>
>#include<signal.h>
>#define CNTR 3 
>
>int sd,rc,i=0,sockfd,len,which=ITIMER_REAL,mark=0;
>struct sockaddr_in cliaddr,remoteservaddr;
>struct hostent *h;
>time_t timeval;
>struct itimerval value;
>struct timespec ts;
>struct timeval tz;
>struct sigaction act;
>FILE *fp;
>char ch,buffer[1000];
>volatile sig_atomic_t flag[CNTR]={0},k=0;
>int n,j,z=0,t,track=0;
>
>static pthread_key_t key;
>pthread_t tid[CNTR];
>pthread_attr_t attr;
>pthread_mutex_t mylock;
>long int thid[3];
>
>
>void myflag(int sig) //This is a signal handler
>{
>    int s=0,ret;
>    pthread_mutex_lock(&mylock);
>    int k =(int)pthread_getspecific(key);
>    printf("\nIn signal Value of K:%d and thread id is:%lu\n",k,pthread_self());   
>    switch(k)
>    {
>      case 0: printf("I am the main thread\t \n");
>              break;
>      
>      case 1:
>              flag[k-1] = 1;
>              break;
>      case 2:
>              flag[k-1] = 1;
>              break;
>     
>      case 3:
>              flag[k-1] = 1;
>              break;
>   
>   }
>   pthread_mutex_unlock(&mylock);
>   return;
>}
>  
>
I did not read past this... but mutex functions are not documented as 
signal safe, are they ?
in fact, the man for pthread_mutex_lock says:
      "If a signal is delivered to a thread waiting for a mutex, upon 
return from the signal handler the thread shall
       resume waiting for the mutex as if it was not interrupted."

i.e. sem_post() is explicitly documented as sig safe in the man page:
       "The  sem_post()  function shall be reentrant with respect to 
signals and may be invoked from a signal-catching
       function."

moreover, in nptl I think that signals are not per-thread but 
per-process... so there is no hope that a signal is routed to a specific 
thread. instead, you assume that the signal is routed to the thread 
which called setitimer().

reading more, in general I think your implementation is "weak" at least :)

regards




More information about the fedora-devel-list mailing list