0% found this document useful (0 votes)
3 views

section4

Uploaded by

hodhodmohamed86
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

section4

Uploaded by

hodhodmohamed86
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

Operating

System
PRESENTED BY SALMA KISHK
Outline

process introduction
process management Linux
C APIs for process
C code examples
Process
a program in execution
Program is passive entity stored on disk (executable file);process is
active
Program becomes process when executable file loaded into
memory
Process in Memory

c process example
Address Space (Memory)
Process in Memory con’t

A page table is the data structure


used by a virtual memory system in
a computer operating system to
store the mapping between virtual
addresses and physical addresses.

page table
Representation of a Process (inside kernel!)
Process State
ready : the process is loaded into the main
memory. The process here is ready to run and is
waiting to get the CPU time for its execution

run : The process is chosen from the ready


queue by the CPU for execution and the
instructions within the process are executed by
any one of the available CPU cores.

terminated : Process is killed as well as PCB is


deleted. The resources allocated to the process
will be released or deallocated.

waiting : The process continues to wait in the


main memory and does not require CPU. Once
the I/O operation is completed the process goes
to the ready state.
Process Control Block (PCB)
Information associated with each process allocated
in kernel

Process state – running, waiting, etc


Program counter – location of instruction to next execute
CPU registers – contents of all process- centric registers
CPU scheduling information- priorities,
memory allocated to the process
allocated to process, list of open files
context switch
occurs when the CPU
switches from one process to another(i/o request
interrupt, scedule)

The operating system maintains


pointers to each process’s PCB in a
process table so that it can access
the PCB quickly.
Difference between Zombie, Orphan and
Daemon Processes
Difference between Zombie, Orphan and
Daemon Processes

more
A Tree of Processes in Linux

First process is started by the kernel,


Often configured as an argument to
the kernel before the kernel
boots,Often called the “init” process

[kthreadd] is the kernel thread


daemon. ... Creation of new kernel
threads is done via kthreadd
process management Linux
ps
details about running process in the terminal

ps aux
details about all system process

ps -ef
details about all system process & parent
process is
Exambles
pstree
display a tree of processes

ps aux | grep “pattern”


kill

-9 ---> force kill the process


-15 ---> terminate (cnt + c)
-3 ----> quit (q--> less)
-19 ---> stop (cnt + z)
-18 ---> continue stopped process
kill -signal_number PID(process id)
pkill process_name
Background and Foreground Jobs

fg send to foreground
bg send to back ground
pg/fg ---> recent job
pg/fg ---> job name
pg/fg ----> job number
Two command to install c

sudo apt update


sudo apt install build-essentials

The build-essentials packages are the form of meta-packages that are


essential to compile software. They contain the GNU/g++ compiler
collection, GNU debugger, and a few more libraries and tools that are
needed for compiling a program.

video to install and run c script


To create one process

pid > 0

int pid =

pid = 0

call fork()
create process with fork
#include <unistd.h> Fork()

copy the current process


New process has different pid
New process contains a single thread
To create one process

pid > 0

int pid =

pid = 0

if pid >0 then we in parent process


call wait() until child finsh
#include<sys/wait.h>
wait()
A call to wait() blocks the calling process until one of its child
processes exits or a signal is received. After child process terminates,
parent continues its execution after wait system call instruction.

Child process may terminate due to any of these:


It calls exit();
It returns (an int) from main
It receives a signal (from the OS or another process) whose
default action is to terminate
To create one process

pid > 0

int pid =

pid = 0

if pid == 0 then we in child process


exec() some code and then exit()
#include<unistd.h>
execl(), execv(),
The exec family of functions replaces the current running process
with a new process. It can be used to run a C program by using
another C program. It comes under the header file unistd.h.
control execution of process
kill()
#include <signal.h>
The signals are an IPC method and they allow interaction between two or
more running processes. This mechanism consists of a sending process and
one or more receiving processes. The sender sends a predefined signal
consisting of an integer between 0 and 64 to the PID of the receiver. Once
received, the receiver interrupts its task, performs the action, and resumes
the execution at the point where it was interrupted.
multi fork()
multi fork()

output
main process
with
id1 = x

first fork()

child
id1 = 0
z
child process
main process
with
with
id1 = x
id1 = x
id2 = 0

second fork()
child
child
id1 = 0
id1 = 0
id2 = 0
id2 = y

x y
multi fork() in for loop
Any questions ?

You might also like