Linux signal processing function

Source: Internet
Author: User
Article Title: linux signal processing function. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.

1. alarm)

Related functions: signal, sleep

Header file # include

Define the function unsigned int alarm (unsigned int seconds );

Function Description alarm () is used to set the signal SIGALRM to be sent to the current process after the specified number of seconds after the seconds parameter. If the seconds parameter is 0, the previously set alarm is canceled and the remaining time is returned.

Return the remaining seconds of the previous alarm. If no alarm is set, return 0.

Example # include

# Include

Void handler (){

Printf ("hello \ n ");

}

Main ()

{

Int I;

Signal (SIGALRM, handler );

Alarm (5 );

For (I = 1; I <7; I ++ ){

Printf ("sleep % d... \ n", I );

Sleep (1 );

}

}

Run sleep 1...

Sleep 2...

Sleep 3...

Sleep 4...

Sleep 5...

Hello

Sleep 6...

Ii. kill (send a signal to a specified process)

Related functions: raise, signal

Header file # include

# Include

Define the int kill (pid_t pid, int sig) function );

Function Description: kill () can be used to send signals specified by the sig parameter to the process specified by the pid parameter. The pid parameter can be used in the following situations:

Pid> 0: send the signal to the process with the pid ID.

Pid = 0 send the signal to all processes in the same process group as the current process

Pid =-1 broadcasts signals to all processes in the system

Pid <0 transmits the signal to all processes whose process ID is the absolute value of pid

For the signal numbers represented by the sig parameter, see Appendix D.

If the return value is successfully executed, 0 is returned. If an error exists,-1 is returned.

The error code EINVAL parameter sig is invalid.

The process or process group specified by the ESRCH Parameter pid does not exist.

The EPERM permission is insufficient to send signals to the specified process.

Example # include

# Include

# Include

# Include

Main ()

{

Pid_t pid;

Int status;

If (! (Pid = fork ())){

Printf ("Hi I am child process! \ N ");

Sleep (10 );

Return;

}

Else {

Printf ("send signal to child process (% d) \ n", pid );

Sleep (1 );

Kill (pid, SIGABRT );

Wait (& status );

If (WIFSIGNALED (status ))

Printf ("chile process receive signal % d \ n", WTERMSIG (status ));

}

}

Execute sen signal to child process (3170)

Hi I am child process!

Child process receive signal 6

3. pause (pause the process until the signal appears)

Related functions: kill, signal, sleep

Header file # include

Define the int pause (void) function );

Function Description: pause () will suspend the current process (into sleep state) until it is interrupted by the signal.

The returned value is-1.

The error code EINTR indicates that the function is interrupted when a signal arrives.

Sigaction (query or set the signal processing method)

Related functions: signal, sigprocmask, sigpending, sigsuspend

Header file # include

Define the int sigaction (int signum, const struct sigaction * act, struct sigaction * oldact) function );

Function Description: sigaction () sets the signal processing function based on the signal number specified by signum. The signum parameter can specify all signals other than SIGKILL and SIGSTOP.

For example, the parameter structure sigaction is defined as follows:

Struct sigaction

{

Void (* sa_handler) (int );

Sigset_t sa_mask;

Int sa_flags;

Void (* sa_restorer) (void );

}

Sa_handler this parameter is the same as the signal () parameter handler, representing a new signal processing function. For other meanings, see signal ().

Sa_mask is used to set the signal specified by sa_mask to be temporarily shelved when processing this signal.

Sa_restorer is not used.

Sa_flags is used to set other operations related to signal processing. The following values are available.

OR operation (|) Combination

A_NOCLDSTOP: If the signum parameter is SIGCHLD, the parent process is not notified when the child process is paused.

SA_ONESHOT/SA_RESETHAND: Before calling a new signal processing function, change the Signal Processing Method to the system preset method.

SA_RESTART: system calls that are interrupted by signals will be restarted on their own.

SA_NOMASK/SA_NODEFER: Ignore the re-arrival of the signal before processing the signal.

If the oldact parameter is not a NULL pointer, the original signal processing method will be returned from the sigaction structure.

If the return value is successfully executed, 0 is returned. If an error exists,-1 is returned.

The error code EINVAL parameter signum is invalid, or an attempt to intercept the SIGKILL/SIGSTOPSIGKILL Signal

The EFAULT parameter act and the oldact pointer address cannot be accessed.

EINTR this call is interrupted

[1] [2] Next page

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.