System Calls

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 20

Process Related

System Calls

By
Neha Hulkoti & Kavya Bhat
System calls
The UNIX system provides several system calls
to create and end program, to send and receive
software interrupts, to allocate memory, and to do
other useful jobs for a process.
  Four system calls are provided for creating a
process, ending a process, and waiting for a
process to complete. These system calls are
fork(), the "exec" family, wait(), and exit().
Fork( )
 The exec family of system calls transforms an
executable binary file into a process that overlays the
process that made the exec system call.
 The UNIX system does not create a new process
in response to an exec system call.
 To create a new process, you must use the fork()
system call. The prototype for the fork() system call
is:
int fork()
 fork() causes the UNIX system to create a new
process, called the "child process", with a new
process ID.
 The contents of the child process are identical to
the contents of the parent process.
The new process inherits several
characteristics of the old process.
 Among the characteristics inherited are:
 The environment.
 All signal settings.
 The set user ID and set group ID status.
 The time left until an alarm clock signal.
 The current working directory and the root
directory.
 The file creation mask as established with
umask().
 The child process begins executing and the parent
process continues executing at the return from the
fork() system call.
 This is difficult to understand at first because you
only call fork() once, yet it returns twice -- once per
process.
 To differentiate which process is which, fork()
returns zero in the child process and non-zero (the
child's process ID) in the parent process.
 exec routines are usually called after a call to
fork(). This combination, known as a fork/exec,
allows a process to create a child to execute a
command, so that the parent doesn't destroy itself
through an exec.
 Most command interpreters (e.g. the shell) on
UNIX use fork and exec.
exec()

 The UNIX system calls that transform a


executable binary file into a process are the
"exec" family of system calls.

 Forking creates a process but it is not enough


to run a new program, to do that the forked child
needs to overwrite its own image with the code
and data of the new program and mechanism is
exec() creation.

 No new process is created here ,the PID &


PPID of the exec’d process remain unchanged.
The prototypes for these
calls are:
int execl(file_name, arg0 [, arg1, ..., argn], NULL)
char *file_name, *arg0, *arg1, ..., *argn;
 int execv(file_name, argv)
char *file_name, *argv[];
 int execle(file_name, arg0 [, arg1, ..., argn], NULL,
envp)
char *file_name, *arg0, *arg1, ..., *argn, *envp[];
int execve(file_name, argv, envp)
char *file_name, *argv[], *envp[];
 int execlp(file_name, arg0 [, arg1, ..., argn], NULL)
char *file_name, *arg0, *arg1, ..., *argn;
 int execvp(file_name, argv)
char *file_name, *argv[];
where file_name names the executable binary file to
be transformed into a process, arg0 through argn and
argv define the arguments to be passed to the
process, and envp defines the environment, also to be
passed to the process.
By convention, arg0 and argv[0] name the last path
name component of the executable binary file named
by file_name.
For execl(), execv(), execle(), and execve(),
file_name must be the fully qualified path name of
the executable binary file.
However for execlp() and execvp(), the PATH
variable is used to find the executable binary file.
 When the environment is not explicitly given as an
argument to an exec system call, the environment of
the current process is used.
Furthermore, the last array element of both argv
and envp must be null to signify the end of the array.
Unlike the other system calls and subroutines,
a successful exec system call does not return.
 Instead, control is given to the executable
binary file named as the first argument.
 When that file is made into a process, that
process replaces the process that executed the
exec system call -- a new process is not created.
 If an exec call should fail, it will return a -1
Letters added to the end of exec
indicate the type of arguments:

 l argn is specified as a list of arguments.


 v argv is specified as a vector (array of
character pointers).
 e environment is specified as an array of
character pointers.
 p user's PATH is searched for command, and
command can be a shell program
 When transforming an executable binary file into a
process, the UNIX system preserves some
characteristics of the replaced process. Among the
items saved by the exec system call are:
The "nice" value for scheduling.
The process ID and the parent process ID.
The time left until an alarm clock signal.
The current working directory and the root directory.
 The file creation mask as established with umask().
 All open files.
 The last of these is the most interesting because the
shell uses this feature to handle input/output redirection.
wait()
 You can control the execution of child processes by
calling wait() in the parent. wait() forces the parent to suspend
execution until the child is finished. wait() returns the process
ID of a child process that finished.
 If the child finishes before the parent gets around to
calling wait(), then when wait() is called by the parent, it will
return immediately with the child's process ID. (It is possible
to have more that one child process by simply calling fork()
more than once.). The prototype for the wait() system call is:
int wait(status)
int *status;
where status is a pointer to an integer where the UNIX system
stores the value returned by the child process. wait() returns
the process ID of the process that ended.
wait() fails if any of the following conditions hold:
 The process has no children to wait for.
 Status points to an invalid address.
The format of the information returned by wait() is as
follows:
 If the process ended by calling the exit() system call,
the second lowest byte of status is set to the argument
given to exit() and the lowest byte of status is set to zeroes.
 If the process ended because of a signal, the
second lowest byte of status is set to zeroes and the
lowest byte of status contains the signal number that ended
the process.
 If the seventh bit of the lowest byte of status is set
(i.e. status & 0200 == 0200) then the UNIX system
produced a core dump of the process.
Zombies

 If a process exits in UNIX and its parent does not pick up


its exit status.
Not using resources
Show up with “Z” in the process state “S” column when
using a “ps -a” command
Stay in system until reboot
To avoid zombies, make the parent process do a wait or
waitpd to pick up the status.
exit()
The exit() system call ends a process and returns a value to
it parent. The prototype for the exit() system call is:
void exit(status)
int status;
where status is an integer between 0 and 255. This number
is returned to the parent via wait() as the exit status of the
process.
 By convention, when a process exits with a status of zero
that means it didn't encounter any problems; when a process
exit with a non-zero status that means it did have problems.
exit() is actually not a system routine; it is a library routine
that call the system routine exit().
exit() cleans up the standard I/O streams before calling
exit(), so any output that has been buffered but not yet actually
written out is flushed.
Following are some example programs that demonstrate the use of fork(),
exec(), wait(), and exit():
/* status.c - demonstrates exit() returning a status to wait(). */
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 > 8);
}
}
Note: since wait() returns the exit status multiplied by 256 (contained
in the upper 8 bits), the status value is shifted right 8 bits (divided by
256) to obtain the correct value.
myshell.c - This program is a simple command interpreter that uses execlp() to execute
commands typed in by the user.
#include <stdio.h>
#define EVER ;;
int main()
{ int process;
char line[81];
for (EVER)
{ fprintf(stderr, "cmd: ");
if ( gets (line) == (char *) NULL) /* blank line input */
exit (0);
process = fork (); /* create a new process */
if (process > 0) /* parent */
wait ((int *) 0); /* null pointer - return value not saved */
else if (process == 0) /* child */
{ execlp (line, line, (char *) NULL); /* execute program */
fprintf (stderr, "Can't execute %s\n", line); /* some problem if exec returns */
exit (1); }
else if ( process == -1) /* can't create a new process */
{ fprintf (stderr, "Can't fork!\n"); exit (2); }
}
newdir.c - create a new directory, called newdir, using fork() and
exec().
#include <stdio.h>
int main()
{ int fd;
if ( fork() != 0)
wait ((int *) 0);
else {
execl ("/bin/mkdir", "mkdir", "newdir", (char *) NULL);
fprintf (stderr, "exec failed!\n");
exit (1); } /* now use newdir */
if ( (fd = open("newdir/foo.bar", O_RDWR | O_CREAT, 0644)) == -1)
{
fprintf (stderr, "open failed!\n");
exit (2);
}
write (fd, "Hello, world\n", 14);
close (fd);
exit (0);
}
kill()
The UNIX system sends a signal to a process when something
happens, such as typing the interrupt key on a terminal, or
attempting to execute an illegal instruction.
Signals are also sent to a process with the kill() system call. Its
prototype is: int kill (process_id, signal_name )
int process_it, signal_name;
where process_id is the ID of the process to be signaled and
signal_name is the signal to be sent to that process.
If process_id has a positive value, that value is assumed to be the
process ID of the process to whom signal_name signal is to be
sent.
If process_id has the value 0, then signal_name signal is sent to all
processes in the sending process' process group, that is all
processes that have been started from the same terminal.
If process_id has the value -1 and the process executing the kill()
system call is the superuser, then signal_name is sent to all
processes excluding process 0 and process 1 that have the same

You might also like