Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 625739
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T19:20:33+00:00 2026-05-13T19:20:33+00:00

Situation: I am writing a program in C that maintains a number of threads.

  • 0

Situation:

I am writing a program in C that maintains a number of threads. Once a thread ends, a new one is created.

Each thread forks – the child runs a process via exec() and the parent waits for it to finish.

In addition, there is a signal handler thread that waits for signals. If SIGINT is detected then it tells the main thread to stop creating threads so eventually all the threads end and the program can exit.

Signals are blocked in all threads except of course the signal handler thread.

Aim:

I want to be able to terminate the program by sending SIGTERM. This would work by stopping the main thread creating new threads and also terminating the running processes created by threads.

Problem:

If signals are blocked in all the threads, how can I send a signal to the running processes to terminate them?

Is there some way to make the spawned processes only receive signals sent from the main program and not signals sent to the main program?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T19:20:33+00:00Added an answer on May 13, 2026 at 7:20 pm

    Create all child processes in a new process group, then send the signal to the group.

    Edit:

    Here’s some minimal code that shows how process can change it’s group and control child processes with a signal.

    #include <err.h>
    #include <signal.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    
    pid_t pgid; /* process group to kill */
    
    void terminator( int s ) /* signal handler */
    {
        printf( "[%d:%d] received signal %d, exiting\n",
            getpid(), getpgrp(), s );
        exit( 1 );
    }
    
    int main() /* program entry */
    {
        printf( "[%d:%d] before first fork\n",
            getpid(), getpgrp() );
    
        switch ( fork()) /* try with and without first fork */
        {
            case -1: err( 1, "fork" );
            case 0: break;
            default: exit( 0 );
        }
    
        if ( signal( SIGTERM, terminator ) == SIG_ERR )
            err( 1, "signal" );
    
        if ( setpgrp() < 0 ) err( 1, "setpgrp" );
        if (( pgid = getpgrp()) < 0 ) err( 1, "getpgrp" );
    
        printf( "[%d:%d -> %d] before second fork\n",
            getpid(), getpgrp(), pgid );
    
        switch ( fork())
        {
            case -1: err( 1, "fork" );
            case 0: /* child */
            {
                printf( "child [%d:%d]\n", getpid(), getpgrp());
                sleep( 20 );
                break;
            }
            default: /* parent */
            {
                printf( "parent [%d:%d]\n", getpid(), getpgrp());
                sleep( 5 );
                killpg( pgid, SIGTERM );
                sleep( 20 );
            }
        }
    
        printf( "[%d:%d] exiting\n", getpid(), getpgrp());
        exit( 1 );
    }
    
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Situation: I am writing a program in C that maintains a number of threads.
Here is my situation. I am writing a program that will store a database.
I am writing a program that can be loaded by another service (under our
Situation: I am writing a program to solve for primes. I need to solve
I'm writing a program in C that needs to do some fast math calculations.
I'm writing a program in Python that needs to use a site's advanced search
I am writing a program that uses an unmanaged library. What is a best
My situation is the following: I am writing a toolbox that generates two libraries.
I've ran into a strange situation. I'm writing some test cases for my program.
I am writing a program that composes an email reply. The original email may

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.