Two very important signals in Linux: SIGALRM signal and Sigchid signal

Source: Internet
Author: User
Tags set time signal handler

When making a blocking system call, you can set timers for these blocking system calls to avoid waiting for the process to fall into an infinite period.        Linux provides alarm system calls and SIGALRM signals to implement this function. To use a timer, first install the SIGALRM signal. If the SIGALRM signal is not installed, the default action is to terminate the current process after the process receives the SIGALRM signal. When the SIGALRM signal is successfully installed, under what circumstances will the process receive the signal? This relies on the timer functionality provided by Linux. Under the Linux system, each process has a unique timer, which provides a timer function in seconds. The process that calls alarm will receive a SIGALRM signal after the timer set time-out is reached. The prototype for the alarm system call is:#include <unistd.h>unsigned int alarm (unsigned int seconds); parameter Description:1) seconds: The timing time to set, in seconds. The SIGALRM signal is triggered when the alarm call succeeds and the time is exceeded. return Value:Returns the number of seconds remaining in the timer that was previously set by the current process.
Example 8-10: programmed to use SIGALRM signals to implement a second timer. The code is as follows:#include <stdio.h>#include <signal.h>//Global counter variableint cnt=0;//SIGALRM Signal Processing functionvoid cbsigalrm (int signo){//Output timing prompt information printf ("Seconds:%d", ++cnt); printf ("\ r"); //Restart Timer for 1-second timing Alarm (1); }void Main (){//install SIGALRM signal if (signal (SIGALRM,CBSIGALRM) ==sig_err) {perror ("signal"); return;}//off row cache mode for standard output setbuf (stdout,null); //Start timer Alarm (1); //process enters an infinite loop and can only be terminated manually while (1) {//pause, wait for signal Pause (); }}
8.5.2 SIGCLD SignalSIGCLD is a very important signal in the multi-process programming of Linux. When a child process exits, it does not immediately release the resource it occupies, but instead notifies its parent process of subsequent work by the parent process. In this process, the following events are generated sequentially. 1) sends the SIGCLD signal to the parent process, and the child process enters the zombie (zombie) state.        2) The parent process receives the SIGCLD signal for processing. If the parent process does not ignore the SIGCLD signal and does not capture the signal for processing during the above process, the child process will enter the zombie state. The zombie state process cannot be called by the operating system, nor does it have any executable code, but it takes up a place in the list of processes. If only a few zombie processes do not affect the operation of the system, but if the zombie process too much, it will seriously affect the operation of the system. Therefore, you should avoid producing zombie processes during the programming process. There are two basic ways to avoid a zombie process: the parent process ignores the SIGCLD signal, and the parent process captures the SIGCLD signal and gets the exit status of the child process in the signal handler function. The way to ignore the signal is simple, just call signal (sigcld,sig_ign)Statement can be completed. If the signal is to be captured and processed, the SIGCLD signal is first installed and then a function called wait or waitpid in the signal processing function gets the exit status of the child process.
Example 8-11: Programming captures the SIGCLD signal, outputting the ID and exit status code of each child process. The code is as follows:#include <stdio.h>#include <stdlib.h>#include <signal.h>//SIGCLD Signal Processing functionvoid cbsigcld (int signo){//Save ID for exit process int pid;//Save exit status code for exit process int status;//wait for any one of the child processes to exit pid=waitpid ( -1,&status,0); //Outputs the child process ID and exit code to exit printf ("Child process%d-exit with status%d\n", Pid,status); }void Main (){ int i,pid;//install SIGCLD signal if (signal (SIGCLD,CBSIGCLD) ==sig_err) {perror ("signal"); return;}//Loop Create child process for (i=0;i<5;i++) {Pid=fork (); //If it is a child process if (pid==0) {//Exit Sub-process, exit status code 0 exit (0); }//If it is a parent process Else{sleep (1); }}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.