Documente Academic
Documente Profesional
Documente Cultură
PROCESS MANAGEMENT
C. Prayline Rajabai
Assistant Professor
SENSE
VIT University
DIRECTORY ACCESS - INTRODUCTION
There are standard functions in perl to play with directories.
Syntax Meaning
opendir DIRHANDLE, EXPR To open a directory
2
EXAMPLES
Program to print all the files in a directory
3
EXAMPLES
Program to print all the files in a directory
4
DIRECTORY ACCESS COMMANDS
We use mkdir function to create a new directory.
Example :
$dir = "/tmp/perl";
# This creates perl directory in /tmp directory.
mkdir( $dir ) or die "Couldn't create $dir directory, $!";
print "Directory created successfully\n";
chdir($dir);
A file can be renamed with the new file name using the
rename function.
You can also use the rename function to move a file from one
directory to the other
6
PROCESS MANAGEMENT - INTRODUCTION
Perl has a feature to create new processes as per your
requirements
The exit() function exits only from the child process which
executes this function.
7
BACKSTICK OPERATOR
Any Unix command can be executed in a perl program by
using the backstick operator.
Example :
#!/usr/bin/perl
@files = `ls -l`;
foreach $file (@files){
print $file\n;
}
1;
8
BACKSTICK OPERATOR CONTD.,
The above program lists down all the files and directories in
the current directory.
Output :
drwxr-xr-x 3 root root 4096 Sep 14 06:46 9-14
drwxr-xr-x 4 root root 4096 Sep 13 07:54 android
-rw-r--r-- 1 root root 574 Sep 17 15:16 index.htm
drwxr-xr-x 3 544 401 4096 Jul 6 16:49 MIME-Lite-3.01
-rw-r--r-- 1 root root 71 Sep 17 15:16 test.pl
drwx------ 2 root root 4096 Sep 17 15:11 vAtrJdy
9
SYSTEM() FUNCTION
system() function is also used to execute any Unix command.
Example :
unless ($pid) {
exec "date";
waitpid($pid, 0);
12
FORK() FUNCTION CONTD.,
13
KILL() FUNCTION
kill('KILL', (Process List)) function can be used to
terminate a pseudo-process by passing it the ID
returned by fork().
Example : To send SIG INT to the process IDs 104 and 102
kill('INT', 104, 102); 14
1;
DEFINED FUNCTION - EXAMPLE
Example:
$var1 = "This is defined";
if( defined($var1) ){
print "$var1\n";
}
if( defined($var2) ){
print "var2 is also defined\n";
}else{
print "var2 is not defined\n";
}