0% found this document useful (0 votes)
43 views43 pages

OS Tutorial: BITS Pilani BITS Pilani

The document provides an overview of common system calls used in processes on UNIX/Linux systems. It discusses the fork(), wait(), exit(), and sleep() system calls. fork() creates a new process, wait() pauses a parent process until a child process terminates, exit() terminates a process and returns a status code, and sleep() temporarily suspends a process for a specified time. Examples are given of how each system call works.

Uploaded by

Kevin Pinto
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views43 pages

OS Tutorial: BITS Pilani BITS Pilani

The document provides an overview of common system calls used in processes on UNIX/Linux systems. It discusses the fork(), wait(), exit(), and sleep() system calls. fork() creates a new process, wait() pauses a parent process until a child process terminates, exit() terminates a process and returns a status code, and sleep() temporarily suspends a process for a specified time. Examples are given of how each system call works.

Uploaded by

Kevin Pinto
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 43

OS Tutorial

BITS Pilani
Pilani Campus
BITS Pilani
Pilani Campus

S. H. Islam
hafizul@pilani.bits-pilani.ac.in

Room No. 6120 K (NAB)


System calls: Process
• fork()
• wait()
• exit()
• sleep()
• exec family

3
BITS Pilani, Pilani Campus
fork()

BITS Pilani, Pilani Campus


fork()

BITS Pilani, Pilani Campus


wait()
• A process (parent) may wait on another
process (child) to complete its execution.
• The parent process may then issue a wait()
system call, which suspends the execution of
the parent process while the child executes.
• When the child process terminates, it returns
an exit status to the OS, which is then
returned to the waiting parent process.
• The parent process then resumes execution.
6
BITS Pilani, Pilani Campus
wait()
• pid_t wait(int *status)
• status is a pointer to an integer where the
UNIX system stores the value returned by the
child process.

7
BITS Pilani, Pilani Campus
wait()
• wait() will force a parent process to wait for a
child process to stop or terminate.
• A call to this function causes the parent
process to wait until one of its child processes
exits.
• The wait() returns the process id of the child
process, which gives the parent the ability to
wait for a particular child process to finish.
• It return -1 for an error.
8
BITS Pilani, Pilani Campus
#include <stdio.h> #include <sys/wait.h> /* contains prototype for wait */
int main(void)
{
int pid;
int status;
printf("Hello World!\n");
pid = fork( );
if (pid == -1) /* check for error in fork */
{
printf(“error");
exit(1);
}
if (pid == 0)
printf("I am the child process.\n");
else
{
wait(&status); /* parent waits for child to finish */
printf("I am the parent process.\n");
}
} 9
BITS Pilani, Pilani Campus
#define MAX _COUNT 20 void ChildProcess(void)
void ChildProcess(void); {
void ParentProcess(void); int i;
void main(void) for (i = 1; i <= MAX_COUNT; i++)
{ printf("This line is from child, value =
pid_t pid; %d\n", i);
pid = fork(); printf("*** Child process is done
if (pid == 0) ***\n");
ChildProcess(); }
else void ParentProcess(void)
ParentProcess(); {
} int i;
for (i = 1; i <= MAX_COUNT; i++)
printf("This line is from parent, value
= %d\n", i);
printf("*** Parent is done ***\n");
} 10
BITS Pilani, Pilani Campus
This line is from child, value = 1
This line is from parent, value = 1 This line is from child, value = 4
This line is from parent, value = 2 This line is from child, value = 5
This line is from parent, value = 3 This line is from child, value = 6
This line is from parent, value = 4 This line is from parent, value = 20
This line is from parent, value = 5 *** Parent is done ***
This line is from parent, value = 6 This line is from child, value = 7
This line is from parent, value = 7 This line is from child, value = 8
This line is from parent, value = 8 This line is from child, value = 9
This line is from parent, value = 9 This line is from child, value = 10
This line is from parent, value = 10 This line is from child, value = 11
This line is from parent, value = 11 This line is from child, value = 12
This line is from parent, value = 12 This line is from child, value = 13
This line is from child, value = 2 This line is from child, value = 14
This line is from parent, value = 13 This line is from child, value = 15
This line is from parent, value =14 This line is from child, value = 16
This line is from parent, value = 15 This line is from child, value = 17
This line is from parent, value = 16 This line is from child, value = 18
This line is from parent, value = 17 This line is from child, value = 19
This line is from child, value = 3 This line is from child, value = 20
This line is from parent, value = 18 *** Child process is done ***
This line is from parent, value = 19
11
BITS Pilani, Pilani Campus
#define MAX _COUNT 20 void ChildProcess(void)
void ChildProcess(void); {
int i;
void ParentProcess(void);
for (i = 1; i <= MAX_COUNT; i++)
void main(void)
printf("This line is from child, value =
{
%d\n", i);
pid_t pid;
printf("*** Child process is done
pid = fork(); ***\n");
if (pid == 0) }
ChildProcess(); void ParentProcess(void)
else {
ParentProcess(); int i;
} int status;
wait(&status);
for (i = 1; i <= MAX_COUNT; i++)
printf("This line is from parent, value =
%d\n", i);
printf("*** Parent is done ***\n");
12
} BITS Pilani, Pilani Campus
This line is from child, value = 1 This line is from parent, value = 1
This line is from child, value = 2 This line is from parent, value = 2
This line is from child, value = 3 This line is from parent, value = 3
This line is from child, value = 4 This line is from parent, value = 4
This line is from child, value = 5 This line is from parent, value = 5
This line is from child, value = 6 This line is from parent, value = 6
This line is from child, value = 7 This line is from parent, value = 7
This line is from child, value = 8 This line is from parent, value = 8
This line is from child, value = 9 This line is from parent, value = 9
This line is from child, value = 10 This line is from parent, value = 10
This line is from child, value = 11 This line is from parent, value = 11
This line is from child, value = 12 This line is from parent, value = 12
This line is from child, value = 13 This line is from parent, value = 13
This line is from child, value = 14 This line is from parent, value = 14
This line is from child, value = 15 This line is from parent, value = 15
This line is from child, value = 16 This line is from parent, value = 16
This line is from child, value = 17 This line is from parent, value = 17
This line is from child, value = 18 This line is from parent, value = 18
This line is from child, value = 19 This line is from parent, value = 19
This line is from child, value = 20 This line is from parent, value = 20
*** Child process is done *** *** Parent is done *** 13
BITS Pilani, Pilani Campus
exit()
• void exit(int status)
• where status is an integer between 0 and
255. This number is returned to the parent
process via wait() as the exit status of the
process.

14
BITS Pilani, Pilani Campus
exit()
• exit() terminates the process which calls this
function and returns the exit status value.
• By convention, a status of 0 means normal
termination. Any other value indicates an
error.

15
BITS Pilani, Pilani Campus
exit()
• Many standard library calls have errors
defined in the sys/stat.h header file.
• The exit status/return code of a process
passed from a child process to a parent
process when it has finished executing a
specific procedure or delegated task.

16
BITS Pilani, Pilani Campus
exit()
• A process terminates its execution by making
an exit() system call.
• The OS reclaims resources (memory, files,
etc.) that were used by the process.
• The process is said to be a dead process after
it terminates.

17
BITS Pilani, Pilani Campus
sleep()
• A process may sleep, which places it into an
inactive state for a period of time.
• Eventually the expiration of an interval timer,
or the receipt of a signal or interrupt causes
the program to resume execution.
• A process may suspend for a period of time
using the sleep command.

18
BITS Pilani, Pilani Campus
sleep()
• unsigned int sleep (time)
• A typical sleep() call takes a time value as a
parameter, specifying the minimum amount of
time that the process is to sleep before
resuming execution.
• The parameter typically specifies seconds,
although some OSs provide finer resolution,
such as milliseconds or microseconds.

19
BITS Pilani, Pilani Campus
Orphan process
• When a parent dies before its child, the child
is automatically adopted by the original “init”
process whose PID is 1.
• An orphan process is a process whose parent
process has finished, though it remains
running itself.

20
BITS Pilani, Pilani Campus
void main()
{
int pid ;
printf("I am original process with PID %d & PPID %d.\n", getpid(), getppid());
pid = fork () ;
if ( pid != 0 )
{
printf("I'am the parent with PID %d and PPID %d.\n", getpid(), getppid()) ;
printf("My child's PID is %d\n", pid ) ;
}
else /* pid is zero, so I must be the child */
{
sleep(40); /* make sure that the parent terminates first */
printf("I'm the child with PID %d and PPID %d.\n", getpid(), getppid()) ;
}
printf ("PID %d terminates.\n", getpid()) ;
} 21
BITS Pilani, Pilani Campus
I am original process with PID 2845 & PPID 2662.

I'am the parent with PID 2845 and PPID 2662.

My child's PID is 2846

PID 2845 terminates.

I'm the child with PID 2846 and PPID 1.

PID 2846 terminates.


22
BITS Pilani, Pilani Campus
Zombie process
• A process that terminates cannot leave the
system until its parent accepts its return code.
• If its parent process is already dead, it will
already have been adopted by the “init”
process, which always accepts its children’s
return codes.

23
BITS Pilani, Pilani Campus
Zombie process
• However, if a process’s parent is alive but
never executes a wait(), the process’s return
code will never be accepted and the process
will remain a zombie.

24
BITS Pilani, Pilani Campus
#include <stdio.h>
main ( )
{
int pid ;
pid = fork(); /* Child and parent continue from here */
if ( pid != 0 ) /* pid is non-zero, so I must be the parent */
{
while (1) /* Never terminate and never execute a wait () */
sleep (100) ; /* stop executing for 100 seconds */
}
else /* pid is zero, so I must be the child */
{
exit (1) ;
}
}
25
BITS Pilani, Pilani Campus
hafi786@hafi786-VirtualBox:~/Desktop/test$ ./a.out /*execute the program
in the background 51868 */
hafi786@hafi786-VirtualBox:~/Desktop/test$ ps
/*obtain process status*/
PID TT STAT TIME COMMAND
5187 p0 Z 0:00 /* the zombie child process */
5149 p0 S 0:01 -bsh /*(bsh) the shell */
5186 p0 S 0:00 a.out /* the parent process */
5188 p0 R 0:00 ps
hafi786@hafi786-VirtualBox:~/Desktop/test$ kill 5186 /*kill the parent
process Terminated a.out */
hafi786@hafi786-VirtualBox:~/Desktop/test$ ps
PID TT STAT TIME COMMAND
5149 p0 S 0:01 -bsh
5189 p0 R 0:00 ps

26
BITS Pilani, Pilani Campus
exec family
• In computing, exec is a functionality of an OS
that runs an executable file in the context of
an already existing process, replacing the
previous executable.
• This act is also referred to as an overlay.

27
BITS Pilani, Pilani Campus
exec family
• As a new process is not created, the process
identifier (PID) does not change, but the
machine code, data, heap, and stack of the
process are replaced by those of the new
program.
• The exec functions are defined in unistd.h
header file

28
BITS Pilani, Pilani Campus
exec family
• Calling one of the exec() family will terminate
the currently running program and starts
executing a new one which is specified in the
parameters of exec in the context of the
existing process.
• The process id is not changed.

29
BITS Pilani, Pilani Campus
exec family
• The family of exec() system calls provides
some flexibility in how arguments are passed
in and how the program is looked up.

30
BITS Pilani, Pilani Campus
exec family
• e – An array of pointers to environment
variables is explicitly passed to the new
process image.

• l – Command-line arguments are passed


individually to the function.

31
BITS Pilani, Pilani Campus
exec family
• p – Uses the PATH environment variable to
find the file named in the path argument to be
executed.

• v – Command-line arguments are passed to


the function as an array of pointers.

32
BITS Pilani, Pilani Campus
exec family
• execv()
• execl()
• execvp()
• execlp()

33
BITS Pilani, Pilani Campus
execv()
• int execv (const char *filename, char *const argv[ ])
• The execv() function executes the file named by
filename as a new process image.
• The argv argument is an array of null-terminated
strings that is used to provide a value for the argv
argument to the main function of the program to be
executed.
• By convention, the first element of this array is
the file name of the program.

34
BITS Pilani, Pilani Campus
execv()
#include <stdio.h>
#include <unistd.h>
void main (argc, argv) It will return all the files as done by
int argc ; $ ls
char *argv[] ;
{
execv ("/bin/ls", /* program to load - full path only */
&argv[0] ) ;
printf ("EXEC Failed\n") ;
/* This above line will be printed only on error and not otherwise */
}
35
BITS Pilani, Pilani Campus
execl()
• The environment for the new process image is taken
from the environ variable of the current process
image.
• int execl (const char *filename, const char *arg0, ...)
• This is similar to execv(), but the argv strings are
specified individually instead of as an array.
• A null pointer must be passed as the last argument.

36
BITS Pilani, Pilani Campus
execl()
#include <stdio.h> $ ls –l –a
#include <unistd.h> $ ls –a Displays all files.
void main ( ) $ls –l Displays the long format listing.
{
execl ("/bin/ls", /* program to run - give full path */
"ls", /* name of program sent to argv[0] */
"-l", /* first parameter (argv[1])*/
"-a", /* second parameter (argv[2]) */
NULL) ; /* terminate arg list */
printf ("EXEC Failed\n") ;
/* This above line will be printed only on error and not otherwise */
}
37
BITS Pilani, Pilani Campus
execvp()
• int execvp (const char *filename, char *const argv[ ])

• The execvp() function is similar to execv(), except


that it searches the directories listed in the PATH
environment variable to find the full file name of a file
from filename if filename does not contain a slash.

• This function is useful for executing system utility


programs, because it looks for them in the places that
the user has chosen.
38
BITS Pilani, Pilani Campus
execvp()
#include <stdio.h>
#include <unistd.h>
main (int argc, char *argv[] )
{
execvp (“ls", /* program to load - PATH searched */
&argv[0] ) ;
printf ("EXEC Failed\n") ;
/* This above line will be printed only on error and not otherwise */
}

39
BITS Pilani, Pilani Campus
execlp()
• int execlp (const char *filename, const char *arg0, ...)
• This function is like execl(), except that it
performs the same file name searching as the
execvp() function.

40
BITS Pilani, Pilani Campus
execlp()
#include <stdio.h> $ ls –l –a
#include <unistd.h> $ ls –a Displays all files.
void main ( ) $ls –l Displays the long format listing.
{
execlp ("ls", /* program to run - PATH Searched */
"ls", /* name of program sent to argv[0] */
"-l", /* first parameter (argv[1])*/
"-a", /* second parameter (argv[2]) */
NULL) ; /* terminate arg list */
printf ("EXEC Failed\n") ;
/* This above line will be printed only on error and not otherwise */
}
41
BITS Pilani, Pilani Campus
Try it
int main()
{
unsigned int status;
if ( fork () == 0 ) /* == 0 means in child */
{
scanf ("%d", &status);
exit (status);
}
else
{ /* != 0 means in parent */
wait (&status);
printf("child exit status = %d\n", status);
}
}
42
BITS Pilani, Pilani Campus
References
• https://www.cs.rutgers.edu/~pxk/416/notes/c-tutorials/exec.html
• http://www.tutorialspoint.com/unix_system_calls/execve.htm
• http://linux.die.net/man/3/execle
• http://en.wikipedia.org/wiki/Zombie_process
• http://en.wikipedia.org/wiki/Parent_process
• http://en.wikipedia.org/wiki/Exit_(system_call)
• http://www.csl.mtu.edu/cs4411.ck/www/NOTES/process/fork/wa
it.html
• http://en.wikipedia.org/wiki/Wait_(system_call)

43
BITS Pilani, Pilani Campus

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy