当前位置:网站首页>Fork (), exec (), waitpid (), $? > > in Perl 8 combination
Fork (), exec (), waitpid (), $? > > in Perl 8 combination
2022-06-27 01:48:00 【Poisonous egg】
perl In language fork、exec、waitpid 、 $? >> 8 Combine
fork()
describe :
Usefork()The system call spawns a new process . Any shared socket or file handle is copied between processes . You must make sure to wait for your child process , To prevent the formation of “ Corpse ” process .
That is to say ,fork()The function creates a connection with the original process through a system call ( The parent process ) Almost exactly the same process ; These two processes do exactly the same thing .
explain :
Child process is a copy of parent process , It will get the parent process data space 、 Pile up 、 Copies of resources such as stacks .
Be careful , The child process holds the above storage space “ copy ”, This means that the parent and child processes do not share this storage space .
linux Copy the address space content of the parent process to the child process , therefore , Subprocesses consist of independent address spaces .
Return value :
IfforkProcess failed , Will returnundef; If it works , Will return the child process ID Parent process ; If it works , Will return the child process ID.
Example :
$pid = fork();
if( $pid == 0 ) {
print "This is child process\n";
print "Child process is existing\n";
exit 0;
}
print "This is parent process and child ID is $pid\n";
print "Parent process is existing\n";
exit 0;
result :
This is parent process and child ID is 19678
Parent process is existing
This is child process
Child process is existing
exec()
stay
forkUsed in the child process afterexecFamily of functions , You can load and run other programs ( The child process replaces the original process , Do something different from the parent process ).execThe function family can find the executable file according to the specified file name or directory name , And use it to replace the data segment of the original calling process 、 Code and stack segments . After execution , The content of the original calling process is in addition to the process number , Everything else has been replaced by the content of the new program . in addition , The executable file here can be binary file , It can also be Linux Any executable script file under .
stay Linux Use in
execThere are two main cases of function families :
When a process thinks it can no longer contribute to the system and users , You can call anyexecThe function family reborn itself ;
If a process wants to execute another program , Then it can call fork Function to create a new process , Then call any one. exec Function to regenerate a child process ;
waitpid()
** Zombie process :** When a subprocess exits , The parent process has not ( Use wait or waitpid) When receiving its exit state , The subprocess becomes the zombie process
** Orphan process :** When the subprocess is still running , The parent process exits first , The child process will be orphaned pid=1 Of init/systemd Process adoption
It should be noted that , After the zombie process's parent process dies , The zombie process will also be pid=1 Of init/systemd Process adoption , and init/systemd The process will periodically clean up the zombie processes under it , And check whether there are any zombie processes in its territory when any of its subprocesses exit , To ensure that init/systemd There won't be too many zombie processes
$? >> 8
$?Last time the pipe was closed , Reverse tick (``) Order or wait,waitpid, perhaps system function The state of return .
Note that it's not just a simple exit code , But from the lower level wait(2) perhaps waitpid(2) The complete... Returned by the system call 16 Bit state .
therefore , The exit value of the subprocess is high , That is to say$? >> 8
Use a combination of
Log::Log4perl::init(\q( log4perl.rootLogger = INFO, Screen log4perl.appender.Screen = Log::Log4perl::Appender::Screen log4perl.appender.Screen.layout = PatternLayout log4perl.appender.Screen.layout.ConversionPattern = %d - %H - %F:%L - %p - %m{chomp}%n ));
my $logger = Log::Log4perl->get_logger();
my $pid = fork();
if ($pid == 0){
exec($cmd_line);
}
system("echo $pid > ${work_dir}/pid.txt");
waitpid($pid, 0);
my $status = $? >> 8;
$logger->info(" finished and exit code is: $status");
Reference resources
https://www.jc2182.com/perl/perl-fork-func.html
https://blog.csdn.net/holden_liu/article/details/100174792
https://blog.csdn.net/zjwson/article/details/53337212
边栏推荐
- Why divide the training set and the test set before normalization?
- Memcached basics 13
- dat.gui.js星星圆圈轨迹动画js特效
- Sample development of WiFi IOT Hongmeng development kit
- svg拖拽装扮Kitty猫
- Oracle/PLSQL: Soundex Function
- Summary of config mechanism and methods in UVM (1)
- 达梦数据库的卸载
- Oracle/PLSQL: Soundex Function
- 为什么传递SPIF_SENDCHANGE标志SystemParametersInfo会挂起?
猜你喜欢

Browser cache

Some exception handling for idea plug-in development
Reading a book in idea is too much!

Continuous delivery blue ocean application

C语言--职工信息管理系统设计

Meituan: data management and pit avoidance strategy summarized after stepping on Thunder for several years

简单学习GoogleColab的入门级概念
![[graduation season] role conversion](/img/4e/aa763455da974d9576a31568fc6625.jpg)
[graduation season] role conversion

Tsinghua & Zhiyuan | cogview2: faster and better text image generation model

Amazon elasticache quickly builds a cache service cluster, which is fast
随机推荐
二叉树oj题目
Oracle/PLSQL: Rtrim Function
XSS笔记(下)
在连接数据库的时候遇到了点问题,请问怎么解决呀?
为什么先划分训练集和测试集后归一化?
理想L9产品力分析:售价45.98万,采用四缸发动机,续航1315公里
Due to the invalidation of the prospectus of bori technology, CICC has stopped providing guidance to it and abandoned the listing on the Hong Kong stock exchange?
Oracle/PLSQL: Cast Function
Oracle/PLSQL: Replace Function
Oracle/PLSQL: Substr Function
three.js多米诺骨牌js特效
[the path of system analyst] Chapter 6: duplicate demand engineering (case paper)
perl语言中 fork()、exec()、waitpid() 、 $? >> 8 组合
Sample development of WiFi IOT Hongmeng development kit
memcached基础11
Memcached foundations 12
Oracle/PLSQL: Lpad Function
BS-GX-016基于SSM实现教材管理系统
简单学习GoogleColab的入门级概念
Oracle/PLSQL: HexToRaw Function