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

08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
Ā 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
Ā 
šŸ¬ The future of MySQL is Postgres šŸ˜
šŸ¬  The future of MySQL is Postgres   šŸ˜šŸ¬  The future of MySQL is Postgres   šŸ˜
šŸ¬ The future of MySQL is Postgres šŸ˜RTylerCroy
Ā 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
Ā 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
Ā 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
Ā 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
Ā 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
Ā 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
Ā 
Scaling API-first ā€“ The story of a global engineering organization
Scaling API-first ā€“ The story of a global engineering organizationScaling API-first ā€“ The story of a global engineering organization
Scaling API-first ā€“ The story of a global engineering organizationRadu Cotescu
Ā 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
Ā 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilV3cube
Ā 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
Ā 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
Ā 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service šŸø 8923113531 šŸŽ° Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service šŸø 8923113531 šŸŽ° Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service šŸø 8923113531 šŸŽ° Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service šŸø 8923113531 šŸŽ° Avail...gurkirankumar98700
Ā 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
Ā 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel AraĆŗjo
Ā 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
Ā 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
Ā 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
Ā 

Recently uploaded (20)

08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
Ā 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
Ā 
šŸ¬ The future of MySQL is Postgres šŸ˜
šŸ¬  The future of MySQL is Postgres   šŸ˜šŸ¬  The future of MySQL is Postgres   šŸ˜
šŸ¬ The future of MySQL is Postgres šŸ˜
Ā 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
Ā 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
Ā 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
Ā 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
Ā 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
Ā 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
Ā 
Scaling API-first ā€“ The story of a global engineering organization
Scaling API-first ā€“ The story of a global engineering organizationScaling API-first ā€“ The story of a global engineering organization
Scaling API-first ā€“ The story of a global engineering organization
Ā 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
Ā 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of Brazil
Ā 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
Ā 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Ā 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service šŸø 8923113531 šŸŽ° Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service šŸø 8923113531 šŸŽ° Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service šŸø 8923113531 šŸŽ° Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service šŸø 8923113531 šŸŽ° Avail...
Ā 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
Ā 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Ā 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Ā 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
Ā 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
Ā 

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?