Blame manual/examples/sigusr.c

Packit 6c4009
/* Using kill for Communication
Packit 6c4009
   Copyright (C) 1991-2018 Free Software Foundation, Inc.
Packit 6c4009
Packit 6c4009
   This program is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU General Public License
Packit 6c4009
   as published by the Free Software Foundation; either version 2
Packit 6c4009
   of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   This program is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 6c4009
   GNU General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU General Public License
Packit 6c4009
   along with this program; if not, if not, see <http://www.gnu.org/licenses/>.
Packit 6c4009
*/
Packit 6c4009
Packit 6c4009
/*@group*/
Packit 6c4009
#include <signal.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <sys/types.h>
Packit 6c4009
#include <unistd.h>
Packit 6c4009
/*@end group*/
Packit 6c4009
Packit 6c4009
/* When a @code{SIGUSR1} signal arrives, set this variable.  */
Packit 6c4009
volatile sig_atomic_t usr_interrupt = 0;
Packit 6c4009
Packit 6c4009
void
Packit 6c4009
synch_signal (int sig)
Packit 6c4009
{
Packit 6c4009
  usr_interrupt = 1;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/* The child process executes this function. */
Packit 6c4009
void
Packit 6c4009
child_function (void)
Packit 6c4009
{
Packit 6c4009
  /* Perform initialization. */
Packit 6c4009
  printf ("I'm here!!!  My pid is %d.\n", (int) getpid ());
Packit 6c4009
Packit 6c4009
  /* Let parent know you're done. */
Packit 6c4009
  kill (getppid (), SIGUSR1);
Packit 6c4009
Packit 6c4009
  /* Continue with execution. */
Packit 6c4009
  puts ("Bye, now....");
Packit 6c4009
  exit (0);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
int
Packit 6c4009
main (void)
Packit 6c4009
{
Packit 6c4009
  struct sigaction usr_action;
Packit 6c4009
  sigset_t block_mask;
Packit 6c4009
  pid_t child_id;
Packit 6c4009
Packit 6c4009
  /* Establish the signal handler. */
Packit 6c4009
  sigfillset (&block_mask);
Packit 6c4009
  usr_action.sa_handler = synch_signal;
Packit 6c4009
  usr_action.sa_mask = block_mask;
Packit 6c4009
  usr_action.sa_flags = 0;
Packit 6c4009
  sigaction (SIGUSR1, &usr_action, NULL);
Packit 6c4009
Packit 6c4009
  /* Create the child process. */
Packit 6c4009
  child_id = fork ();
Packit 6c4009
  if (child_id == 0)
Packit 6c4009
    child_function ();		/* Does not return.  */
Packit 6c4009
Packit 6c4009
/*@group*/
Packit 6c4009
  /* Busy wait for the child to send a signal. */
Packit 6c4009
  while (!usr_interrupt)
Packit 6c4009
    ;
Packit 6c4009
/*@end group*/
Packit 6c4009
Packit 6c4009
  /* Now continue execution. */
Packit 6c4009
  puts ("That's all, folks!");
Packit 6c4009
Packit 6c4009
  return 0;
Packit 6c4009
}