System Calls in linux

System Calls

  • Process creation system call fork ( )
  • Wait system call
  • Exit system call
  • Sleep system call
  • Exec system call

Fork() system Call:

  • Processes are created with the fork () system
  • The child process created by fork is a copy of the original parent process, except that it has its own process ID.
  • After forking a child process, both the parent and child processes continue to execute
  • If you want your program to wait for a child process to finish executing before continuing, you must do this explicitly after the fork operation, by calling wait () or waitpid (). These functions give you limited information about why the child For example, its exit status code.
  • A newly forked child process continues to execute the same program as its parent process, at the point where the fork call
  • You can use the return value from fork to tell whether the program is running in the parent process or the child
  • If the fork return value is 0,it will be
  • When a child process terminates, its death is communicated to its parent so that the parent may take some appropriate
  • If process creation failed, fork returns a value of -1 in the parent process and no child is

Example 1 – Single fork():

Example 1

 

Example 3

 

Example 4:

Wait ()

  • A process wait () for a child process to terminate or stop, and determine its
  • These functions are declared in the header file “sys/wait.h”.
  • A call to wait () blocks the calling process until one of its child processes exits or a signal is
  • After child process terminates, parent continues its execution after wait system call
  • Wait () will force a parent process to wait for a child process to stop or
  • Wait () return the pid of the child or -1 for an

Exit ():

  • Exit () terminates the process which calls this function and returns the exit status
  • Both UNIX and C (forked) programs can read the exit status
  • By convention, a status of 0 means normal
  • Any other value indicates an error or unusual occurrence

Sleep ( ):

A process may suspend for a period of time using the sleep () command.

zombie process

A process which has finished the execution but still has entry in the process table to report to its parent process is known as a zombie process.

A child process always first becomes a zombie before being removed from the process table.

The parent process reads the exit status of the child process which reaps off the child process entry from the process table.

When a child process terminates it does not disappear entirely. The process of eliminating zombie processes is known as ‘reaping‘. The simplest method is to call wait , but this will block the parent process if the child has not yet terminated.

Program:

Orphan Process:

A process whose parent process no more exists i.e. either finished or terminated without waiting for its child process to terminate is called an orphan process.

However, the orphan process is soon adopted by init process, once its parent process dies.

After executing the above code, an orphan process will be created.

Activity:

Create a process and make it a Zombie.

  1. Execute fork to create a child
  2. In parent process (using if statement) Create an infinite loop so that it never terminates and never executes wait ()
  3. Make parent sleep for 100 sec
  4. Terminate child process exit using

Solution:

  1. Open gedit with file name ‘wait.c’.

 

  1. Write below code in ‘wait.c’

  1. Execute the c code in background using ./wait & .
  2. View process status using ‘ps -lf’. You will see zombie process with state ‘Z’ in STAT column. Now this child is a zombie because no parent is waiting for him

  1. Get parent id and kill parent process using “kill (parent id)”
  2. Again execute ‘ps -lf’.
  3. Note that Zombie is gone now

Exec() System Call:

  • The exec family of functions replaces the current running process with a new
  • It comes under the header file h.
  • The program that the process is executing is called its process
  • Starting execution of a new program causes the process to forget all about its previous process
  • when the new program exits, the process exits too, instead of returning to the previous process
  • There are a lot of exec functions included in exec family of functions, for executing a file as a process image e.g. execl, execlp, execle, execv,execvpe
  • The functions in this family differ in how you specify the arguments, but otherwise they all do the same

execv() system call:

With execv(), you can pass all the parameters in a NULL terminated array. The first element of the array should be the path of the executable file.

Example 1:

Output

Example 2:

  • Create a file with name “beforeexecv.c” using gedit.

Type ‘gedit beforeexecv.c’ in terminal and press Enter

  • Compile above C file code using gcc

gcc filename -o executable_file_name

  • Create another file with name ‘after_execv.c’ using  gedit

  • Compile the C file using gcc After compiling the code, Executable file name is ‘hello’.

Output:

execlp() system call:

execlp() uses the PATH environment variable. So, if an executable file or command is available in the PATH, then the command or the filename is enough to run it, the full path is not needed.

Example 1:

In below code, we only pass the command name ls, not the full path /bin/ls

As you can see, I got the same output as before executing the program using execv() system call.

Example 2:

Execl() system call:

In execl() system function takes the path of the executable binary file (i.e. /bin/ls) as the first and second argument. Then, the arguments that you want to pass to the executable followed by NULL. Then execl() system function runs the command and prints the output. If any error occurs, then execl() returns -1. Otherwise, it returns nothing.

 

Related Links  to Operating System topics

Operating system Course content

Lab Practice Task

#Operating System complete course #Operating System past paper #Operating System-project #Computer Science all courses  #operating system Problem with source code#University Past Paper #Programming language #Question paper #old paper #Operating System-Functions and History #Generations of Operating System #Functions of an Operating System #Components of Operating System #Types of Operating System #Services of Operating System #Properties of Operating System #Processes in Operating System #Process Scheduling in Operating System #Introduction to Linux Ubunto #Installation with virtual Box #Writing Linux Commands #Navigation in File System and Directory Management in Ubunto using CLI #File Handling and I/O Redirection In Ubunto #File Access Permission in Linux #Text Processing Tools and Basic System Configuration Tools in Linux #Package Management in Linux #How to manage processes in Linux #Compiling and Executing C++ programs in Linux #System Calls #Introduction To Shell Programming

Search within CuiTutorial

Scroll to Top