SlideShare une entreprise Scribd logo
1  sur  33
© 2010-2019 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Processes
2© 2010-2019 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
What to Expect?
W's of a Process
Processes in Linux
Scheduling & Preemption
Process States & Transitions
Process Management
Programming the Processes
3© 2010-2019 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
What is a Process?
Program in Execution
Executable/Program Loaded → Process
Program is just the Code & initial Data part
Additionally
Value of Variables
Stack
Heap
Program Counter
Processor Registers
And any other OS resources, needed by the Program
make it a Process
4© 2010-2019 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Why we need a Process?
To do any task or job
Moreover, to achieve multi-processing
Really do multiple tasks at a time (in multi-processor systems),
Or
At least get a feel of doing multiple tasks at a time (on uni-
processor systems)
In turn needs
Timesharing (on same processor)
Scheduling
Priority
And for all these: Process Identifier (pid)
5© 2010-2019 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Let's View
Shell Local Processes: ps
Console attached System Processes: ps a
All System Processes: ps ax
List many more details: Add l
Observe
uid, pid, ppid, priority, nice, status, tty, time
Dynamic Process Status: top
Try pid.c
6© 2010-2019 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Processes in Linux
7© 2010-2019 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Linux Schedulers
Provide multi-tasking capabilities by
Time Slicing
Preemption
Based on various task priorities
Specified by its scheduling policies
Understand the following execution instances
Kernel Thread
User Process
User Thread
8© 2010-2019 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Linux Schedulers ...
Linux Basic Scheduling
Normal (SCHED_OTHER) – Fairness Scheduling
Other Advanced Scheduling supported
Round Robin (SCHED_RR)
FIFO (SCHED_FIFO)
All Schedulers are O(1)
9© 2010-2019 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Linux Kernel Preemption Levels
None (PREEMPT_NONE)
No forced preemption
Overall throughput, on average, is good
Voluntary (PREEMPT_VOLUNTARY)
First stage of latency reduction
Explicit preemption points are placed at strategic locations
Standard (PREEMPT_DESKTOP)
Preemption enabled everywhere except within critical sections
Good for soft real-time applications, like audio, multimedia, …
Kernel Parameter: preempt
10© 2010-2019 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Kernel Preemption Visualization
Process A
Process B
User Space
Kernel Space
System Call
Interface
Time
11© 2010-2019 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Process Context Switch
Process A
Process B
Time
Save State
into PCB A
Reload State
from PCB B
Save State
into PCB B
Reload State
from PCB A
Time Wasted in Context Switch
Interrupt or System Call Interrupt or System Call
12© 2010-2019 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Generic Process State Diagram
Terminated
RunningReady
New
Blocked
ExitDispatch
Wakeup on
I/O or event
completion
Admitted
Block on
I/O or
wait event
Time Run-Out
Ready & Blocked states have Queues
Additional States in Linux
Defunct / Zombie (Terminated but not reaped by its parent)
Stopped (By job control signal or because being traced)
Uninterruptible or Interruptible
Zombie
Stopped
13© 2010-2019 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Linux Process States
Process States as in Linux
TASK_RUNNING (R) – ready or running
TASK_INTERRUPTIBLE (S) – blocked (waiting for an
event)
TASK_UNINTERRUPTIBLE (D) – blocked (usually for I/O)
TASK_ZOMBIE (Z) – terminated but not cleaned up by its
parent
TASK_STOPPED (T) – execution stopped
Mutually exclusive
Additional Information: Foreground (+), Threaded (l)
14© 2010-2019 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Process Management in Linux
Needs collation of all this information
Address Space (Code, Variables, Stack, Heap)
Processor State (PC, Registers, …)
OS resources in use (File descriptors, ...)
Scheduling Info, Priority, ...
Preemption Info, Process State, ...
for every Process
Stored in structure of type 'task_struct'
Maintained by Linux Kernel on a per process basis
Also called the Process Descriptor / Process Control Block
15© 2010-2019 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Process Control Block (PCB)
Listing: <kernel_source>/include/linux/sched.h
Some of its fields are
volatile long state; /* -1 unrunnable, 0 runnable, >0 stopped */
void *stack;
unsigned int flags; /* per process flags */
int prio, static_prio, normal_prio;
unsigned int rt_priority;
const struct sched_class *sched_class;
unsigned int policy;
struct mm_struct *mm, active_mm; / Pointers to Memory Regions, Descriptors */
pid_t pid, tgid;
struct task_struct *real_parent; /* real parent process */
struct task_struct *parent; /* recipient of SIGCHLD, wait4() reports */
struct list_head children /* list of its children */, sibling; /* linkage in its parent's children list */
struct task_struct *group_leader; /* threadgroup leader */
struct list_head thread_group;
struct fs_struct fs; /* file system info like current directory, … */
struct files_struct files; / file descriptors */
struct signal_struct signal; / signal handlers */
sigset_t blocked, real_blocked, saved_sigmask;
struct sigpending pending; /* signals received */
16© 2010-2019 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Basic Process Management
bg - Starts a suspended process in the
background
fg - Starts a suspended process in the
foreground
jobs - Lists the jobs running
pidof - Find the process ID of a running program
top - Display the processes that are using the
most CPU resources
17© 2010-2019 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Programming Linux Processes
18© 2010-2019 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Process Creation
From Shell
By running a Command / Program / Script (Even by .)
By 'exec' ing a Command / Program
By Programming
Using system()
Simple but Inefficient
Security risks
Using fork() and exec() family function
Comparatively complex
Greater flexibility, speed, and security
19© 2010-2019 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
System function
Used to execute the command from within the
program
Creates the subprocess running the system shell
& hands the command to that shell for execution
Subject to the features & limitations of the
system shell
Try system.c
20© 2010-2019 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
fork()
Creates the duplicate of the calling process
Process invoking the fork() becomes the Parent
of newly created Child process.
Child process too executes the same program
from the same place
All the statements after the call to fork are
executed twice
Try fork.c
21© 2010-2019 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Distinguishing Child & Parent
fork() provides the different return values to the
parent & child process.
One process “goes in” to the fork call & 2
processes “come out” with different return values
Return value in the parent process is the process
ID of the child
Return value in the child process is 0.
Try fork_basic.c
22© 2010-2019 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Using the exec family
All exec* functions do the same thing
Just in different ways
Replaces current program by a new one
And hence never returns, unless an error
New program is immediately started
Process remains the same
Process Id, Parent Process Id
Current directory, ...
Open file descriptor tables, …
Try exec_start.c
23© 2010-2019 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
exec* function specifics
exec*p (execvp, execlp)
Accepts a program name
Searches it in the current execution path
Others must be given the full path
execv* (execv, execvp, execve)
Argument list should be a NULL-terminated array of pointers to strings
execl* (execl, execlp, execle)
Argument list uses the varargs mechanism
exec*e (execve, execle)
Accepts an additional argument: an array of environment variables
It should be a NULL-terminated array of pointers to character strings
Each character string should be of the form “VARIABLE=value”
24© 2010-2019 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Using fork & exec together
Allows the program calling exec to continue
execution after exec
First fork a program & then exec the subprogram
in child process
Original program continues in the parent process
& new program is executed in child process
Try fork_execv.c
25© 2010-2019 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Cons of using fork
Forked child process executes the copy of parent
process' program
And typically, a fork is followed by exec
Replacing the copy by the new program
What is the point of copying?
Overhead!! What else?
Is there any way out to prevent this?
Yes. And it is ...
26© 2010-2019 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Copy On Write (COW)
Parent and Child shares the Address Space (ro)
Data & other Resources are marked COW
If written to, a duplicate is made and each process
receives a unique copy
Consequently, the duplication of resources occurs only
when they are written to
Avoids copy in cases of immediate exec
fork()'s only overheads
Duplication of the parent's page tables
Creation of a unique PCB for the child
27© 2010-2019 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Process Termination
Parent & Children Processes terminate as usual
Success Exit – Normal Success
Error Exit – Normal Failure
Fatal Exit – Signaled from Kernel Space for a Bug
Kill Exit – Signaled by a Process
But which one of them terminates, first?
Does it matter?
If it matters, parents can wait for their children
Using wait family of system calls
And can retrieve information about its child’s termination
In fact, wait does the cleanup act for the exited child
28© 2010-2019 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Using the wait family
Four different system calls in the wait family
wait(): Block until one of its child processes exits
waitpid(): Wait for a specific child to exit/stop/resume
wait3(): Along with, return resource usage information about the
exiting/stopping/resuming child process
wait4(): wait3() for a specific child or children
All of these fill up a status code
in an integer pointer argument
about how the child process exited
which can be decoded using …
Try wait_basic.c
29© 2010-2019 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
wait status macros
WIFEXITED, WEXITSTATUS
WIFSIGNALED, WTERMSIG, WCOREDUMP
WIFSTOPPED, WSTOPSIG
WIFCONTINUED
30© 2010-2019 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
What if Parents Don't Wait?
If Parent dies before the Children
Children become Orphans
And are adopted by the Init Process
which then does the cleanup on their exit
If a Child exits before the Parent
That's a sad thing :)
It will become a Ghost / Zombie
Note that, even if the parent does a wait later
It remains a Zombie till then
31© 2010-2019 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Who Cleans Up a Zombie?
Typically, again a Parent
By doing a wait on it
What if the parent exits without “wait”?
Does it stay around in the system?
Not really. It gets inherited by init
which then cleans it up, right there
32© 2010-2019 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
What all have we learnt?
W's of a Process?
Process Scheduling & Preemption in Linux
Process States & Transitions (Linux specific)
Linux Process Management using PCB
Programming Linux Processes
Creation Techniques: fork, exec* & COW
Termination & Waiting Techniques
Orphans & Zombies
33© 2010-2019 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Any Queries?

Contenu connexe

Tendances (20)

Embedded Software Design
Embedded Software DesignEmbedded Software Design
Embedded Software Design
 
Introduction to Linux
Introduction to LinuxIntroduction to Linux
Introduction to Linux
 
BeagleBone Black Bootloaders
BeagleBone Black BootloadersBeagleBone Black Bootloaders
BeagleBone Black Bootloaders
 
SPI Drivers
SPI DriversSPI Drivers
SPI Drivers
 
Character Drivers
Character DriversCharacter Drivers
Character Drivers
 
Signals
SignalsSignals
Signals
 
I2C Drivers
I2C DriversI2C Drivers
I2C Drivers
 
Kernel Debugging & Profiling
Kernel Debugging & ProfilingKernel Debugging & Profiling
Kernel Debugging & Profiling
 
System Calls
System CallsSystem Calls
System Calls
 
Kernel Timing Management
Kernel Timing ManagementKernel Timing Management
Kernel Timing Management
 
Video Drivers
Video DriversVideo Drivers
Video Drivers
 
Synchronization
SynchronizationSynchronization
Synchronization
 
Toolchain
ToolchainToolchain
Toolchain
 
Block Drivers
Block DriversBlock Drivers
Block Drivers
 
Linux Network Management
Linux Network ManagementLinux Network Management
Linux Network Management
 
Introduction to Embedded Systems
Introduction to Embedded SystemsIntroduction to Embedded Systems
Introduction to Embedded Systems
 
Linux DMA Engine
Linux DMA EngineLinux DMA Engine
Linux DMA Engine
 
BeagleBoard-xM Booting Process
BeagleBoard-xM Booting ProcessBeagleBoard-xM Booting Process
BeagleBoard-xM Booting Process
 
Linux Internals Part - 3
Linux Internals Part - 3Linux Internals Part - 3
Linux Internals Part - 3
 
Linux Porting
Linux PortingLinux Porting
Linux Porting
 

Similaire à Processes

Similaire à Processes (20)

Processes
ProcessesProcesses
Processes
 
14712-l4.pptx
14712-l4.pptx14712-l4.pptx
14712-l4.pptx
 
ch03.pptx
ch03.pptxch03.pptx
ch03.pptx
 
Processes
ProcessesProcesses
Processes
 
ch3.pptx
ch3.pptxch3.pptx
ch3.pptx
 
ch3.pptx
ch3.pptxch3.pptx
ch3.pptx
 
ch3.ppt
ch3.pptch3.ppt
ch3.ppt
 
Lecture_Slide_4.pptx
Lecture_Slide_4.pptxLecture_Slide_4.pptx
Lecture_Slide_4.pptx
 
Unit II - 1 - Operating System Process
Unit II - 1 - Operating System ProcessUnit II - 1 - Operating System Process
Unit II - 1 - Operating System Process
 
ch3-lect7.pptx
ch3-lect7.pptxch3-lect7.pptx
ch3-lect7.pptx
 
Ch3OperSys
Ch3OperSysCh3OperSys
Ch3OperSys
 
OperatingSystemChp3
OperatingSystemChp3OperatingSystemChp3
OperatingSystemChp3
 
3.5 create, monitor and kill processes v2
3.5 create, monitor and kill processes v23.5 create, monitor and kill processes v2
3.5 create, monitor and kill processes v2
 
Week03-Ch3-Process Concept.pptx.gfhgvjhg
Week03-Ch3-Process Concept.pptx.gfhgvjhgWeek03-Ch3-Process Concept.pptx.gfhgvjhg
Week03-Ch3-Process Concept.pptx.gfhgvjhg
 
ch3_LU6_LU7_Lecture_1691052564579.pptx
ch3_LU6_LU7_Lecture_1691052564579.pptxch3_LU6_LU7_Lecture_1691052564579.pptx
ch3_LU6_LU7_Lecture_1691052564579.pptx
 
Apache Flink Worst Practices
Apache Flink Worst PracticesApache Flink Worst Practices
Apache Flink Worst Practices
 
Inter Process Communication
Inter Process CommunicationInter Process Communication
Inter Process Communication
 
2.ch3 Process (1).ppt
2.ch3 Process (1).ppt2.ch3 Process (1).ppt
2.ch3 Process (1).ppt
 
ch3 (1).ppt
ch3 (1).pptch3 (1).ppt
ch3 (1).ppt
 
ch3.ppt
ch3.pptch3.ppt
ch3.ppt
 

Plus de Anil Kumar Pugalia (18)

File System Modules
File System ModulesFile System Modules
File System Modules
 
Embedded C
Embedded CEmbedded C
Embedded C
 
Playing with R L C Circuits
Playing with R L C CircuitsPlaying with R L C Circuits
Playing with R L C Circuits
 
Audio Drivers
Audio DriversAudio Drivers
Audio Drivers
 
Shell Scripting
Shell ScriptingShell Scripting
Shell Scripting
 
References
ReferencesReferences
References
 
Functional Programming with LISP
Functional Programming with LISPFunctional Programming with LISP
Functional Programming with LISP
 
Power of vi
Power of viPower of vi
Power of vi
 
gcc and friends
gcc and friendsgcc and friends
gcc and friends
 
"make" system
"make" system"make" system
"make" system
 
Hardware Design for Software Hackers
Hardware Design for Software HackersHardware Design for Software Hackers
Hardware Design for Software Hackers
 
RPM Building
RPM BuildingRPM Building
RPM Building
 
System Calls
System CallsSystem Calls
System Calls
 
Timers
TimersTimers
Timers
 
Threads
ThreadsThreads
Threads
 
Linux Memory Management
Linux Memory ManagementLinux Memory Management
Linux Memory Management
 
Linux File System
Linux File SystemLinux File System
Linux File System
 
Introduction to Linux
Introduction to LinuxIntroduction to Linux
Introduction to Linux
 

Dernier

Glenn Lazarus- Why Your Observability Strategy Needs Security Observability
Glenn Lazarus- Why Your Observability Strategy Needs Security ObservabilityGlenn Lazarus- Why Your Observability Strategy Needs Security Observability
Glenn Lazarus- Why Your Observability Strategy Needs Security Observabilityitnewsafrica
 
QCon London: Mastering long-running processes in modern architectures
QCon London: Mastering long-running processes in modern architecturesQCon London: Mastering long-running processes in modern architectures
QCon London: Mastering long-running processes in modern architecturesBernd Ruecker
 
Infrared simulation and processing on Nvidia platforms
Infrared simulation and processing on Nvidia platformsInfrared simulation and processing on Nvidia platforms
Infrared simulation and processing on Nvidia platformsYoss Cohen
 
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality AssuranceInflectra
 
Potential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsPotential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsRavi Sanghani
 
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...Nikki Chapple
 
Bridging Between CAD & GIS: 6 Ways to Automate Your Data Integration
Bridging Between CAD & GIS:  6 Ways to Automate Your Data IntegrationBridging Between CAD & GIS:  6 Ways to Automate Your Data Integration
Bridging Between CAD & GIS: 6 Ways to Automate Your Data Integrationmarketing932765
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxLoriGlavin3
 
Design pattern talk by Kaya Weers - 2024 (v2)
Design pattern talk by Kaya Weers - 2024 (v2)Design pattern talk by Kaya Weers - 2024 (v2)
Design pattern talk by Kaya Weers - 2024 (v2)Kaya Weers
 
Microservices, Docker deploy and Microservices source code in C#
Microservices, Docker deploy and Microservices source code in C#Microservices, Docker deploy and Microservices source code in C#
Microservices, Docker deploy and Microservices source code in C#Karmanjay Verma
 
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentEmixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentPim van der Noll
 
2024 April Patch Tuesday
2024 April Patch Tuesday2024 April Patch Tuesday
2024 April Patch TuesdayIvanti
 
Generative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfGenerative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfIngrid Airi González
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxLoriGlavin3
 
UiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPathCommunity
 
A Framework for Development in the AI Age
A Framework for Development in the AI AgeA Framework for Development in the AI Age
A Framework for Development in the AI AgeCprime
 
Decarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityDecarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityIES VE
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxLoriGlavin3
 
Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Hiroshi SHIBATA
 
Modern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better StrongerModern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better Strongerpanagenda
 

Dernier (20)

Glenn Lazarus- Why Your Observability Strategy Needs Security Observability
Glenn Lazarus- Why Your Observability Strategy Needs Security ObservabilityGlenn Lazarus- Why Your Observability Strategy Needs Security Observability
Glenn Lazarus- Why Your Observability Strategy Needs Security Observability
 
QCon London: Mastering long-running processes in modern architectures
QCon London: Mastering long-running processes in modern architecturesQCon London: Mastering long-running processes in modern architectures
QCon London: Mastering long-running processes in modern architectures
 
Infrared simulation and processing on Nvidia platforms
Infrared simulation and processing on Nvidia platformsInfrared simulation and processing on Nvidia platforms
Infrared simulation and processing on Nvidia platforms
 
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
 
Potential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsPotential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and Insights
 
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...
 
Bridging Between CAD & GIS: 6 Ways to Automate Your Data Integration
Bridging Between CAD & GIS:  6 Ways to Automate Your Data IntegrationBridging Between CAD & GIS:  6 Ways to Automate Your Data Integration
Bridging Between CAD & GIS: 6 Ways to Automate Your Data Integration
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptx
 
Design pattern talk by Kaya Weers - 2024 (v2)
Design pattern talk by Kaya Weers - 2024 (v2)Design pattern talk by Kaya Weers - 2024 (v2)
Design pattern talk by Kaya Weers - 2024 (v2)
 
Microservices, Docker deploy and Microservices source code in C#
Microservices, Docker deploy and Microservices source code in C#Microservices, Docker deploy and Microservices source code in C#
Microservices, Docker deploy and Microservices source code in C#
 
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentEmixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
 
2024 April Patch Tuesday
2024 April Patch Tuesday2024 April Patch Tuesday
2024 April Patch Tuesday
 
Generative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfGenerative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdf
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
 
UiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to Hero
 
A Framework for Development in the AI Age
A Framework for Development in the AI AgeA Framework for Development in the AI Age
A Framework for Development in the AI Age
 
Decarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityDecarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a reality
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
 
Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024
 
Modern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better StrongerModern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
 

Processes

  • 1. © 2010-2019 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Processes
  • 2. 2© 2010-2019 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. What to Expect? W's of a Process Processes in Linux Scheduling & Preemption Process States & Transitions Process Management Programming the Processes
  • 3. 3© 2010-2019 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. What is a Process? Program in Execution Executable/Program Loaded → Process Program is just the Code & initial Data part Additionally Value of Variables Stack Heap Program Counter Processor Registers And any other OS resources, needed by the Program make it a Process
  • 4. 4© 2010-2019 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Why we need a Process? To do any task or job Moreover, to achieve multi-processing Really do multiple tasks at a time (in multi-processor systems), Or At least get a feel of doing multiple tasks at a time (on uni- processor systems) In turn needs Timesharing (on same processor) Scheduling Priority And for all these: Process Identifier (pid)
  • 5. 5© 2010-2019 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Let's View Shell Local Processes: ps Console attached System Processes: ps a All System Processes: ps ax List many more details: Add l Observe uid, pid, ppid, priority, nice, status, tty, time Dynamic Process Status: top Try pid.c
  • 6. 6© 2010-2019 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Processes in Linux
  • 7. 7© 2010-2019 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Linux Schedulers Provide multi-tasking capabilities by Time Slicing Preemption Based on various task priorities Specified by its scheduling policies Understand the following execution instances Kernel Thread User Process User Thread
  • 8. 8© 2010-2019 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Linux Schedulers ... Linux Basic Scheduling Normal (SCHED_OTHER) – Fairness Scheduling Other Advanced Scheduling supported Round Robin (SCHED_RR) FIFO (SCHED_FIFO) All Schedulers are O(1)
  • 9. 9© 2010-2019 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Linux Kernel Preemption Levels None (PREEMPT_NONE) No forced preemption Overall throughput, on average, is good Voluntary (PREEMPT_VOLUNTARY) First stage of latency reduction Explicit preemption points are placed at strategic locations Standard (PREEMPT_DESKTOP) Preemption enabled everywhere except within critical sections Good for soft real-time applications, like audio, multimedia, … Kernel Parameter: preempt
  • 10. 10© 2010-2019 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Kernel Preemption Visualization Process A Process B User Space Kernel Space System Call Interface Time
  • 11. 11© 2010-2019 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Process Context Switch Process A Process B Time Save State into PCB A Reload State from PCB B Save State into PCB B Reload State from PCB A Time Wasted in Context Switch Interrupt or System Call Interrupt or System Call
  • 12. 12© 2010-2019 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Generic Process State Diagram Terminated RunningReady New Blocked ExitDispatch Wakeup on I/O or event completion Admitted Block on I/O or wait event Time Run-Out Ready & Blocked states have Queues Additional States in Linux Defunct / Zombie (Terminated but not reaped by its parent) Stopped (By job control signal or because being traced) Uninterruptible or Interruptible Zombie Stopped
  • 13. 13© 2010-2019 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Linux Process States Process States as in Linux TASK_RUNNING (R) – ready or running TASK_INTERRUPTIBLE (S) – blocked (waiting for an event) TASK_UNINTERRUPTIBLE (D) – blocked (usually for I/O) TASK_ZOMBIE (Z) – terminated but not cleaned up by its parent TASK_STOPPED (T) – execution stopped Mutually exclusive Additional Information: Foreground (+), Threaded (l)
  • 14. 14© 2010-2019 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Process Management in Linux Needs collation of all this information Address Space (Code, Variables, Stack, Heap) Processor State (PC, Registers, …) OS resources in use (File descriptors, ...) Scheduling Info, Priority, ... Preemption Info, Process State, ... for every Process Stored in structure of type 'task_struct' Maintained by Linux Kernel on a per process basis Also called the Process Descriptor / Process Control Block
  • 15. 15© 2010-2019 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Process Control Block (PCB) Listing: <kernel_source>/include/linux/sched.h Some of its fields are volatile long state; /* -1 unrunnable, 0 runnable, >0 stopped */ void *stack; unsigned int flags; /* per process flags */ int prio, static_prio, normal_prio; unsigned int rt_priority; const struct sched_class *sched_class; unsigned int policy; struct mm_struct *mm, active_mm; / Pointers to Memory Regions, Descriptors */ pid_t pid, tgid; struct task_struct *real_parent; /* real parent process */ struct task_struct *parent; /* recipient of SIGCHLD, wait4() reports */ struct list_head children /* list of its children */, sibling; /* linkage in its parent's children list */ struct task_struct *group_leader; /* threadgroup leader */ struct list_head thread_group; struct fs_struct fs; /* file system info like current directory, … */ struct files_struct files; / file descriptors */ struct signal_struct signal; / signal handlers */ sigset_t blocked, real_blocked, saved_sigmask; struct sigpending pending; /* signals received */
  • 16. 16© 2010-2019 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Basic Process Management bg - Starts a suspended process in the background fg - Starts a suspended process in the foreground jobs - Lists the jobs running pidof - Find the process ID of a running program top - Display the processes that are using the most CPU resources
  • 17. 17© 2010-2019 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Programming Linux Processes
  • 18. 18© 2010-2019 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Process Creation From Shell By running a Command / Program / Script (Even by .) By 'exec' ing a Command / Program By Programming Using system() Simple but Inefficient Security risks Using fork() and exec() family function Comparatively complex Greater flexibility, speed, and security
  • 19. 19© 2010-2019 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. System function Used to execute the command from within the program Creates the subprocess running the system shell & hands the command to that shell for execution Subject to the features & limitations of the system shell Try system.c
  • 20. 20© 2010-2019 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. fork() Creates the duplicate of the calling process Process invoking the fork() becomes the Parent of newly created Child process. Child process too executes the same program from the same place All the statements after the call to fork are executed twice Try fork.c
  • 21. 21© 2010-2019 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Distinguishing Child & Parent fork() provides the different return values to the parent & child process. One process “goes in” to the fork call & 2 processes “come out” with different return values Return value in the parent process is the process ID of the child Return value in the child process is 0. Try fork_basic.c
  • 22. 22© 2010-2019 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Using the exec family All exec* functions do the same thing Just in different ways Replaces current program by a new one And hence never returns, unless an error New program is immediately started Process remains the same Process Id, Parent Process Id Current directory, ... Open file descriptor tables, … Try exec_start.c
  • 23. 23© 2010-2019 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. exec* function specifics exec*p (execvp, execlp) Accepts a program name Searches it in the current execution path Others must be given the full path execv* (execv, execvp, execve) Argument list should be a NULL-terminated array of pointers to strings execl* (execl, execlp, execle) Argument list uses the varargs mechanism exec*e (execve, execle) Accepts an additional argument: an array of environment variables It should be a NULL-terminated array of pointers to character strings Each character string should be of the form “VARIABLE=value”
  • 24. 24© 2010-2019 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Using fork & exec together Allows the program calling exec to continue execution after exec First fork a program & then exec the subprogram in child process Original program continues in the parent process & new program is executed in child process Try fork_execv.c
  • 25. 25© 2010-2019 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Cons of using fork Forked child process executes the copy of parent process' program And typically, a fork is followed by exec Replacing the copy by the new program What is the point of copying? Overhead!! What else? Is there any way out to prevent this? Yes. And it is ...
  • 26. 26© 2010-2019 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Copy On Write (COW) Parent and Child shares the Address Space (ro) Data & other Resources are marked COW If written to, a duplicate is made and each process receives a unique copy Consequently, the duplication of resources occurs only when they are written to Avoids copy in cases of immediate exec fork()'s only overheads Duplication of the parent's page tables Creation of a unique PCB for the child
  • 27. 27© 2010-2019 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Process Termination Parent & Children Processes terminate as usual Success Exit – Normal Success Error Exit – Normal Failure Fatal Exit – Signaled from Kernel Space for a Bug Kill Exit – Signaled by a Process But which one of them terminates, first? Does it matter? If it matters, parents can wait for their children Using wait family of system calls And can retrieve information about its child’s termination In fact, wait does the cleanup act for the exited child
  • 28. 28© 2010-2019 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Using the wait family Four different system calls in the wait family wait(): Block until one of its child processes exits waitpid(): Wait for a specific child to exit/stop/resume wait3(): Along with, return resource usage information about the exiting/stopping/resuming child process wait4(): wait3() for a specific child or children All of these fill up a status code in an integer pointer argument about how the child process exited which can be decoded using … Try wait_basic.c
  • 29. 29© 2010-2019 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. wait status macros WIFEXITED, WEXITSTATUS WIFSIGNALED, WTERMSIG, WCOREDUMP WIFSTOPPED, WSTOPSIG WIFCONTINUED
  • 30. 30© 2010-2019 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. What if Parents Don't Wait? If Parent dies before the Children Children become Orphans And are adopted by the Init Process which then does the cleanup on their exit If a Child exits before the Parent That's a sad thing :) It will become a Ghost / Zombie Note that, even if the parent does a wait later It remains a Zombie till then
  • 31. 31© 2010-2019 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Who Cleans Up a Zombie? Typically, again a Parent By doing a wait on it What if the parent exits without “wait”? Does it stay around in the system? Not really. It gets inherited by init which then cleans it up, right there
  • 32. 32© 2010-2019 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. What all have we learnt? W's of a Process? Process Scheduling & Preemption in Linux Process States & Transitions (Linux specific) Linux Process Management using PCB Programming Linux Processes Creation Techniques: fork, exec* & COW Termination & Waiting Techniques Orphans & Zombies
  • 33. 33© 2010-2019 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Any Queries?