When doing fork() using Perl, a child process created and parent process are writing to the same log file, and because of this, the script is getting stuck in windows.
I want child process to write in a different log file, and parent process to write in a different log file so that issue of getting stuck because of two processes writing to the same file at the same time doesn't arise.
Script code snippet:
print "I am parent process";
my $var2=&run_sleep();
print " I am out of subroutine";
print "I want to write in different log file";
sub run_sleep {
print "Before fork, parent ID === script ID, i.e $scriptPid \n";
my $pid = fork();
return $pid if $pid; # returns to the parent process(out of function)
print "Running child process\n"; # Proceeds for the child process
print "I am child";
print "PID of child process is $pid\n";
&function_2();
&function_3();
print "I have finished all functions; ending child";
}
Here, I want all the child process's print statements (after the fork) and all the statements(functions) executed by a child should log in a different file, and parent process logs should be in a different log file. [For Windows]
For Linux, I tried the "select" function as follows, and it worked. But, in windows, it's not working.
use feature qw/say/;
use autodie;
# copy STDOUT to another filehandle
open (my $STDOLD, '>&', STDOUT);
# redirect STDOUT to log.txt
open (STDOUT, '>>', 'log.txt');
say 'This should be logged.';
# restore STDOUT
open (STDOUT, '>&', $STDOLD);
say 'This should show in the terminal';