|
Quick Navigation Bar files and directories :: process management :: overview [ toc | forums ] |
Note: If the document URL does not begin with http://randu.org/tutorials/perl/ then you are viewing a copy. Please direct your browser to the correct location for the most recent version. |
system
function. What if we wanted to get the current time and add who is
currently logged into the system?
system("date"); # pretty useless by itself, what if...
$filename = "who.out";
system "(date; who) >$filename &";
This would run the date command followed by the who command and redirect
standard output to who.out. A sample output:
Wed Aug 1 13:24:41 EDT 2001 username pts/0 Aug 1 12:10
@gccoptions = ("-ansi", "-pedantic", "-Wall", "-O2");
system "gcc -o binexe @options @cfiles"; # shell used
system "gcc", "-o", "binexe", @options, @cfiles; # shell avoided
$now = "time now is ".`date`;
open(WHOPROC, "who|"); @whosaid = <WHOPROC>;Note the vertical bar on the right side of who. If a command expects input, we could (you guessed it) put the vertical bar on the left side of who:
open(LPR, "|lpr -pqueue"); # CUPS print to queue print LPR @somedata; close(LPR);
fork()
if (!defined($kidpid = fork())) {
# fork returned undef, so failed
die "Cannot fork: $!";
} elsif ($pid == 0) {
# fork returned 0, so this branch is child
exec("date");
# if exec fails, fall through to the next statement
die "can't exec date: $!";
} else {
# fork returned 0 nor undef
# so this branch is parent
waitpid($kidpid, 0);
}
Of course, you would never fork a date process because that's just
dumb. But for illustrative purposes, it gets the job done. If you
do not understand what is happening, you should consult the
fork(2) and exec(2) system calls and Schwartz/Wall. This is somewhat
of a more "advanced" topic.
kill('INT', 234, 237);
Would send SIGINT to processes 234 and 237.
