OPERATING SYSTEMS LABORATORY IV - Processes I

  1. Examples:
    1. Creating a (child) process; fork - http://siber.cankaya.edu.tr/OperatingSystems/cfiles/code14.c code14.c.
      Figure 1: System call; Fork
      \includegraphics[scale=0.7]{figures/forklab.ps}
      #include <stdio.h>
      main()
      {
        puts("Begin fork test.");
        fork();
        puts("End fork test.");
      }
      
      You should have the message "End fork test." twice. Why?
    2. Parent and child process; getpid - http://siber.cankaya.edu.tr/OperatingSystems/cfiles/code15.c code15.c.
      • Execute this code several times. You should observe that the order for the print messages of the parent and child processes change. Why?
    3. Fork at the beginning and error analysis; perror - http://siber.cankaya.edu.tr/OperatingSystems/cfiles/code16.c code16.c.
      • Find out that the function perror is included by which library.
        • Why the value $-1$ is checked?
      • Fork is executed at the beginning of the program. So that the rest of the code will be duplicated, now belonging to both parent and child processes.
      • Execute several times and examine the order in the output. Is there any specific order that which one will be executed first?
      • What is the function of the type definition pid_t?
    4. Signaling; sleep,getppid - http://siber.cankaya.edu.tr/OperatingSystems/cfiles/code17.c code17.c.
      • What is the function of sleep?
      • Who is the parent of the PARENT? Find it out by the command
        ps aux | grep <Put the ParentPID you see here>
        
      • Why CHILD process prints out the PID of its parent as 1 (See next example)?
    5. Synchronizing; wait - http://siber.cankaya.edu.tr/OperatingSystems/cfiles/code18.c code18.c.
      • What is meant by synchronization?
      • wait and sleep are system calls. What is the function of wait?
      • Why CHILD process prints out the PID of its parent correctly now?!
    6. Zombie processes - http://siber.cankaya.edu.tr/OperatingSystems/cfiles/code19.c code19.c.
      • When a child process terminates, an association with its parent survives until the parent in turn either terminates normally or calls wait.
      • The child process entry in the process table is therefore not freed up immediately.
      • Although no longer active, the child process is still in the system because its exit code needs to be stored in case the parent subsequently calls wait. It becomes what is known as defunct, or a zombie process.
      • Call the ps -ux program in another shell after the child has finished but before the parent has finished, we'll see a $<defunct>$ phrase in the line. (Some systems may say $<zombie>$ rather than $<defunct>$.)
      • If the parent then terminates abnormally, the child process automatically gets the process with PID 1 (init) as parent.
      • The child process is now a zombie that is no longer running but has been inherited by init because of the abnormal termination of the parent process.
    7. Understanding system - http://siber.cankaya.edu.tr/OperatingSystems/cfiles/code20.c code20.c .
      • Try to recognize your ``code20'' and corresponding PID from the output.
  2. Shared Memory
  3. Exercises:
    1. Write a program that creates a zombie and then call system to execute the ps command to verify that the process is zombie.
    2. Write a program to create 5 processes where the first process is the parent of the second and third ones and the third process is the parent of the fourth and fifth ones. Your program should be capable of;
      • checking if the processes are forked with success,
      • printing the pid and parent pid of each process,
      • printing the parent pid of the first process.
Figure 2: Fork exercise with 5 processes
\includegraphics[scale=1]{figures/fork-exercise.eps}
Cem Ozdogan 2010-03-15