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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T22:55:01+00:00 2026-05-16T22:55:01+00:00

I have written a program that invokes a system command from inside: #include <stdlib.h>

  • 0

I have written a program that invokes a system command from inside:

#include <stdlib.h>

int main(void)
{
    while(1)
    {
        system("ls 2>&1 1>/dev/null"); // comment this line out to enable ctrl+break
    }

    return 0;
}

However, when it is running, CTRL+C and CTRL+BREAK no longer work and appear to be ignored.

I am trying to write a program that performs some operations in the background involving the shell, but I also want to be able to break out of the program when the user wants to break.

Is there a way to make it work the way I want? Should I change the architecture to perform some kind of fork / exec?

  • 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-16T22:55:01+00:00Added an answer on May 16, 2026 at 10:55 pm

    From the POSIX specification for system():

    The system() function ignores the SIGINT and SIGQUIT signals, and blocks the SIGCHLD signal, while waiting for the command to terminate. If this might cause the application to miss a signal that would have killed it, then the application should examine the return value from system() and take whatever action is appropriate to the application if the command terminated due to receipt of a signal.

    So, in order to respond properly to signals, you need to examine the return value of system().

    system() returns the termination status of the command language interpreter in the format specified by waitpid()

    And the docs of waitpid() refer to the docs for wait(), which instruct you to use the following macros to find out why a process exited:

    • WIFEXITED(stat_val)
      Evaluates to a non-zero value if status was returned for a child process that terminated normally.
    • WEXITSTATUS(stat_val)
      If the value of WIFEXITED(stat_val) is non-zero, this macro evaluates to the low-order 8 bits of the status argument that the child process passed to _exit() or exit(), or the value the child process returned from main().
    • WIFSIGNALED(stat_val)
      Evaluates to non-zero value if status was returned for a child process that terminated due to the receipt of a signal that was not caught (see ).
    • WTERMSIG(stat_val)
      If the value of WIFSIGNALED(stat_val) is non-zero, this macro evaluates to the number of the signal that caused the termination of the child process.
    • WIFSTOPPED(stat_val)
      Evaluates to a non-zero value if status was returned for a child process that is currently stopped.
    • WSTOPSIG(stat_val)
      If the value of WIFSTOPPED(stat_val) is non-zero, this macro evaluates to the number of the signal that caused the child process to stop.
    • WIFCONTINUED(stat_val)
      Evaluates to a non-zero value if status was returned for a child process that has continued from a job control stop.

    Here is an example of how you would use this information, without having to fork a separate process. Note that you won’t actually receive the signal in the parent process, but you can determine the signal sent to the child process:

    #include <stdlib.h>
    #include <stdio.h>
    
    int main(void)
    {
        while(1)
        {
            int result = system("ls 2>&1 1>/dev/null");
            if (WIFEXITED(result)) {
              printf("Exited normally with status %d\n", WEXITSTATUS(result));
            } else if (WIFSIGNALED(result)) {
              printf("Exited with signal %d\n", WTERMSIG(result));
              exit(1);
            } else {
              printf("Not sure how we exited.\n");
            }
        }
    
        return 0;
    }
    

    And if you run it, you get:

    $ ./sys
    Exited normally with status 0
    Exited normally with status 0
    Exited normally with status 0
    Exited normally with status 0
    Exited normally with status 0
    Exited normally with status 0
    ^CExited with signal 2
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written a program that will etablish a network connection with a remote
I have a website cgi-bin program that is written in c++. Unfortunately the website
I have a console program written in C# that I am using to send
I have a bash script that runs a simulation program written in Fortran 90,
I have written a program that handles signal for Floating point exception and I
I have written a program that reads in a File object (really an XML
I have written a program that prints a table. I have not included the
Question: I have written a console program that uses the SQL server 2005 web
Assume that i have written a program in C++ without using RTTI and run-time
Just wondering what little scripts/programs people here have written that helps one with his

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.