当前位置:网站首页>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
边栏推荐
猜你喜欢

为什么传递SPIF_SENDCHANGE标志SystemParametersInfo会挂起?

idea 插件开发一些异常处理

理想L9产品力分析:售价45.98万,采用四缸发动机,续航1315公里

Weibo comments on high performance and high availability architecture

Amazon elasticache quickly builds a cache service cluster, which is fast

【毕业季】角色转换
在 IDEA 里看个书很过分嘛!

热议:月薪1.8万却毫无意义的工作,你干吗?

浏览器缓存

Systematic analysis of social networks using Networkx: Facebook network analysis case
随机推荐
TopoLVM: 基于LVM的Kubernetes本地持久化方案,容量感知,动态创建PV,轻松使用本地磁盘
Oracle/PLSQL: HexToRaw Function
UVM in UVM_ config_ Setting and obtaining DB non-linear
在 IDEA 里看个书很过分嘛!
浏览器缓存
Memcached basics 15
h5液体动画js特效代码
每日刷题记录 (五)
UVM中uvm_config_db在sequence中的使用
getReader() has already been called for this request
Sample development of WiFi IOT Hongmeng development kit
Oracle/PLSQL: HexToRaw Function
memcached基础13
On the operation mechanism of numpy array
I encountered some problems when connecting to the database. How can I solve them?
Break through the performance bottleneck of image recognition through rust language computing acceleration technology
Interface isolation principle
Oracle/PLSQL: Trim Function
Oracle/PLSQL: CharToRowid Function
markdown表格(合并)