Topic 3:
Write a program to implement the countdown Exit function, when the process receives the SIGUSR1 signal to start the countdown, 3 seconds after exiting the program.
The process of requiring output from the terminal to receive a signal and start the countdown
Such as:
The process is terminating in 3 sec ...
The process is terminating in 2 sec ...
The process is terminating in 1 sec ...
1#include <sys/types.h>2#include <unistd.h>3#include <signal.h>4#include <stdlib.h>5#include <string.h>6#include <stdio.h>7#include <errno.h>8#include <signal.h>9 Ten intG_tag; One A voidMyhandle (intnum) - { -printf"recv signal ID num:%d \ n", num); the if(num = =Ten) - { -G_tag =0; - } + } - + intMainvoid) A { at -G_tag =1; - - - if(Signal (SIGUSR1, myhandle) = =Sig_err) - { inPerror ("Signal Err"); - return 0; to } + - while(1) the { * if(G_tag = =0) $ {Panax Notoginsengprintf"The process is terminating in 3 sec ... \ n"); -Sleep1); theprintf"The process is terminating in 2 sec ... \ n"); +Sleep1); Aprintf"The process is terminating in 1 sec ... \ n"); theSleep1); + Break; - } $ } $ - - return 0; the}
"Linux Learning" Countdown exit, when the process received SIGUSR1 signal start Countdown, 3 seconds after exiting the program.