Blame manual/examples/sigusr.c

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