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

  • Home
  • SEARCH
  • 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 6707957
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:42:05+00:00 2026-05-26T07:42:05+00:00

My current program is creating child processes and giving them work (CPU intensive work).

  • 0

My current program is creating child processes and giving them work (CPU intensive work). The main() sits there and waits for the child processes to send data via pipes (using select).

What I wanted to do is when the program is processing data I could press CTRL+C to stop the child processes from working and asking the user if he wants to quit or resume work.

If user wants to quit, the program would kill all the processes. If user wants to resume work, it would tell the child processes to resume the computation.

I already have the code in place but it’s not quite working right.

In main I have signal(SIGINT, pausar); to handle SIGINT (CTRL+C).

This is the pausar() function:

void pausar(int signum){
    signal(SIGINT, pausar);

    int i;
    // pid[] contains all the child processes
    for(i = 0; i<CORES; i++)
    {
        kill(pid[i], SIGSTOP);
    }

    char option[2];
    printf("\n Computacao pausada.\n'S' para sair ou 'C' para continuar: ");

    scanf("%1s", option);
    if (option[0] == 's' || option[0] == 'S') {
        printf("A desligar...\n");

        //if user wants to quit, kill all the child processes
        for(i = 0; i<CORES; i++)
        {
            kill(pid[i], SIGKILL);
        }

        exit(0);
    }
    else
    {
        printf("[%d] A resumir computacao...\n",getpid());
        kill(getpid(), SIGCONT);

        //if user wants to resume work, send signal to continue
        for(i = 0; i<CORES; i++)
        {
            kill(pid[i], SIGCONT);
            printf("%d resumiu\n", pid[i]);
        }
    }
}

The problem is that sometimes I press CTRL+C and nothing shows in the console (but the processes STOP because I’m paying attention to the process manager). The other problem is that after I enter ‘C’ to resume work, I get errors in select() and the children never resume work.

  • 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-26T07:42:06+00:00Added an answer on May 26, 2026 at 7:42 am

    First, for what you’re trying to-do, your signal handler is way too complex. Secondly, calling signal() inside your signal handler is not a good idea … it’s not an asynchronous signal-safe function.

    What you can do is the following:

    1. In your main, set the signal handler function using signal() like you’ve done.
    2. Block the SIGINT signal via sigprocmask(). This prevents a spurious signal from arriving before the call to pselect().
    3. Inside your signal handler only set a simple global flag that is a sig_atomic_t
    4. Use pselect() instead of select(). This will allow you to change the process signal mask to allow a SIGINT signal to arrive, and it will do-so in an atomic manner with respect to signals. Otherwise, you could have your SIGINT arrive before the call to select(), and then you have “lost” that signal, even though it does set the flag in the handler.
    5. When the pselect() call returns, detect whether the flag has been set.
    6. If the global sig_atomic_t flag was set, and you returned from pselect because of a caught signal, then launch another function that will actually do all the ending of the child-processes and prompt the user, etc.

    Doing these steps will simplify your signal-handling code and reduce the chances of race-conditions or other unexpected results because of the asynchronous nature of signal arrival.

    If you’d like some more information on pselect(), you there is a nice article on that here.

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

Sidebar

Related Questions

In my current program I have a main window and a secondary window that
I am creating a c# program that should get: 1) The current users on
My current program need to use programatically create a XPathExpression instance to apply to
http://img136.imageshack.us/img136/3508/texturefailz.png This is my current program. I know it's terribly ugly, I found two
I need to use the current library name explicitly in a CL program. CHGVAR
How can I change the current working directory from within a Java program? Everything
I am trying to get the program to call up the current date, add
Using my c++ program how can I find out what group the current user
How can I avoid that a user starts the same program twice? The current
Current, I've got a stored procedure that has a main goal of doing a

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.