Operating System - Processes in General

3 minute read

A process is a program in execution. A process has more than program code. It has the following components.

  1. Text section
  2. Stack
  3. Data selection
  4. Heap

TEXT SECTION: Program code + current activity like program counter and contents of processors registers
STACK: Temporary data such as function parameters, local variables (auto, register variables in C), return addresses
DATA SECTION: Global variables
HEAP: Memory allocated during dynamic allocation.

PROCESS STATE IN GENERAL:

NEW: Process is being created.
RUNNING: Instructions are being executed.
WAITING: Process is waiting for some event to occur/ to complete.
READY: Process has all resources allocated except C.P.U (i.e) waiting for to be attached to C.P.U
TERMINATED: Process has finished execution.

_config.yml

Each process in an OS is represented by Process Control Block (P.C.B) PCB has the following information about a process

  1. Process State
  2. Program Counter
  3. CPU Registers
  4. CPU Scheduling Information (Priorities, etc.)
  5. Memory- Management Information

TWO KINDS OF PROCESSES

  • I/O – BOUND PROCESSES: Spends more time doing I/O than it spends doing computations
  • CPU-BOUND PROCESS: generates I/O requests infrequently and uses more of its time doing computations.

It is important that the select good mix of I/O bound and CPU bound processes. Because if all are CPU bound, the wait queue will be empty and devices will go unused. If all are I/O bound, the read queue will almost be empty, the CPU utilization will be very less.

CONTEXT SWITCH:

When an interrupt occurs or the scheduler wants to run other process the system needs to save the state of current context of running process namely in PCB and loading the state of process to be runned. So, switching CPU to another process requires performing a state save of the current process and a state restore of different process. This is known as “CONTEXT SWITCH”.

PROCESS CREATION:

In “Linux”, process can be created by “fork ()” system call.

  • fork () returns pid of child process to parent process.
  • fork () returns value of “O” to new process created.

When a parent forks a child process

Child, parent can have different address spaces with same content copied and starts executing immediately after fork () function call.

Also, child, parent can have sane address space, create a copy of necessary required when one of them is trying to modify. This is known as “Copy on write”.

Apart from fork(), execlp () replace entire address space with contents of new process that has to be runned.

PROCESS TERMINATION: When a process creates a child process using “fork ()” system call. The parent has to wait for to collect exit status (int status; exit(&status);) of child. If not

  • Parent process is busy somewhere else not collected exit status of child process. The entry in process table doesn’t get removed and it becomes “ZOMBIE” process.
  • If parent has terminated before child terminates and has not collected child’s exit status then also child’s entry in process table is not removed and it becomes “Orphan” process and get’s attached as child process to “init” process (or) “systemd” daemon process.

INTER PROCESS COMMUNICATION:

A process is “independent” if it cannot affect or cannot get affected by the other processes executing in the system A process is “cooperating” if it can affect or be affected by other processes executing in the system.

MAINLY TWO FUNDAMENTAL MODES:

  • Shared memory
  • Message passing

1) SHARED MEMORY: A process shares a memory in its address space with other processes.

2) MESSAGE PASSING: Allows processes to communicated and to synchronize their actions without sharing the same address space. send (message) receive (message)

In shared- memory systems, system calls are required only to establish shared-memory regions. Whereas message passing systems are typically implemented using system calls. But as the processing cores increases, the message passing is preferred over shared memory because shared memory suffers from “cache coherency issues”, which arise because shared data migrate among the several caches.

Apart from them there are other IPC mechanisms like follow

PIPES:

  • Named pipe.
  • Unnamed pipe (ordinary pipes)

ORDINARY PIPES:

  • Uni directional.
  • One process writes to one end of the pipe (write-end) and other process reads from the other end (read-end).

Sampe code in C to implement using ordinary (unnamed) pipes in UNIX SYSTEMS


// ls -l | more

int fd[2];
pipe(fd);
if (!fork()) {  
	// fd[0] = read
	//fd[1]= write
close (fd[])
dup()   // 0= STDIN , 1=STDOUT
//write (fd)
execlp (“ls”,”ls”,”-1”,NULL);
}
else {
close (fd[0])
dup(0)
execlp (“more”,”more”,NULL);
}

References

[1] Silberschatz, Abraham, Peter Baer Galvin, and Greg Gagne. Operating system concepts essentials. John Wiley & Sons, Inc., 2014.

Leave a comment