当前位置:网站首页>Reentrant function
Reentrant function
2022-04-23 20:46:00 【baboon_ chen】
List of articles
One 、 Global variables are asynchronous IO What problems may be caused ?
The parent-child processes accumulate :
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <stdlib.h>
// Parent-child processes pair n Add up
// every other 1 second , Send signals to each other to inform accumulation
int n = 0, flag = 0;
void sys_err(char *str)
{
perror(str);
exit(EXIT_FAILURE);
}
void deal_sig_child(int signo)
{
printf("I am child %d\t%d\n", getpid(), n);
n += 2;
flag = 1; // Count complete
//sleep(1);
}
void deal_sig_parent(int num)
{
printf("I am parent %d\t%d\n", getpid(), n);
n += 2;
flag = 1; // Count complete
//sleep(1);
}
int main()
{
pid_t pid;
struct sigaction act;
if ((pid = fork()) < 0)
{
sys_err("fork");
}
else if (pid > 0)
{
n = 1;
sleep(1); // Ensure that the action of sub process registration signal can be completed
act.sa_handler = deal_sig_parent;
sigemptyset(&act.sa_mask);
act.sa_flags = 0;
sigaction(SIGUSR2, &act, NULL); // register SIGUSR2 Capture function
// The parent process starts accumulating
deal_sig_parent(0);
while(1)
{
// wait for signal
if (flag == 1)
{
kill(pid, SIGUSR1);
// ---------------- There may be a loss of CPU
flag = 0;
}
}
}
else if (pid == 0)
{
n = 2;
act.sa_handler = deal_sig_child;
sigemptyset(&act.sa_mask);
act.sa_flags = 0;
sigaction(SIGUSR1, &act, NULL); // register SIGUSR1 Capture function
while(1)
{
// wait for signal
if (flag == 1)
{
kill(getppid(), SIGUSR2); // Send to parent process SIGUSR2
flag = 0;
}
}
}
return 0;
}
SIGUSR1: 10, User defined signals . That is, the programmer can define and use the signal in the program . The default action is to terminate the process .
SIGUSR2: 12, Another user-defined signal . That is, the programmer can define and use the signal in the program . The default action is to terminate the process .
If you comment out two places in the code sleep(1), There is a problem :n After accumulating for a while , No more accumulation , The program enters the loop and waits . Here's why :
- Parent process call kill Send a signal to the child process , Notification start count .
- The parent process is executing flag=0 You may lose CPU, wait for CPU.
- And the child process count is complete , Will send a signal to the parent process .
- The parent process gets CPU after , Call the signal processing function first , take flag = 1;
- After the parent process has processed the signal , Restore to breakpoint to continue processing , perform flag = 0,flag The value of is overridden .
- At this time, the child process no longer sends signals to the parent process , Nor can the parent process send signals to the child process , The program is in a dead loop .
resolvent :
- Lock ;
- Don't use global variables , Make the signal processing function reentrant .
Two 、 What is a reentrant function ?
When a function is called and executed ( The call has not ended yet ), Because a certain timing is repeatedly called , be called
Reentrant. According to the method of function implementation, it is divided intoReentrant functionandNon reentrant functionTwo kinds of .
1、 Reentrant function
int main()
{
func(a) {
...
func(a);
...
}
}
int main()
{
func(a);
func(a);
}
// If the top two main The results of function execution are consistent , said func(int a) Is a reentrant function .
Recursive calls and successive calls , Consistent result , Is a reentrant function .
2、 Non reentrant function

obviously ,insert A function is a non reentrant function , Repeated calls to , Can lead to unexpected results . The reason is , It is the internal implementation of the function that uses global variables head.
3、 matters needing attention
Define reentrant functions , A function cannot contain global variables and static Variable , Out of commission malloc、free.
The signal capture function should be designed as a reentrant function .
The reentrant functions that can be called by the signal handler can refer to
man 7 signal.Most of the functions not included in the above list are non reentrant functions . The reason is :
a) Using a static data structure
b) Called malloc or free
c) It's a standard. IO function
版权声明
本文为[baboon_ chen]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204210546350847.html
边栏推荐
- Come in and teach you how to solve the problem of port occupation
- Matlab matrix index problem
- go reflect
- MySQL basic collection
- LeetCode 994、腐烂的橘子
- MySQL stored procedures and functions
- An error occurs when the addressable assets system project is packaged. Runtimedata is null
- Mysql database common sense storage engine
- Queue template code
- Learn to C language fourth day
猜你喜欢

GO語言開發天天生鮮項目第三天 案例-新聞發布系統二

Leetcode 74. Search two-dimensional matrix

浅谈数据库设计之三大范式

Go language development Daily Fresh Project Day 3 Case - Press Release System II

Rt-1052 learning notes - GPIO architecture analysis

常用60类图表使用场景、制作工具推荐

How to configure SSH public key in code cloud

Come in and teach you how to solve the problem of port occupation

Plato farm is one of the four largest online IEOS in metauniverse, and the transaction on the chain is quite high

Commande dos pour la pénétration de l'Intranet
随机推荐
The iswow64process function determines the number of program bits
Syntaxerror: unexpected token r in JSON at position 0
MySQL advanced common functions
Zhongchuang storage | how to choose a useful distributed storage cloud disk
Plato farm is one of the four largest online IEOS in metauniverse, and the transaction on the chain is quite high
深入探究ASP.NET Core读取Request.Body的正确方式
Linux中,MySQL的常用命令
UnhandledPromiseRejectionwarning:CastError: Cast to ObjectId failed for value
Common commands of MySQL in Linux
Bash script learning -- for loop traversal
Syntax Error: TypeError: this. getOptions is not a function
Go limit depth traversal of files in directory
Scripy tutorial - (2) write a simple crawler
Parsing methods of JSON data in C - jar and jobobject: error reading jar from jsonreader Current JsonReader item
Vscode download speed up
How to do after winning the new debt? Is it safe to open an account online
Easy to use nprogress progress bar
PHP的Laravel与Composer部署项目时常见问题
How to configure SSH public key in code cloud
Use of node template engine