当前位置:网站首页>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 :
Iffork
Process 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
fork
Used in the child process afterexec
Family of functions , You can load and run other programs ( The child process replaces the original process , Do something different from the parent process ).exec
The 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
exec
There are two main cases of function families :
When a process thinks it can no longer contribute to the system and users , You can call anyexec
The 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
边栏推荐
- Interface test framework practice (I) | requests and interface request construction
- 速看!2022年6月编程语言排行榜出炉!第一名太牛啦
- [the path of system analyst] Chapter 6: duplicate demand engineering (case paper)
- Learn the most basic operation of discodiffusion
- Reporting Classes中uvm_report_server的get_severity_count和get_server用法
- Oracle/PLSQL: Trim Function
- Oracle/PLSQL: Length Function
- Oracle/PLSQL: NumToDSInterval Function
- SystemVerilog仿真速率提升
- Some exception handling for idea plug-in development
猜你喜欢
ThreadLocal详解
The listing of Fuyuan pharmaceutical is imminent: the net amount raised will reach 1.6 billion yuan, and hubaifan is the actual controller
Two days of beautiful butterfly animation
Tsinghua & Zhiyuan | cogview2: faster and better text image generation model
你的case真的pass了吗?
消费者追捧iPhone,在于它的性价比超越国产手机
h5液体动画js特效代码
使用命令行安装达梦数据库
二叉樹oj題目
速看!2022年6月编程语言排行榜出炉!第一名太牛啦
随机推荐
Consumers pursue the iPhone because its cost performance exceeds that of domestic mobile phones
JVM 的指针压缩
做了两天的唯美蝴蝶动画
Oracle/PLSQL: To_ Clob Function
Oracle/PLSQL: Length Function
markdown表格(合并)
你的case真的pass了吗?
Memcached basics 15
Why divide the training set and the test set before normalization?
numpy 数组运算机制浅探
Oracle/PLSQL: Length Function
两个页面之间传参方法
p5.js死亡星球
Simply learn the entry-level concepts of googlecolab
memcached基础10
[the path of system analyst] Chapter 6: duplicate demand engineering (case paper)
Break through the performance bottleneck of image recognition through rust language computing acceleration technology
Oracle/PLSQL: VSize Function
Oracle/PLSQL: NumToYMInterval Function
D's appendto packaging