Advertisement

03.31.2003 at 02:14PM PST, ID: 20569321
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

7.2

Linux threads vs signal handler

Asked by pankaj_garg in Linux Programming

Tags: , , ,

Hi,
  I have a program in which I'm spawning of 3 child processes. The main associates a signal handler (Sigterm and Sigint) and than spawns 3 threads. After this main calls pthread_join to wait for 3 child processes to get over. In the signal handler method, in case the signal is received by one of the threads, I'm re-sending the signal to the main thread. In case it is received by the main thread, I'm not doing anything.

This program works fine on Solaris and program terminates gracefully after sending maximum of 4 signals. But when I run it on Linux, the signal is always received by the main thread. So the program never terminates, even if I send the signal any number of time. How can I rectify the problem? Here is the program.

--------------------------------------------------------------------
#include<iostream.h>
#include <pthread.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#define NTHREADS 4


pthread_t worker[NTHREADS];
pthread_t pid;    

void smServerSignalHandler(int sig_num) {
     cout<<"Got signal "<<sig_num<<" in the handler"<<endl;

        struct sigaction arg;
     arg.sa_handler=smServerSignalHandler;
     sigaction(SIGTERM, &arg, NULL);    
        if (pthread_self() == pid)
     {
          cout<<" pthread_self() == pid xxxxxxxxxxxx Current id"<<endl;

     }
     else
     {
                pthread_kill(SIGTERM,pid);
          cout<<"pthread_self() != pid xxxxxxxxxxxxx NOT Current id"<<endl;
     }
}

//The function hello is being called on the creation of all the 3 threads.
void *hello(void *arg) {
     int myid=*(int*)arg;
     int *status1;
     
     pause();
     
     return (void*)myid;
}

int main(int argc, char *argv[]) {

     int counter[NTHREADS];
     int *status;
     
     pid=pthread_self();

        struct sigaction arg;
     arg.sa_handler=smServerSignalHandler;
     sigaction(SIGTERM, &arg, NULL);    
        if (pthread_self() == pid)

     for(int k=0;k<NTHREADS;k++) {
          int errorcode;

          if ((errorcode=pthread_create(&worker[k],NULL,hello,(void*)&k)) > 0)    
               printf("The error code is %d \n",errorcode);
     }

     for(int l=0;l<NTHREADS;l++) {
         
          //Now the main thread is going to join all the threads.
           pthread_join(worker[l],(void**)&status);
     }
}
-----------------------------------------------------------------------

Regards,
PankajStart Free Trial
 
Loading Advertisement...
 
[+][-]04.01.2003 at 11:21AM PST, ID: 8248017

View this solution now by starting your 14-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: Linux Programming
Tags: pthread_kill, kill, signal, vs
Sign Up Now!
Solution Provided By: GaryFx
Participating Experts: 3
Solution Grade: A
 
 
[+][-]04.02.2003 at 06:52PM PST, ID: 8258393

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 14-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]04.02.2003 at 06:56PM PST, ID: 8258408

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 14-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]04.02.2003 at 07:00PM PST, ID: 8258418

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 14-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]04.05.2003 at 07:26AM PST, ID: 8275186

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 14-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]04.05.2003 at 08:13AM PST, ID: 8275343

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 14-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]01.28.2004 at 10:53PM PST, ID: 10225261

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 14-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]02.05.2004 at 06:26PM PST, ID: 10286670

Experts Exchange has a courteous staff of administrators who help members get the most out of the website by means of administrative comments like this one.

Start your 14-day free trial to view this Administrative Comment or ask the Experts your question.

 
 
Loading Advertisement...
20081112-EE-VQP-43