SlideShare a Scribd company logo
1 of 42
© 2014-2015 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Linux Kernel Process Management
2
© 2014-2015 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
What to Expect?
W's of Threads & Kthreads
W's of Concurrency Management
Waiting / Sleeping in a process
'select & poll' support in kernel
3
© 2014-2015 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
W's of Threads
Basic unit of CPU utilization
Also, known as Light weight process
Implemented as POSIX threads in user space,
commonly known as pthread
Multiple threads share the same address space in
a process
Communication is usually through global variables
4
© 2014-2015 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
W's of Kernel Threads
Exist in Kernel Space
Used to perform some tasks in background
Are descended from kthreadd
Can be viewed in user space with command
'ps -fx' or, 'ps -mx'
Examples:
khubd
ksoftirqd
kworker
5
© 2014-2015 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Creating a Kernel Thread
#include <kthread.h>
kthread_create(int (*function)(void *data), void *data, const char
name[], ...)
Parameters:
function – The function that the thread has to execute
data – The 'data' to be passed to the function
name – The name by which the process will be recognized in the kernel
Returns:
Pointer to a structure of type task_struct
Try Threads/thread.c
6
© 2014-2015 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Waking Up & Stopping a Thread
wake_up_process(struct task_struct *)
Function passed to kthread_create() gets executed
Parameters:
Pointer to struct of type task_struct
kthread_stop(struct task_struct *)
Waits for the thread to stop
Parameters
Pointer to structure of type task_struct
Returns:
Result of thread function
Try Threads/thread1.c and Threads/thread_stop.c
7
© 2014-2015 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Running a Thread
kthread_run(int (*function)(void *data), void *data,
const char name[], ...)
Creates & starts the threads
Parameters:
function – The function that the thread has to execute
data – The 'data' to be passed to the function
name – The name by which the process will be recognized in the
kernel
Try Threads/thread_run.c
© 2014-2015 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Concurrency Management
9
© 2014-2015 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Concurrency Issues
Multitasking leads to the uncontrolled access to
the shared resources
Easiest to create & hard to debug
May lead to memory leak
Global variable should best be avoided
If can't avoid global variables, then need to
resort to the synchronization mechanisms
10
© 2014-2015 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Mutex
Stands for MUTual EXclusion
Only one thread of execution can hold it at a
time.
Has operations such as lock & unlock.
'lock' call blocks if mutex is not available & the
process goes to sleep
11
© 2014-2015 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Data Structures for Mutex
#include <linux/mutex.h>
struct mutex
Initialization
Statically
DEFINE_MUTEX(mutex)
Dynamically
mutex_init
Operations
mutex_lock()
mutex_unlock()
mutex_lock_interruptible()
mutex_trylock()
12
© 2014-2015 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Hands On
Try Synchronization/mutex.c and
Synchronization/mutex1.c
13
© 2014-2015 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Semaphore
Semaphore is a counter, which keeps track of
resources
Has two operations – wait/down & post/up
Wait decrements the count by one
Post increments the count by one
Process blocks if the value of count is zero.
14
© 2014-2015 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Data Structures for Semaphore
#include <linux/semaphore.h>
struct semaphore
Initialization
Statically
DEFINE_SEMAPHORE(name)
Dynamically
sema_init(&sem, val)
Operations
void down((&sem)
int down_interruptible(&sem)
Int down_trylock()
void up(&sem)
15
© 2014-2015 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Hands On
Try Synchronization/sem.c
Try Synchronization/cons_prod.c
Modify cons_prod.c to avoid exit of consumers
threads
16
© 2014-2015 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Reader/Writer Semaphores
#include <linux/rwsem.h>
structure rw_semaphore
Allows one writer or unlimited number of readers to hold the
semaphore
Intialization:
void init_rwsem(&rwsem)
Operations for Readers
void down_read(&sem)
int down_read_trylock(&sem)
void up_read(&sem)
17
© 2014-2015 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Reader/Writer Semaphores...
Operations for Writers
void down_write(struct rw_semaphore *sem)
int down_write_trylock(struct rw_semaphore *sem)
void up_write(struct rw_semaphore *sem)
Try Synchronization/rwsem.c
18
© 2014-2015 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Spinlocks
Provides synchronization mechanism for the code
which can't sleep. Example – Interrupt handlers
Two states – locked & unlocked
If lock is available, code enters the critical section
If lock is unavailable, code goes into the tight loop.
So, the name spinlock
19
© 2014-2015 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Spinlock Data Structures
#include <linux/spinlock.h>
Type: spinlock_t
spin_lock_init(&my_slock)
spin_lock(&my_slock)
spin_unlock(&my_slock)
Try spinlock_example.c
20
© 2014-2015 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Cautions while holding a spin lock
Avoid the code which can sleep
Preemption is disabled by spin lock code
Critical section should be fast & short
Sometimes, disabling of interrupts on the local
processor is required to avoid deadlock
21
© 2014-2015 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Other spinlock functions
spin_lock_irqsave(&my_lock, flags)
Disables interrupts on the local processor
Interrupt state is saved in flags
spin_lock_irq(&my_slock)
Disables interrupts, but doesn't keeps track of flags
spin_lock_bh
Disables software interrupts only
22
© 2014-2015 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
spin_unlock variants
void spin_unlock_irqrestore(&my_slock, flags)
Unlocks & restores the irqs
void spin_unlock_irq(&my_slock)
void spin_unlock_bh(&my_slock)
Try Synchronization/spinlock.c and
Synchronization/spinlock1.c
23
© 2014-2015 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Kernel thread states
TASK_RUNNING: Thread is in run queue or is running
TASK_INTERRUPTIBLE: Waiting for an event to occur. Wakes on event or
signal and moves to run queue
TASK_UINTERRUPTIBLE: Similar to TASK_INTERRUPTIBLE, receipt of a
signal has not effect
TASK_TRACED: If strace is used to trace the thread
EXIT_ZOMBIE: Terminated, but not cleaned up yet
24
© 2014-2015 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Waiting / Sleeping in a Process
Task moves out of run queue and waits for an event.
Also, known as sleeping
Event can be:
Specified amount of time
Data from file IO
Waiting on semaphore or mutex
Task can be in TASK_INTERRUPTIBLE or
TASK_UNITERRUPTIBLE state
25
© 2014-2015 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Schedule() & set_current_state()
schedule()
Used by processes to voluntarily relinquish the CPU
set_current_state(state)
Sets the state of the process to:
TASK_INTERRUPTIBLE
TASK_UNITERRUPTIBLE
TASK_RUNNING
26
© 2014-2015 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
wake_up_process
Wakes up the process & changes the process
state to TASK_RUNNING
wake_up_process(struct task_struct *);
27
© 2014-2015 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Example Codes
Try WaitQueues/sched.c &
WaitQueues/sched1.c
Modify sched1.c to wait for an event
28
© 2014-2015 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Wait Queues
Set of sleeping processes waiting for an event
#include <linux/wait.h>
Data structure
wait_queue_head_t
Created statically
DECLARE_WAITQUEUE(wait_queue_name)
Created dynamically
wait_queue_head_t my_queue
init_waitqueue_head(&my_queue)
29
© 2014-2015 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Variants of wait
wait_event(queue, condition)
wait_event_interruptible(queue, condition)
wait_event_timeout(queue, condition, timeout)
wait_event_interruptible_timeout(queue,
condition, timeout)
30
© 2014-2015 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Waking up the process
wake_up family of functions
Wakes up all the processes waiting on a queue
wake_up(wake_queue_head_t *)
Wakes up all the processes waiting on the queue
wake_up_interruptible(wait_queue_head_t *)
Wakes up only the processes performing the
interruptible sleep
31
© 2014-2015 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Hands On
WaitQueues/wait_queue.c
Modify an example WaitQueues/sched2.c to
use the wait queues
32
© 2014-2015 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Waiting using wait queue entry
Initialize of wait queue entry
Statically
DEFINE_WAIT(wait_queue)
Dynamically
wait_queue_t wait_queue
init_wait(&wait_queue)
Add an entry to the queue
void prepare_to_wait()
Parameters:
Pointer to wait_queue_head_t
Pointer to wait_queue_t
State
Cleanup:
Void finish_wait()
Pointer to wait_queue_head_t
Pointer to wait_queue_t
33
© 2014-2015 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Hands On
Try WaitQueues/mwait.c
Modify mwait.c to wait for an event
34
© 2014-2015 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Thundering Herd Problem
'wake_up' awakens all the processes waiting on
a wait queue
Only one will succeed in obtaining the desired
resource & rest will have to sleep
For large number of processes on wait queue,
this 'thundering herd' may degrade the system
performance
35
© 2014-2015 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Exclusive wait
Exclusive waiters get added to the end of the
wait queue
wake_up on the wait queue stops after
awakening the first exclusive waiter
void prepare_to_wait_exclusive() sets the
'exclusive' flag in the wait queue
Can't be performed wait_event()
36
© 2014-2015 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Hands On
Try WaitQueues/wait_thunder.c
Modify above example to prevent thundering
herd
37
© 2014-2015 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
wake_up variants
wake_up()
wake_up_interruptible()
wake_up_nr()
wake_up_interruptible_nr()
wake_up_all()
wake_up_interruptible_all()
38
© 2014-2015 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
poll and select
Allows an application to wait on more than one inputs or
outputs
Application blocks, if no data is available
Example, hyperterminal/minicom needs to wait on user input
as well as serial data
Multiple system calls for same functionality
poll – System V solution
select – BSD unix
epoll – for supporting thousand's of file descriptors
39
© 2014-2015 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Driver support for select & poll
#include <linux/poll.h>
Support in driver is provided through poll method
unsigned int (*poll) (struct file *filp, poll_table *wait)
Does the following
Adds the wait_queues to poll_table that can indicate the change in the poll status.
void poll_wait(struct file *, wait_queue_head_t *, poll_table *)
Returns a bit mask for the operations that can be performed without blocking
POLLIN
POLLRDNORM
POLLHUP
POLLOUT
POLLWRNORM
40
© 2014-2015 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Hands On
Select/select.c – Driver
Apps/select_ap – Application
41
© 2014-2015 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
What all have learnt?
W's of Threads & Kthreads
W's of Concurrency Management
Waiting / Sleeping in a Process
'select & poll' support in Kernel
42
© 2014-2015 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Any Queries?

More Related Content

More from SysPlay eLearning Academy for You (13)

SPI Drivers
SPI DriversSPI Drivers
SPI Drivers
 
BeagleBone Black Bootloaders
BeagleBone Black BootloadersBeagleBone Black Bootloaders
BeagleBone Black Bootloaders
 
Introduction to BeagleBone Black
Introduction to BeagleBone BlackIntroduction to BeagleBone Black
Introduction to BeagleBone Black
 
Introduction to BeagleBoard-xM
Introduction to BeagleBoard-xMIntroduction to BeagleBoard-xM
Introduction to BeagleBoard-xM
 
Platform Drivers
Platform DriversPlatform Drivers
Platform Drivers
 
BeagleBone Black Bootloaders
BeagleBone Black BootloadersBeagleBone Black Bootloaders
BeagleBone Black Bootloaders
 
BeagleBone Black Booting Process
BeagleBone Black Booting ProcessBeagleBone Black Booting Process
BeagleBone Black Booting Process
 
BeagleBoard-xM Bootloaders
BeagleBoard-xM BootloadersBeagleBoard-xM Bootloaders
BeagleBoard-xM Bootloaders
 
BeagleBoard-xM Booting Process
BeagleBoard-xM Booting ProcessBeagleBoard-xM Booting Process
BeagleBoard-xM Booting Process
 
Serial Drivers
Serial DriversSerial Drivers
Serial Drivers
 
SPI Drivers
SPI DriversSPI Drivers
SPI Drivers
 
I2C Drivers
I2C DriversI2C Drivers
I2C Drivers
 
Linux System
Linux SystemLinux System
Linux System
 

Recently uploaded

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
 
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
 
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
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxLoriGlavin3
 
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
 
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Mark Goldstein
 
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
 
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesHow to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesThousandEyes
 
So einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdfSo einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdfpanagenda
 
Generative AI - Gitex v1Generative AI - Gitex v1.pptx
Generative AI - Gitex v1Generative AI - Gitex v1.pptxGenerative AI - Gitex v1Generative AI - Gitex v1.pptx
Generative AI - Gitex v1Generative AI - Gitex v1.pptxfnnc6jmgwh
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfLoriGlavin3
 
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
 
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
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxLoriGlavin3
 
Data governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationData governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationKnoldus Inc.
 
Varsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
Varsha Sewlal- Cyber Attacks on Critical Critical InfrastructureVarsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
Varsha Sewlal- Cyber Attacks on Critical Critical Infrastructureitnewsafrica
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersNicole Novielli
 
React Native vs Ionic - The Best Mobile App Framework
React Native vs Ionic - The Best Mobile App FrameworkReact Native vs Ionic - The Best Mobile App Framework
React Native vs Ionic - The Best Mobile App FrameworkPixlogix Infotech
 
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
 
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Alkin Tezuysal
 

Recently uploaded (20)

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#
 
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
 
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
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
 
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
 
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
 
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
 
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesHow to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
 
So einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdfSo einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdf
 
Generative AI - Gitex v1Generative AI - Gitex v1.pptx
Generative AI - Gitex v1Generative AI - Gitex v1.pptxGenerative AI - Gitex v1Generative AI - Gitex v1.pptx
Generative AI - Gitex v1Generative AI - Gitex v1.pptx
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdf
 
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
 
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
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
 
Data governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationData governance with Unity Catalog Presentation
Data governance with Unity Catalog Presentation
 
Varsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
Varsha Sewlal- Cyber Attacks on Critical Critical InfrastructureVarsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
Varsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software Developers
 
React Native vs Ionic - The Best Mobile App Framework
React Native vs Ionic - The Best Mobile App FrameworkReact Native vs Ionic - The Best Mobile App Framework
React Native vs Ionic - The Best Mobile App Framework
 
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
 
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
 

Kernel Process Management

  • 1. © 2014-2015 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Linux Kernel Process Management
  • 2. 2 © 2014-2015 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. What to Expect? W's of Threads & Kthreads W's of Concurrency Management Waiting / Sleeping in a process 'select & poll' support in kernel
  • 3. 3 © 2014-2015 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. W's of Threads Basic unit of CPU utilization Also, known as Light weight process Implemented as POSIX threads in user space, commonly known as pthread Multiple threads share the same address space in a process Communication is usually through global variables
  • 4. 4 © 2014-2015 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. W's of Kernel Threads Exist in Kernel Space Used to perform some tasks in background Are descended from kthreadd Can be viewed in user space with command 'ps -fx' or, 'ps -mx' Examples: khubd ksoftirqd kworker
  • 5. 5 © 2014-2015 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Creating a Kernel Thread #include <kthread.h> kthread_create(int (*function)(void *data), void *data, const char name[], ...) Parameters: function – The function that the thread has to execute data – The 'data' to be passed to the function name – The name by which the process will be recognized in the kernel Returns: Pointer to a structure of type task_struct Try Threads/thread.c
  • 6. 6 © 2014-2015 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Waking Up & Stopping a Thread wake_up_process(struct task_struct *) Function passed to kthread_create() gets executed Parameters: Pointer to struct of type task_struct kthread_stop(struct task_struct *) Waits for the thread to stop Parameters Pointer to structure of type task_struct Returns: Result of thread function Try Threads/thread1.c and Threads/thread_stop.c
  • 7. 7 © 2014-2015 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Running a Thread kthread_run(int (*function)(void *data), void *data, const char name[], ...) Creates & starts the threads Parameters: function – The function that the thread has to execute data – The 'data' to be passed to the function name – The name by which the process will be recognized in the kernel Try Threads/thread_run.c
  • 8. © 2014-2015 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Concurrency Management
  • 9. 9 © 2014-2015 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Concurrency Issues Multitasking leads to the uncontrolled access to the shared resources Easiest to create & hard to debug May lead to memory leak Global variable should best be avoided If can't avoid global variables, then need to resort to the synchronization mechanisms
  • 10. 10 © 2014-2015 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Mutex Stands for MUTual EXclusion Only one thread of execution can hold it at a time. Has operations such as lock & unlock. 'lock' call blocks if mutex is not available & the process goes to sleep
  • 11. 11 © 2014-2015 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Data Structures for Mutex #include <linux/mutex.h> struct mutex Initialization Statically DEFINE_MUTEX(mutex) Dynamically mutex_init Operations mutex_lock() mutex_unlock() mutex_lock_interruptible() mutex_trylock()
  • 12. 12 © 2014-2015 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Hands On Try Synchronization/mutex.c and Synchronization/mutex1.c
  • 13. 13 © 2014-2015 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Semaphore Semaphore is a counter, which keeps track of resources Has two operations – wait/down & post/up Wait decrements the count by one Post increments the count by one Process blocks if the value of count is zero.
  • 14. 14 © 2014-2015 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Data Structures for Semaphore #include <linux/semaphore.h> struct semaphore Initialization Statically DEFINE_SEMAPHORE(name) Dynamically sema_init(&sem, val) Operations void down((&sem) int down_interruptible(&sem) Int down_trylock() void up(&sem)
  • 15. 15 © 2014-2015 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Hands On Try Synchronization/sem.c Try Synchronization/cons_prod.c Modify cons_prod.c to avoid exit of consumers threads
  • 16. 16 © 2014-2015 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Reader/Writer Semaphores #include <linux/rwsem.h> structure rw_semaphore Allows one writer or unlimited number of readers to hold the semaphore Intialization: void init_rwsem(&rwsem) Operations for Readers void down_read(&sem) int down_read_trylock(&sem) void up_read(&sem)
  • 17. 17 © 2014-2015 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Reader/Writer Semaphores... Operations for Writers void down_write(struct rw_semaphore *sem) int down_write_trylock(struct rw_semaphore *sem) void up_write(struct rw_semaphore *sem) Try Synchronization/rwsem.c
  • 18. 18 © 2014-2015 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Spinlocks Provides synchronization mechanism for the code which can't sleep. Example – Interrupt handlers Two states – locked & unlocked If lock is available, code enters the critical section If lock is unavailable, code goes into the tight loop. So, the name spinlock
  • 19. 19 © 2014-2015 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Spinlock Data Structures #include <linux/spinlock.h> Type: spinlock_t spin_lock_init(&my_slock) spin_lock(&my_slock) spin_unlock(&my_slock) Try spinlock_example.c
  • 20. 20 © 2014-2015 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Cautions while holding a spin lock Avoid the code which can sleep Preemption is disabled by spin lock code Critical section should be fast & short Sometimes, disabling of interrupts on the local processor is required to avoid deadlock
  • 21. 21 © 2014-2015 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Other spinlock functions spin_lock_irqsave(&my_lock, flags) Disables interrupts on the local processor Interrupt state is saved in flags spin_lock_irq(&my_slock) Disables interrupts, but doesn't keeps track of flags spin_lock_bh Disables software interrupts only
  • 22. 22 © 2014-2015 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. spin_unlock variants void spin_unlock_irqrestore(&my_slock, flags) Unlocks & restores the irqs void spin_unlock_irq(&my_slock) void spin_unlock_bh(&my_slock) Try Synchronization/spinlock.c and Synchronization/spinlock1.c
  • 23. 23 © 2014-2015 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Kernel thread states TASK_RUNNING: Thread is in run queue or is running TASK_INTERRUPTIBLE: Waiting for an event to occur. Wakes on event or signal and moves to run queue TASK_UINTERRUPTIBLE: Similar to TASK_INTERRUPTIBLE, receipt of a signal has not effect TASK_TRACED: If strace is used to trace the thread EXIT_ZOMBIE: Terminated, but not cleaned up yet
  • 24. 24 © 2014-2015 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Waiting / Sleeping in a Process Task moves out of run queue and waits for an event. Also, known as sleeping Event can be: Specified amount of time Data from file IO Waiting on semaphore or mutex Task can be in TASK_INTERRUPTIBLE or TASK_UNITERRUPTIBLE state
  • 25. 25 © 2014-2015 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Schedule() & set_current_state() schedule() Used by processes to voluntarily relinquish the CPU set_current_state(state) Sets the state of the process to: TASK_INTERRUPTIBLE TASK_UNITERRUPTIBLE TASK_RUNNING
  • 26. 26 © 2014-2015 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. wake_up_process Wakes up the process & changes the process state to TASK_RUNNING wake_up_process(struct task_struct *);
  • 27. 27 © 2014-2015 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Example Codes Try WaitQueues/sched.c & WaitQueues/sched1.c Modify sched1.c to wait for an event
  • 28. 28 © 2014-2015 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Wait Queues Set of sleeping processes waiting for an event #include <linux/wait.h> Data structure wait_queue_head_t Created statically DECLARE_WAITQUEUE(wait_queue_name) Created dynamically wait_queue_head_t my_queue init_waitqueue_head(&my_queue)
  • 29. 29 © 2014-2015 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Variants of wait wait_event(queue, condition) wait_event_interruptible(queue, condition) wait_event_timeout(queue, condition, timeout) wait_event_interruptible_timeout(queue, condition, timeout)
  • 30. 30 © 2014-2015 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Waking up the process wake_up family of functions Wakes up all the processes waiting on a queue wake_up(wake_queue_head_t *) Wakes up all the processes waiting on the queue wake_up_interruptible(wait_queue_head_t *) Wakes up only the processes performing the interruptible sleep
  • 31. 31 © 2014-2015 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Hands On WaitQueues/wait_queue.c Modify an example WaitQueues/sched2.c to use the wait queues
  • 32. 32 © 2014-2015 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Waiting using wait queue entry Initialize of wait queue entry Statically DEFINE_WAIT(wait_queue) Dynamically wait_queue_t wait_queue init_wait(&wait_queue) Add an entry to the queue void prepare_to_wait() Parameters: Pointer to wait_queue_head_t Pointer to wait_queue_t State Cleanup: Void finish_wait() Pointer to wait_queue_head_t Pointer to wait_queue_t
  • 33. 33 © 2014-2015 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Hands On Try WaitQueues/mwait.c Modify mwait.c to wait for an event
  • 34. 34 © 2014-2015 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Thundering Herd Problem 'wake_up' awakens all the processes waiting on a wait queue Only one will succeed in obtaining the desired resource & rest will have to sleep For large number of processes on wait queue, this 'thundering herd' may degrade the system performance
  • 35. 35 © 2014-2015 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Exclusive wait Exclusive waiters get added to the end of the wait queue wake_up on the wait queue stops after awakening the first exclusive waiter void prepare_to_wait_exclusive() sets the 'exclusive' flag in the wait queue Can't be performed wait_event()
  • 36. 36 © 2014-2015 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Hands On Try WaitQueues/wait_thunder.c Modify above example to prevent thundering herd
  • 37. 37 © 2014-2015 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. wake_up variants wake_up() wake_up_interruptible() wake_up_nr() wake_up_interruptible_nr() wake_up_all() wake_up_interruptible_all()
  • 38. 38 © 2014-2015 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. poll and select Allows an application to wait on more than one inputs or outputs Application blocks, if no data is available Example, hyperterminal/minicom needs to wait on user input as well as serial data Multiple system calls for same functionality poll – System V solution select – BSD unix epoll – for supporting thousand's of file descriptors
  • 39. 39 © 2014-2015 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Driver support for select & poll #include <linux/poll.h> Support in driver is provided through poll method unsigned int (*poll) (struct file *filp, poll_table *wait) Does the following Adds the wait_queues to poll_table that can indicate the change in the poll status. void poll_wait(struct file *, wait_queue_head_t *, poll_table *) Returns a bit mask for the operations that can be performed without blocking POLLIN POLLRDNORM POLLHUP POLLOUT POLLWRNORM
  • 40. 40 © 2014-2015 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Hands On Select/select.c – Driver Apps/select_ap – Application
  • 41. 41 © 2014-2015 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. What all have learnt? W's of Threads & Kthreads W's of Concurrency Management Waiting / Sleeping in a Process 'select & poll' support in Kernel
  • 42. 42 © 2014-2015 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Any Queries?