Sample Header Ad - 728x90

How to send a signal to a set of pthread within one process in Linux?

0 votes
2 answers
595 views
I am working on an embedded Linux system (kernel-5.10.24). There is a multi-threaded process needed to handle a certain of exception delivered through a signal. The process is something like below.
void *thread2(void *arg)
{
    while(1) {
        does_other_jobs();
    }
    return NULL;
}

void *thread1(void *arg)
{
    pthread_create(tid2, ..., thread2, ...);

    while(1) {
        does_some_job();
    }
    pthread_join(...);

    return NULL;
}


int main()
{
    pthread_create(tid1, ..., thread1, ...);
    pthread_join(tid1, NULL);
}
Now, I want to have a way to kill/stop running of thread1 and its children. I tried to send SIGTERM to thread1, but the SIGTERM killed this process as a whole. What I did is as follows,
int main()
{
    sigset_t mask;

    pthread_create(tid1, ..., thread1, ...);

    sigemptyset(&mask);
    sigaddset(&mask, SIGTERM);
    pthread_sigmask(SIG_BLOCK, &mask, NULL);
    pthread_join(tid1, NULL);

    return 0;
}
So is there anyting wrong in above implementation? And is there a way to only stop running of thread1 and its children pthread?
Asked by wangt13 (631 rep)
Jun 12, 2024, 02:25 PM
Last activity: Jun 13, 2024, 07:17 PM