Quick Navigation Bar files and directories :: process management :: overview [ toc | forums ] |
Note: If the document URL does not begin with https://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.
Notice: Please do not replicate or copy these pages and
host them elsewhere. This is to ensure that the latest version can always
be found here.
Disclaimer: The document author has published these pages
with the hope that it may be useful to others. However, the document
author does not guarantee that all information contained on these
webpages are correct or accurate. There is no warranty, expressed or
implied, of merchantability or fitness for any purpose. The author does
not assume any liability or responsibility for the use of the information
contained on these webpages.
If you see an error, please send an email to the address below indicating
the error. Your feedback is greatly appreciated and will help to
continually improve these pages.