Linux system Programming signal (二) signal sending function and different precision sleep

Source: Internet
Author: User
Tags signal handler sleep sleep function

First, kill, raise, KILLPG function

int Kill (pid_t pid, int sig);

int raise (int sig);

int killpg (int pgrp, int sig);

The KILL command is implemented by calling the Kill function, which can send a specified signal to a specified process or process group, in which the value of the PID parameter of the KILL function differs to indicate a different meaning, which can be man-specific. The Raise function can send a specified signal to the current process (signal itself to itself). The KILLPG function can signal to a process group. These three functions are successfully returned 0, error return-1. Here is an example of a small program:

/************************************************************************* 
> File name:process_.c
> Author:simba
> Mail:dameng34@163.com
> Created time:sat Feb 2013 02:34:02 PM CST
* * * /
#include <sys/types.h>
# Include<sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <signal.h
#define ERR_EXIT (m) \
Do {\
Perror (m), \
EXIT (exit_failure); \
} while (0)
void handler (int sig);
int main (int argc, char *argv[])
{
if (signal (SIGUSR1, handler) = = Sig_err)
Err_exit ("Signal erro R ");
pid_t pid = fork ();
if (pid = = 1)
Err_exit ("fork Error");
if (PID = = 0)
{
/*
PID = GETPGRP ()///Get process GroupPID
Kill (-pid, SIGUSR1),//Send signal to process group
*/
KILLPG (Getpgrp (), SIGUSR1);
Exit (exit_success); The child process finishes processing the signal before exiting
}
int n = 5;
Do
{
n = the sleep (n);///It will be interrupted by a signal, returning the unslept time
}
while (n > 0);
return 0;
}
Void handler (int sig)
{
printf ("Recv a sig=%d\n", SIG);
}
/* Raise (SIG) is equivalent to Kill (Getpid (), SIG) send a signal to itself */

Registration signal in the program before the fork, so the child process will inherit, in the child process group sent a signal, so the signal processing function will be called two times:

simba@ubuntu:~/documents/code/linux_programming/apue/signal$./kill

Recv a sig=10

Recv a sig=10

simba@ubuntu:~/documents/code/linux_programming/apue/signal$

Because the sleep function is interrupted by the signal processing function (return Value:zero If the requested time has elapsed, or the number of seconds left to sleep, if the Call is interrupted by a signal handler, if we want it to sleep enough 5s, you can use a while loop to determine its return value. Note here is that the output of two times after the recv to continue to sleep is not necessarily the time, it may be 5s, that is, the signal processing function is called sleep before the call (the child process is first executed by the system), sleeping is not interrupted. It also shows a point: as long as the signal, signal processing function can be called at any time, not only in the process of active call sleep, pause and other functions (let the CPU to run other programs), the CPU has been in the process of scheduling, user space and kernel space to switch, The signal recorded in the PCB is processed at a time before the user space code returning from the kernel to the process continues to execute.

Related Article

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.