当前位置:网站首页>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 function
andNon reentrant function
Two 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
边栏推荐
- 中创存储|想要一个好用的分布式存储云盘,到底该怎么选
- 【SQL】字符串系列2:将一个字符串根据特定字符分拆成多行
- Zhongchuang storage | how to choose a useful distributed storage cloud disk
- Singleton mode
- Leetcode 542, 01 matrix
- DOS command of Intranet penetration
- [SQL] string series 2: split a string into multiple lines according to specific characters
- "Meta function" of tidb 6.0: what is placement rules in SQL?
- Selenium 显示等待WebDriverWait
- 笔记本电脑卡顿怎么办?教你一键重装系统让电脑“复活”
猜你喜欢
[PTA] l1-002 printing hourglass
[matlab 2016 use mex command to find editor visual studio 2019]
Flex layout
电脑越用越慢怎么办?文件误删除恢复方法
Identifier CV is not defined in opencv4_ CAP_ PROP_ FPS; CV_ CAP_ PROP_ FRAME_ COUNT; CV_ CAP_ PROP_ POS_ Frames problem
MySQL进阶之表的增删改查
Install MySQL 5.0 under Linux 64bit 6 - the root password cannot be modified
Vscode download speed up
Leetcode 74. Search two-dimensional matrix
DOS command of Intranet penetration
随机推荐
Matlab: psychtoolbox installation
Come in and teach you how to solve the problem of port occupation
一些接地气的话儿
pikachuxss如何获取cookie靶场,返回首页总是失败
Syntax Error: TypeError: this. getOptions is not a function
LeetCode 116. 填充每个节点的下一个右侧节点指针
又一款数据分析神器:Polars 真的很强大
[PTA] l1-006 continuity factor
41. The first missing positive number
Is qiniu school useful and is the recommended securities account safe
C migration project record: modify namespace and folder name
2021-09-02 unity project uses rider to build hot change project failure record of ilruntime
laravel 发送邮件
Summary and effect analysis of methods for calculating binocular parallax
Vscode download speed up
Use of node template engine
MySQL基础之写表(创建表)
go defer
3-5 obtaining cookies through XSS and the use of XSS background management system
MySQL进阶之常用函数