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 9068191
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T17:07:56+00:00 2026-06-16T17:07:56+00:00

Possible Duplicate: Child process receives parent’s SIGINT the is my code in below The

  • 0

Possible Duplicate:
Child process receives parent’s SIGINT

the is my code in below

The function is
after the father receive the signal of CTRL-C.
the father process send signal to son1 and son2;
after the son1 and son2 exit, the father process exit;

but the result does not like that;
this is the answer

son1
son2
^Cheeell
the son1 id 5963
the son2 id 5964
Parent process is killed !!

who can help me, thank you before!!

#include <stdio.h>
#include <sys/signal.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/wait.h>

#define SIGNAL_TO_SON1 10
#define SIGNAL_TO_SON2 12

#define FLAG_MSG_NO 0
#define FLAG_MSG_YES 1

int parent_msg = FLAG_MSG_NO;
void signal_sendToParent( int sig )
{
    printf("heeell\n");
    parent_msg = FLAG_MSG_YES;
}

int son1_flag = FLAG_MSG_NO;
void signal_handle_son1( int sig )
{
    printf("handle_son1\n");
    son1_flag = FLAG_MSG_YES;
}

int son2_flag = FLAG_MSG_NO;
void signal_handle_son2( int sig )
{
    printf("handle_son2\n");
    son2_flag = FLAG_MSG_YES;
}

int main( void )
{
    int pid1, pid2;
    if ( ( pid1 = fork() ) < 0 )
    {
        printf("erro!!!!");
        return -1;
    }

    if ( pid1  > 0 )
    {
        // in the parent
        while( ( pid2 = fork() ) == -1);
        if ( pid2 == 0 )
        {
            signal( SIGNAL_TO_SON2, signal_handle_son2 );
            printf("son2\n");
            // in the son2;
            while ( son2_flag != FLAG_MSG_YES );
            {
                printf("Child process 2 is killed by parent !!\n");
                exit(0);            
            }
        }else{  
            // setup the signal
            signal( SIGINT, signal_sendToParent );
            while ( parent_msg != FLAG_MSG_YES );
            {
                kill( pid1, SIGNAL_TO_SON1 );
                kill( pid2, SIGNAL_TO_SON2 );
                wait(0);
                printf("the son1 id %d\n", pid1 );
                printf("the son2 id %d\n", pid2 );              

                wait(0);
                int status , pid ;
                while( ( pid = waitpid( -1, &status, 0 ) )  > 0 )
                {
                    printf("child %d \n", pid );
                }
                printf("Parent process is killed !!\n");
                exit(0);
            }
        }
    }else{
        // in the son1;
        printf("son1\n");
        signal( SIGNAL_TO_SON1, signal_handle_son1 );
        while ( son1_flag != FLAG_MSG_YES );
        {
            printf("Child process 1 is killed by parent !!\n");
            exit(0);
        }
    }

    return 0;
}
  • 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-06-16T17:07:58+00:00Added an answer on June 16, 2026 at 5:07 pm

    to get what you want you must ignore SIGINT in the childs.

    see this What happens to a SIGINT (^C) when sent to a perl script containing children?

    In short Ctrl-C is send to all processes in the foreground group.
    That means your child processes get SIGINT too, they do not have a handler and get killed.

    signal( SIGINT, SIG_IGN );
    

    add in the child code, near setting the other signal handler.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: How to force child same virtual function call its parent virtual function
Possible Duplicate: How to force child same virtual function call its parent virtual function
Possible Duplicate: Complex CSS selector for parent of active child I'm making a CSS
Possible Duplicate: Complex CSS selector for parent of active child How would I make
Possible Duplicate: Complex CSS selector for parent of active child I want to match
Possible Duplicate: jQuery single selector vs .find() Is there any difference between $(Parent Child)
Possible Duplicate: How can I capture the stdout output of a child process? I'm
Possible Duplicate: I do not want to inherit the child opacity from the parent
Possible Duplicate: Delete object and all its child objects in Entity Framework? This code:
Possible Duplicate: How can I run a child process that requires elevation and wait?

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.