Example explanation of alarm function in Linux

Example explanation of alarm function in Linux

Introduction to Linux alarm function

Above code:

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <signal.h>
int main(int argc, char *argv[]) 
{ 
 alarm(5);
 sleep(20); 
 printf("end!\n"); 
 return 0; 
}

After running for 5 seconds, the kernel sends a SIGALRM message to the process and the process is terminated. So the result of the above program is:

Alarm clock

Of course, we can also manually define the signal processing function as follows:

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <signal.h>
void sig_alarm(int sig) 
{ 
 printf("sig is %d, sig_alarm is called\n", sig);
}
int main(int argc, char *argv[]) 
{ 
 signal(SIGALRM, sig_alarm); // Register the function corresponding to the alarm signal alarm(5); // After 5 seconds, the kernel sends an alarm signal to the process and executes the corresponding signal registration function sleep(20); 
 printf("end!\n"); 
 return 0; 
}

result:

sig is 14, sig_alarm is called
end!

It can be seen that the kernel sends a SIGALRM signal to the application process and executes the corresponding registration function instead of killing the process.

It’s very simple, that’s all I have to say for now.

Summarize

The above is the full content of this article. I hope that the content of this article will have certain reference learning value for your study or work. Thank you for your support of 123WORDPRESS.COM. If you want to learn more about this, please check out the following links

You may also be interested in:
  • PHP executes 6 Linux command function code examples
  • Detailed explanation of the use of stat function and stat command in Linux
  • How to get the current time using time(NULL) function and localtime() in Linux
  • How to add a timeout to a Python function on Linux/Mac
  • Linux unlink function and how to delete files
  • Detailed explanation of the use of Linux lseek function
  • A brief analysis of the function calling process under the ARM architecture

<<:  Configuring MySQL and Squel Pro on Mac

>>:  vue.config.js packaging optimization configuration

Recommend

Mini Programs enable product attribute selection or specification selection

This article shares the specific code for impleme...

Several ways to remove the dotted box that appears when clicking a link

Here are a few ways to remove it: Add the link dir...

Vue defines private filters and basic usage

The methods and concepts of private filters and g...

Introduction to the use of MySQL performance stress benchmark tool sysbench

Table of contents 1. Introduction to sysbench #Pr...

Implementation of CSS child element selection parent element

Usually a CSS selector selects from top to bottom...

MySQL 5.7.27 installation and configuration method graphic tutorial

The installation tutorial of MySQL 5.7.27 is reco...

Detailed introduction to logs in Linux system

Table of contents 1. Log related services 2. Comm...

Tutorial on installing Ceph distributed storage with yum under Centos7

Table of contents Preface Configure yum source, e...

Tutorial on setting up scheduled tasks to backup the Oracle database under Linux

1. Check the character set of the database The ch...

Detailed explanation of MySQL slow queries

Query mysql operation information show status -- ...

js to achieve waterfall flow layout (infinite loading)

This article example shares the specific code of ...

How to Enable or Disable Linux Services Using chkconfig and systemctl Commands

This is an important (and wonderful) topic for Li...

Detailed explanation on how to get the IP address of a docker container

1. After entering the container cat /etc/hosts It...

In-depth understanding of HTML form input monitoring

Today I saw a blog post about input events, and o...