You are on page 1of 24

Operating Systems (OS)

Process Management

Dr. Najar Yousra


Email : yousra.najar@isi.utm.tn

Med Tech First Year Eng 2021/2022


CS303 Dr. Najar Yousra
Fall 2021 1
Road Map
 Introduction to operating systems

 Process Management

 Interprocessus Synchronisation

 Memory Management

 File System Management


CS303 Dr. Najar Yousra
Fall 2021 2
Week 2 (13/09/2021) : L1 and L2
 Lecture 1 and Lecture 2 explain the
aspects of processes their states, the
threads and the POSIX call systems
dealing with them,

 Recitation 2 : Handling Processes with


POSIX

CS303 Dr. Najar Yousra


Fall 2021 3
Outlines of chapter 2

• The Process Notion


• Operations on Processes (Posix)
• Threading
• Handling Processes and threads with POSIX
• Processor scheduling algorithms
OS : Processes Management
The operating system is responsible for the following activities
in connection with process management:

• Scheduling processes and threads on the CPUs

• Creating and deletingboth user and system processes

• Suspending and resuming processes

• Providing mechanisms for process synchronization

• Providing mechanisms for process communication


1. Process Notion
Process notion
A program does nothing unless its Text editor
instructions are executed by a CPU :
Program code (high level language)
passive entity
compiler

The process is a fundamental Object Program (machine language)

concept of any operating system. A Link editor

process is an execution unit of a Executable program


program : active entity Program loader

Process (loaded in RAM with context)


• A process needs certain resources : CPU time,
Execution
memory, files, and I/O devices to accomplish its
task. Debogger
• These resources are either given to the process
when it is created or allocated to it while it is
running.
Process notion
Although two processes may be associated with the same
program, they are nevertheless considered two separate
execution sequences.

A process it self can be an execution environment for other


code. The Java programming environment provides a good
example.
java Program

The command java runs the JVM as an ordinary process,


which in turns executes the Java program Program in the
virtual machine,
Process in the main memory
To be executed, a process is loaded in the main memory in process adress
space. The memory space used by a process is divided into several
segments.

Code segment : The code segment represents the


program to execute. It is always placed in zones fixed
memory.

Data segment : contains global variables,

Stack segment : contains temporary data (such as


function parameters, return addresses, and local
variables),

Heap segment :which is memory that is


dynamically allocated during process runtime
Process Tree structure
•The processes are organized in a tree structure where each
process has a single parent and can have many children.

•A process is identified by a PID (Process Identifier : long int)


and one PPID (Parent Process Identify).

Example:The process tree in Linux.


Process States
When a process is running it is an active entity : it
changes state. It can be in one of three main states:

• Ready (Ready): the process waits for its turn to


run
• Elected (Running): the instructions are in running.
• Waiting (Blocked): the process blocked in waiting
for event: signal, I / O, ...
• New and terminated
Process States Diagram
Process terminated :
- End of instructions
New - Interruption Terminated
- Error

Running P1

- Demanding unavailable ressource


- Executing an I/O

Ready

P1 Waiting
P2
P3
P4
P5

Ready queue
Process PCB
Each process is represented in the operating system by a process control block
(PCB).
It contains many pieces of information associated with a specific process, including
these:

• Pid (process id), ppid (parent process id), state,


creation time, the amount of CPU and real time
used, adresses in the memory,
• PC (The counter indicates the address of the next
instruction to be executed for this process. )
• List of received signals
• List of opened files
• CPU Registers : The registers vary in number
and type, depending on the computer architecture.
They include accumulators, index registers, stack
pointers, and general-purpose registers, plus any
condition-code information.
• CPU Scheduling information : priority,
pointers to scheduling queues,
Switching Context
 On a multiprogrammed system, the OS must give back control of
the processor from one process to another by performing context
switches.
 Context switching consists in memorizing the PCB
of the current process and load the PCB of the process to be
elected
Process Table
 Operating systems manipulate one main data structure to manage the
created processes on a computer: the process table.
The process table contains all the information essential to the operating
system to ensure consistent management of processes. It is stored in the
operating system's memory space, which means that processes cannot
access it.
Process …..
 Zombie processes
Is a term denoting a process that ended, but still has an identifier (PID) and
therefore remains visible in the table of process.
 Orphan processes
Is a term denoting a process whose father is terminated before him.
 Threads
 A process contains at least one single thread of control.
 A thread or even a light process (lightweight process) is a code execution
unit.
Operations on Processes (POSIX)
 Process Creation with fork()
• A process may create several new processes.
• The creating process is called a parent process, and the new processes are called
the children of that process.
• On UNIX and Linux systems, we can obtain a listing of processes by using the ps
command: ps -el
Operations on Processes (POSIX)
 Process Creation with fork()
• When a process creates a new process, the parent continues to execute
concurrently with its children.
• A new process is created by the fork() system call.
• The new process consists of a copy of the address space of the original process.
• Both processes (the parent and the child) continue execution at the instruction
after the fork(), with one difference:
• the return code for the fork() is zero for the new (child) process, whereas the
(nonzero) process identifier of the child is returned to the parent

#include <sys/types.h>
#include <unistd.h>
pid_t fork()
Operations on Processes (POSIX)
 Process Creation with fork()

$ gcc fils.c –o fils


$ Je suis le père avec pid 4130
$ Je suis le fils avec pid 4131
Operations on Processes (POSIX)
 Process Terminisation with exit()
A process terminates when it finishes executing its final statement and asks the
operating system to delete it by using the exit() system call. At that point, the
process may return a status value (typically an integer) to its parent process (via the
wait() system call). All the resources of the process—including physical and virtual
memory, open files, and I/O buffers—are deallocated by the operating system.
Termination can occur in other circumstances as well : interruption, bug ….

#include <unistd.h>
Void exit(int returncode);

Getpid and Getppid

Pid_t getpid() : returns the pid of the process executing the system call
Pid_t getppid() : returns the pid of the parent process executing the system call
Operations on Processes (POSIX)

 Exec family : After a fork() system call, one of the two processes typically
uses the exec() system call to replace the process’s memory space with
a new program. The exec() system call loads a binary file into memory
(destroying the memory image of the program containing the exec()
system call) and starts its execution.

char *arguments[4]
...
#include <unistd.h> arguments[0]="/bin/ls";
int execl(const char *path, const char *argv, ...); arguments[1]="-l";
int execv(const char *path, const char *argv[]); arguments[2]="/etc";
int execlp(const char *file, const char *argv, ...); arguments[3]="NULL";
int execvp(const char *file, const char *argv[]);
execv("/bin/ls", arguments);
execl("/bin/ls", "/bin/ls " ,"-l" , "/etc" , "NULL");
execvp("ls", arguments);
execlp("ls", "ls " ,"-l" , "/etc" , "NULL");
Operations on Processes (POSIX)
 Wait and waitpid
The parent can create more children; or, if it has nothing else to do while the child
runs, it can issue a wait() system call to move it self off the ready queue until the
termination of the child.

A parent process may wait for the termination of a child process by using the wait()
system call. The wait() system call is passed a parameter that allows the parent to
obtain the exit status of the child. This system call also returns the process identifier
of the terminated child so that the parent can tell which of its children has
terminated:

#include <wait.h>
int status;
int pid ;
pid = wait(&status);
pid = waitpid(int pid, &status);
Operations on Processes (POSIX)
Example :
Operations on Processes (POSIX)
Application : Write prog.c that implements this process tree.

Waits for all his children


P1

Prints the pid of his child and Prints its pid in the pid of his father
waits for his terminisation
P2 P3

Executes « ls –l »
P4

You might also like