Processes

OS Laboratory Team
Fall 2018

The Process

A program in execution

Memory Layout

Text section
The executable code
Data section
Global variables
Heap section
Memory that is dynamically allocated during program run time
Stack section
Temporary data storage when invoking functions (such as function parameters, return address, and local variables)
ps_mem_layout

Let's code :)


          #include <stdlib.h>
          #include <stdio.h>

          int x;
          int y = 15;

          int main(int argc, char *argv[]) {
            int *values;
            int i;

            values = (int *) malloc(sizeof(int) * 5);

            for (i = 0; i < 5; i++) {
              values[i] = i;
            }

            return 0;
          }
          

          # the size (in bytes) of some of these sections
          $ size program_name
          

Process State

New
The process is being created
Running
Instruction are being executed
Waiting
The process is waiting for some event to occur (such as an I/O completion or reception of a signal)
Ready
The process is waiting to be assigned to a processor
Teminated
The process has finished execution
process_state_diagram

Operations on Processes

The processes in the must systems can execute concurrently, and they may be created and deleted dynamically.

Concurrent vs Parallel

Based on Stackoverflow answer

If you program using concurrent programming, it's not necessarily going to be executed as such (parallel execution), since it depends on the machine.


              --  --  --
           /              \
       ---- --  --  --  -- ----
           ------
          /      \
       --------------
          

Let's code :)


          # -l Display information associated with the following keywords: uid, pid, ppid, ...
          # -e Display information about other uesrs' processes, including those without controlling terminals.
          ps -el
          

          pstree
          

          top
          

          htop
          

When a process creates a new process, two possibilities for execution

  • The parent continues to execute concurrently with its children
  • The parent waits until some or all of its chidlren have terminated

There are also two address-space possibilities for the new process

  • The child process is a duplicate of the parent process
  • The child process has a new program loaded into it

Let's code :)

  • fork
    • System call fork() is used to create processes. It takes no arguments and returns a process ID
    • The purpose of fork() is to create a new process, which becomes the child process of the caller.
  • exec
    • exec runs an executable file in the context of an already existing process, replacing the previous executable.
  • wait
    • wait for state changes in a child of the calling process
    • obtain information about the child whose state has changed
  • vfork
    • The vfork() function has the same effect as fork()
    • except that the behavior is undefined if the process created by vfork():
      1. modifies any data other than a variable of type pid_t used to store the return value from vfork()
      2. returns from the function in which vfork() was called
      3. calls any other function before successfully calling _exit(2) or one of the exec(3) family of functions.

          #include  <stdio.h>
          #include  <unistd>

          #define   MAX_COUNT  200
          #define   BUF_SIZE   100

          int  main(int argc, const char *argv[]) {
            pid_t  pid;
            int    i;
            char   buf[BUF_SIZE];

            fork();
            pid = getpid();
            for (i = 1; i ≤ MAX_COUNT; i++) {
              sprintf(buf, "This line is from pid %d, value = %d\n", pid, i);
              write(1, buf, strlen(buf));
            }
          

fork() system call

fork_1
fork_2

          int execl(char const *path, char const *arg0, ...);
          int execle(char const *path, char const *arg0, ..., char const *envp[]);
          int execlp(char const *file, char const *arg0, ...);
          int execv(char const *path, char const *argv[]);
          int execve(char const *path, char const *argv[], char const *envp[]);
          int execvp(char const *file, char const *argv[]);
          
  • e – An array of pointers to environment variables is explicitly passed to the new process image.
  • l – Command-line arguments are passed individually (a list) to the function.
  • p – Uses the PATH environment variable to find the file named in the file argument to be executed.
  • v – Command-line arguments are passed to the function as an array (vector) of pointers.
The first argument arg0 should be the name of the executable file.

fork() and wait() system call

process_creation_os

Zombieeeee

zombie_process