1. Signal Processing: kill (), alarm (), pause () function, alarmpause

Source: Internet
Author: User
Tags sigalarm sleep function

1. Signal Processing: kill (), alarm (), pause () function, alarmpause
Zookeeper

1. How to view the signal: man 7 signal. You can use this command to view all the information.

2. view the signal kill-l

Note that the following 32 signals represent real-time signals.

 

The first 32 types of signals have different names. The last 32 types start with "SIGRTMIN" or "SIGRTMAX". The former is a signal inherited from unix, it is called an unreliable signal (also called a non-real-time signal). The latter changes and expands the signal to solve the problem of "unreliable signal" and forms a reliable signal (also called a real-time signal)

To learn about reliable and unreliable signals, you need to understand the signal lifecycle:

A complete signal cycle can be divided into three important stages. The three important stages are characterized by four important events: signal generation, signal registration in the process, signal cancellation in the process, and signal processing function execution.

The interval between two adjacent events forms a stage of the lifecycle. There are multiple signal processing methods, generally completed by the kernel or by the user process.

3. man 7 signal

 

Signal dispositions

Each signal has a current disposition, which determines how the process

Behaves when it is delivered the signal.

 

The entries in the "Action" column of thetables below specify

Default disposition for each signal, asfollows:

Term Default action is to terminate the process. Indicates terminating the current thread

Ign Default action is to ignore the signal. indicates that the signal is ignored.

Core Default action is to terminate the process and dump core (see

Core (5) indicates that the current process is terminated and Core Dump (Core Dump is used for gdb debugging)

Stop Default action is to stop the process. indicates that the current process is stopped.

Cont Default action is to continue the process if it iscurrently

Stopped. indicates that the previously stopped process continues.

Signal generation type:

Ctrl + C SIGINT

Ctrl + z SIGTSTP

Ctrl + \ SIGQUIT

4. Hardware exception

* Operations except 0

* Access to invalid memory

Int kill (pid_tpid, int sig)

Pid> 0

Sig sends a pid to a process.

Pid <0

Sig sends a signal to a process whose ID is | pid |, And the sending process has the permission to send signals to it.

Pid =-1

When all processes on the system where sig has the permission to send signals to the sender process are 0, it is used for retrieval. If the pid process exists,-1 is returned.

Syntax format of the kill function:

Syntax points of the raise () function (sending signals to itself:

In the following example, the child process does not exit before the parent process calls kill, and then the parent process calls kill to exit the child process:

Running result:

When 24 rows are removed, the running result is:

5. alarm (), pause ()

Function Description

Alarm () is also called an alarm function. It can set a timer in the process. When the timer specifies the time, it sends a SIGALARM signal to the process. Note that a process can only have one alarm time. If an alarm time has been set before calling alarm (), any previous alarm time will be replaced by a new value.

The pause () function is used to suspend the calling process until the signal is captured. This function is often used to determine whether a signal has arrived.

Function Format

In this experiment, a simple sleep () function is completed.

Running result:

Comment 9th rows

Running result:

The above results are obtained with a flash.

Analysis: when the program is scheduled to execute 9th pause (), the process will be suspended. When the timer arrives, the signal SIGALARM will be sent, and then the pause () will capture the signal, the process is directly terminated.

Another example is to use alarm to print the count:


Running result:



Use the alarm () and pause () functions in linux to implement sleep ()

Chapter 3 of <unix advanced programming> describes the sleep function.
Go and check it out.

C language pause () function problems

Perhaps, what we need is an endless loop.
Press Enter to stop pause. Loop, and then pause.

Press Ctrl + C to end the program. (Some systems end with Ctrl + D ).

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.