当前位置:网站首页>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 :
Use fork() 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 :
If fork Process failed , Will return undef; 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 after exec 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 any exec 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

原网站

版权声明
本文为[Poisonous egg]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/178/202206270128236909.html