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

MySQL 5.7.17 installation and use graphic tutorial

MySQL is a relational database management system ...

Detailed explanation of the process of modifying Nginx files in centos7 docker

1. Install nginx in docker: It is very simple to ...

Detailed instructions for installing SuPHP on CentOS 7.2

By default, PHP on CentOS 7 runs as apache or nob...

Why the disk space is not released after deleting data in MySQL

Table of contents Problem Description Solution Pr...

How to install Docker and configure Alibaba Cloud Image Accelerator

Docker Installation There is no need to talk abou...

The difference and use of json.stringify() and json.parse()

1. Differences between JSON.stringify() and JSON....

How are spaces represented in HTML (what do they mean)?

In web development, you often encounter characters...

Detailed analysis of mysql MDL metadata lock

Preface: When you execute a SQL statement in MySQ...

How to solve the front-end cross-domain problem using Nginx proxy

Preface Nginx (pronounced "engine X") i...

Advertising skills in the Baidu Union environment (graphic tutorial)

Recently, students from the User Experience Team o...

How to install and deploy ftp image server in linux

Refer to the tutorial on setting up FTP server in...

Some tips on deep optimization to improve website access speed

Some tips for deep optimization to improve websit...

In-depth understanding of MySQL master-slave replication thread state transition

Preface The basic principle of MySQL master-slave...