Process

Characteristics

  1. A process contains:

    • A unique ID
    • A thread of control
    • Some private data
  2. Parent-Child Links:

    • parent process: executes the request for creation of a process
    • child process: the created process
    • Noted: A child process inherits all the properties of its parent when it is created.

Process ID

  1. Process ID (PID):

    • Each process has a unique ID number.
    • It’s use to identify different processes.
  2. User ID (UID):

    • Each user has a unique user ID.
    • Each process is associated with the the owner of the process, the owner has privileges with respect to the process.
  1. Obtain ID in C:

    1. getpid() - obtain the child’s PID.
    2. getppid() - obtain the parent’s PID.
    3. getuid() - obtain the owner’s UID.

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      /* 1-getpid */
      #include <stdio.h> // printf()
      #include <unistd.h> // getpid()

      int main(void) {
      printf("My PID is %d, UID is %d, My Parent PID is %d\n",
      getpid(), getuid(), getppid());
      return 0;
      }

      /*
      My PID is 4003, UID is 501, My Parent PID is 3843
      */
  1. Obtain ID in terminal:
    1. ps - lists the current processes.
    2. ps -a - lists more processes, including the processes that being run by other users and at other terminals.
    3. ps -l - prints more information lines, including UID, PID, PPID, process status, etc.

Process Operations

fork System Call

  • Description:

    • To create a process (child process).
  • Syntax: int fork()

  • Return:

    • To child: return 0
    • To parent: return child’s PID

  • Example:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    /* 2-fork */
    #include <stdio.h>
    #include <sys/types.h>
    #include <unistd.h>

    int main(void){
    int ret_from_fork;

    printf("Before: my pid is %d\n", getpid());

    ret_from_fork = fork(); // create a child

    sleep(1);

    printf("After: my pid is %d, return value from fork():%d\n",
    getpid(), ret_from_fork);

    return 0;
    }

    /*
    Before: my pid is 3991
    After: my pid is 3994, return value from fork():0
    After: my pid is 3991, return value from fork():3994
    */
  • Task:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    /* How many child processes were created? */
    fork();
    fork();
    fork();

    /* 6-3fork */
    #include <sys/types.h>
    #include <sys/wait.h>
    #include <unistd.h>
    #include <stdio.h>
    #include <stdlib.h>

    int main(void){
    fork();
    fork();
    fork();
    printf("PID - %d, PPID -%d\n", getpid(), getppid());
    sleep(1);
    return 0;
    }

    /*
    PID - 5207, PPID -5141
    PID - 5210, PPID -5207
    PID - 5209, PPID -5207
    PID - 5211, PPID -5209
    PID - 5208, PPID -5207
    PID - 5213, PPID -5208
    PID - 5212, PPID -5208
    PID - 5214, PPID -5212
    */

wait System Call

  • Description:

    • After fork() system call, both parent and child proceed independently.
    • Therefore, if a parent wants to wait unitl the child finishes, then executes wait() or waitpid().
  • Syntax: pid_t wait(int *stat)

    • stat: the child process
  • Return:

    • If because a child terminated: return the PID of that child
    • Otherwise, return -1
  • Working Process:
    • it causes the caller process to pause,
      • until the a child terminates or stops, or
      • until the caller recieves a signal
    • it returns right away,
      • if the process has no child, or
      • if the child has already terminated or stopped but has not yet been waited for
  • Example:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    /* 7-mywait */
    #include <stdio.h>
    #include <sys/types.h>
    #include <sys/wait.h>
    #include <unistd.h>
    #include <errno.h>

    int main(void)
    {
    int i;
    int n = 5;
    pid_t childpid;
    int status;
    pid_t waitreturn;

    // repeat 5 times
    for (i = 1; i < n; ++i){
    // create a child then break -> new process becomes parent
    if((childpid = fork()) > 0)
    break;
    }

    // wait the child precess terminates
    if((waitreturn = wait(&status)) < 0)
    // if there is no child (wait returns -1 which < 0)
    printf("wait(): Returns - %d.\n", waitreturn);

    printf("I am process %d, my parent is %d, childpid:%d, i:%d\n",
    getpid(), getppid(), childpid, i);

    return 0;
    }

    /*
    wait(): Returns - -1.
    I am process 4119, my parent is 4118, childpid:0, i:5
    I am process 4118, my parent is 4117, childpid:4119, i:4
    I am process 4117, my parent is 4116, childpid:4118, i:3
    I am process 4116, my parent is 4115, childpid:4117, i:2
    I am process 4115, my parent is 3843, childpid:4116, i:1
    */

exec System Call

  • Description:
    • To change processes.
    • The fork system call creates a copy of the calling process (child inherits all the properties of its parent).
    • However, many applications require the child process to executes code different from the parent’s.
    • Therefore, executes exec family system calls to overlay the calling process with a new executable module.
  • Working Process:
    • exec will load a new executable into the process image, copies the arguments into the process and calls main(args, argy).
    • If succeed, exec never returns. The calling process is completely overlaid by the new program.
    • The fork-exec combination can let the child execute the new program while the parent continues to execute the original code.
  • Variations of exec system call:

    • execl calls (execl, execlp, execle)

      • pass the command-line arguments as a list of pointers.
    • execv calls (execv, execvp, execve)

      • pass the command-line arguments in an argument array, which is an array of pointers to null-terminated strings (ie. must be terminated by a NULL pointer).
  • Example:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    /* 8-myexecl */
    #include <sys/types.h>
    #include <sys/wait.h>
    #include <unistd.h>
    #include <stdio.h>
    #include <stdlib.h>

    int main(void){
    pid_t childpid;
    int status;

    char *cmd = "ls";
    char *argv[2];
    argv[0] = "ls";
    argv[1] = NULL;

    printf("%d", wait(&status));

    // create a child to execute new program
    if ((childpid = fork()) == -1){
    printf("Error: fork()\n");
    return -1;

    }else if(childpid == 0) {
    /* child code (childpid for child is 0) */
    if (execvp(cmd, argv) < 0) {
    printf("Error: execvp()\n");
    return -1;
    }

    }else if(childpid != wait(&status)){
    /* parent code */
    printf("Error: wait()");
    return -1;
    }

    return 0;
    }

    /*
    Results: print the list
    */

Process Termination

Two Terminations

  1. Normal Process Termination

    • A process terminates normally when,
      1. a return from main
      2. an implicit return from main
      3. a call to the _exit system call
      4. a call to the C function exit
  2. Abnormal Process Termination

    • A process terminates abnormally when,
      1. calling abort, causing the SIGABRT signal to be sent to the calling process.
      2. processing a signal that causes termination.
    • User-installed exit handler will not be called upon abnormal termination.

Orphan Process

  • it occurs if when the child still executing but its parent has terminated.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    /* 9-orphan */
    #include <sys/types.h>
    #include <sys/wait.h>
    #include <unistd.h>
    #include <stdio.h>
    #include <stdlib.h>

    int main(void)
    {
    pid_t childpid;

    if((childpid = fork()) == -1){
    printf("Error in the fork");
    return -1;

    }else if(childpid == 0) {
    /*child code*/
    printf("Before: I am the child (PID - %d, PPID -%d)\n", getpid(), getppid());
    sleep(10); // the child has not been terminated
    printf("Before: I am the child (PID - %d, PPID -%d)\n", getpid(), getppid());
    return 0;

    }else{
    /* parent code */
    sleep(1); // the parent is terminated
    printf("I am the parent (PID - %d, PPID -%d)\n", getpid(), getppid());
    return 0;
    }
    }

Zombie Process

  • it occurs if the parent of the terminating process is not currently executing a wait().
  • Noted: the zombie process is not an orphan process.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    /* 9-1-zombie */
    #include <sys/types.h>
    #include <sys/wait.h>
    #include <unistd.h>
    #include <stdio.h>
    #include <stdlib.h>

    int main(void){
    pid_t childpid;

    if ((childpid = fork()) == -1){
    printf("Error in the fork");
    return -1;
    } else if (childpid == 0) {
    /*child code*/
    printf("Before: I am the child (PID - %d, PPID -%d)\n", getpid(), getppid());
    return 0;
    }else{
    /* parent code */
    sleep(100); // child process is terminated but parent does not
    printf("I am the parent (PID - %d, PPID - %d)\n", getpid(), getppid());
    return 0;
    }
    }

    /*
    ls -l
    UID PID PPID S
    501 4276 4275 Z+ // this is the zombie process
    */