33803

SIGSTOP not working in c program

Question:

I am trying to write a C program in which the parent process suspends the child process and after a few seconds continues executing it.

#include <stdio.h> #include <signal.h> #include <stdlib.h> #include <unistd.h> void sigstop(); void sigcont(); void sigquit(); int main() { int pid; if ((pid = fork()) < 0) { perror("fork"); exit(1); } if (pid == 0) { /* child */ signal(SIGSTOP,sigstop); signal(SIGCONT,sigcont); signal(SIGQUIT, sigquit); for(;;); /* loop for ever */ } else { printf("\nPARENT: sending SIGSTOP to suspend the process\n\n"); kill(pid, SIGSTOP); sleep(5); printf("\nPARENT: sending SIGCONT to continue the suspended process\n\n"); kill(pid, SIGCONT); sleep(5); printf("killing child"); kill(pid,SIGQUIT); sleep(5); } } void sigstop() { printf("CHILD: I've been suspended\n"); } void sigcont() { printf("CHILD: I'm back\n"); } void sigquit() { printf("My DADDY has Killed me!!!\n"); exit(0); }

printf inside sigstop() never gets executed and sigquit() gets called before printf("killing child");. How does this happen and how can I get output in proper order ?

Answer1:

If you <a href="http://man7.org/linux/man-pages/man7/signal.7.html" rel="nofollow">read the signal(7) manual page</a> you will see that

<blockquote>

The signals SIGKILL and SIGSTOP cannot be caught, blocked, or ignored.

</blockquote>

You simply can not catch that signal.

<hr />

The last bit, about "Killing child" get printed in the wrong order is very easy to fix: Add a trailing newline to the string you print.

This is because output to stdout (which is what printf writes to) is by default <em>line-buffered</em>. This means that the internal buffer of stdout is <em>flushed</em> (i.e. actually written to the terminal) on a newline. If you don't have the newline, then the buffer will be flushed when the process exits, which is after you exit the child process.

Recommend

  • C- what exactly is SIGUSR1 syntactically
  • execvp in C not going through ar
  • Is there a version of GNU indent which supports Objective C?
  • Not all data coming back from mysql query
  • angular 2 translation json observable does not work
  • A more efficient way than: execlp(cmd[0], cmd[0], cmd[1] cmd[2], cmd[3] NULL);
  • Implementing input/output redirection in a Linux shell using C
  • GeckoFX WebBrowser page height and width
  • Random number between forked processes is the same
  • How to connect from Linux to Android Bluetooth socket
  • How to limit the number of actors of a particular type?
  • Switching role between fork and parent
  • Substitute a package in NPM/Node
  • How do I printf() after a call to execlp() in a child process?
  • Why can I not read from stdin in this forked process?
  • How do you increment a count while using the sleep function?
  • ETXTBSY and how to override it
  • SecurityException Permission Denial MediaProvider READ_EXTERNAL_STORAGE
  • Using Node cluster module with SailsJs: EADDRINUSE
  • Associate git repo with existing fork network
  • Is there a way to get the process ID of a console program I've just started in the background?
  • Find VMID for running instance
  • rewrite uppercase url to lowercase url htaccess
  • Ubuntu and bcrypt
  • Using JRuby with Rails 3.2
  • Code in Job's Script Block after Start-Process Does not Execute
  • Mysterious problem with floating point in LISP - time axis generation
  • How to know when stdin is empty if it contains EOF?
  • Obtain ObjectIdHex value from mgo query
  • output of program is not same as passed argument
  • Can I have the cursor start on a particular column by default in jqgrid's edit mode?
  • Timeout for blocking function call, i.e., how to stop waiting for user input after X seconds?
  • Akka Routing: Reply's send to router ends up as dead letters
  • How get height of the a view with gone visibility and height defined as wrap_content in xml?
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • unknown Exception android
  • Checking variable from a different class in C#
  • Sorting a 2D array using the second column C++
  • Reading document lines to the user (python)
  • java string with new operator and a literal