Instruction

by Paweł Paduch published 2020/11/12 09:20:00 GMT+2, last modified 2023-10-12T11:02:04+02:00
This lab is to learn about the fork, wait functions and how to intercept signals (signal function)

Starting and ending process

This lab is to familiarize you with the fork, wait function and how to capture signals (function signal)

  1. Introduction
    The only way to create a new process in Unix is to call a system function fork(). (man fork) other functions np. clone vfork are interfaces of fork. A function closely related to the fork is wait  waiting for the completion of the child.
    Singals. The signal function is used to capture the signal. kill is the function used to send signals.

  2. Write or copy paste and run the following program, try understand how it works.
    #include <stdlib.h> 
    #include <stdio.h>
    #include <unistd.h>

    int main() { int childpid; int status=1; printf("Parent process is starting. pid = %d\n",getpid()); if ((childpid = fork()) == -1) { perror("Can't fork"); exit(1); } else if (childpid ==0 ) { printf("Child process whith pid %d from parent %d\n",getpid(),getppid()); } else { printf("Parent process whit pid %d and child %d\n",getpid(),childpid); } exit(0); }
  3. Make the child process terminate after the parent process [0,5 points] (use function sleep).
    Check the parent process ID in the child process after the parent terminates. Make the child process terminate before the end of the parent (the parent does not call wait yet). Check if the child process exists in the system (ps -efa).

  4. Use function wait [0,5 point]
    receiving the end result of the child process. Check the behavior of both processes. The child process should return a value other than 0, e.g. exit (2). Display what the wait will receive in unformatted way (don't use  WEXITSTATUS or ).

  5. Make n=5 child process from one parent process.  [1,5 point]
    Use wait function for proper ending. Each child should pause for 5 seconds (use sleep) in order to check on the others consolas (ps -efa) whether exactly 5 children were generated (ps should show 5 chldren process). Processes should start together and end together. Use a loop instead of an construct if else if else ...

  6. Make n=5 generation [1,5 point]
    parrent->child1->child2->...->child5. Caution. there is a risk of looping the creation of processes. Take care not to lose control of the system. Also save the edited files. Check as above if it was possible to generate exactly 5 children (ps should show 5 child processes). Processes should start together and end together. Use a loop instead of an construct if else if else ...

  7. Example [1 point]
    of handling singal SIGCHLD - sent by the child at the time of finish. Use the example below in any of the programs you have made.
    #include <signal.h> /* there is signal definitions */
    /* sample signal handling function */
    void child_termination_handler(int nr_sig)
    {
    printf("The parent has already found out about the end of the child %d\n",wait(NULL));
    }
    /* telling the process to call the function hild_termination_handler when a SIGCHLD signal arrives*/
    signal(SIGCHLD,hild_termination_handler);
  8. Report

        Don't forget about report