Lecture 4 - Process

Definition

The sequential execution of the instructions of a program in a namespace.

Namespace:

  • code
  • stack
  • variables and constants
  • files

Different processes run on different namespace.

Notion of Process

  • Corresponds to the execution of a binary program
  • Identified in a unique way by its pid number
  • 3 segments: code (which can be kept in shared memory), data, stack
  • Runs under the identity of a user
  • Has a current directory

Data

Data segment

  • Data - initialized data
  • BSS (Block Started by Symbol) - Non initialized data
  • Heap - malloc(), calloc()

Namespace of a process

text executable code
data initialized data
bss non initialized data
heap dynamic allocation
stack execution (function calls, local variables, parameters)
U-area argv[], envp[], etc

States of a process

(we should have a graph here)

Process Control Block (PCB)

Infomation associated with each process

  • Process state
  • PID - pid_t getpid(void)
  • Program counter
  • CPU registers
  • CPU scheduling info
  • Memory management info
  • Accounting info
  • I/O info

Process Creation with a fork

pid_t fork(void)

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

int main(){
    int pid;
    pid = fork();
    printf("The pid of the program is %d\n", getpid());
    return 0;
}

Return value of fork():

  • Parent: child pid
  • Child: 0
  • Error: -1

getppid() - get parent pid.