Home
Explore
Submit Search
Upload
Login
Signup
Advertisement
Check these out next
SPI Drivers
SysPlay eLearning Academy for You
BeagleBone Black Bootloaders
SysPlay eLearning Academy for You
Introduction to BeagleBone Black
SysPlay eLearning Academy for You
Introduction to BeagleBoard-xM
SysPlay eLearning Academy for You
Platform Drivers
SysPlay eLearning Academy for You
BeagleBone Black Bootloaders
SysPlay eLearning Academy for You
BeagleBone Black Booting Process
SysPlay eLearning Academy for You
BeagleBoard-xM Bootloaders
SysPlay eLearning Academy for You
1
of
42
Top clipped slide
Kernel Process Management
Mar. 20, 2021
•
0 likes
0 likes
×
Be the first to like this
Show More
•
2,201 views
views
×
Total views
0
On Slideshare
0
From embeds
0
Number of embeds
0
Report
Technology
Process Management in Linux Kernel
SysPlay eLearning Academy for You
Follow
SysPlay eLearning Academy for You
Advertisement
Advertisement
Advertisement
Recommended
Linux Internals Part - 3
SysPlay eLearning Academy for You
116 views
•
52 slides
Linux Internals Part - 2
SysPlay eLearning Academy for You
57 views
•
57 slides
Linux Internals Part - 1
SysPlay eLearning Academy for You
84 views
•
70 slides
Kernel Timing Management
SysPlay eLearning Academy for You
3K views
•
29 slides
Understanding the BBB
SysPlay eLearning Academy for You
9.5K views
•
10 slides
POSIX Threads
SysPlay eLearning Academy for You
4.1K views
•
21 slides
More Related Content
More from SysPlay eLearning Academy for You
(13)
SPI Drivers
SysPlay eLearning Academy for You
•
9.8K views
BeagleBone Black Bootloaders
SysPlay eLearning Academy for You
•
3.9K views
Introduction to BeagleBone Black
SysPlay eLearning Academy for You
•
8.1K views
Introduction to BeagleBoard-xM
SysPlay eLearning Academy for You
•
3.2K views
Platform Drivers
SysPlay eLearning Academy for You
•
17.2K views
BeagleBone Black Bootloaders
SysPlay eLearning Academy for You
•
4.4K views
BeagleBone Black Booting Process
SysPlay eLearning Academy for You
•
18K views
BeagleBoard-xM Bootloaders
SysPlay eLearning Academy for You
•
7.2K views
BeagleBoard-xM Booting Process
SysPlay eLearning Academy for You
•
3.7K views
Serial Drivers
SysPlay eLearning Academy for You
•
16.7K views
SPI Drivers
SysPlay eLearning Academy for You
•
13.1K views
I2C Drivers
SysPlay eLearning Academy for You
•
28.4K views
Linux System
SysPlay eLearning Academy for You
•
851 views
Recently uploaded
(20)
如何办理一份高仿南达科他大学毕业证成绩单?
aazepp
•
0 views
Pill Camera.pptx
Md Refatul Amin Refat
•
0 views
NS-CUK Seminar: S.T.Nguyen, Review on "Improving Graph Neural Network Express...
ssuser4b1f48
•
0 views
proposal (2).pdf
mennaHendy
•
0 views
Nanotechnology.pdf
shikharbhadouria
•
0 views
Fortnite Is Awsome!!!
YT SavageGuy
•
0 views
Digital Marketing Plan.pdf
mennaHendy
•
0 views
如何办理一份高仿昆特兰理工大学毕业证成绩单?
aazepp
•
0 views
RC522 RFID Reader_Write For Arduino.pdf
RoboDJ
•
0 views
Do Reinvent the Wheel - Nov 2021 - DigiNext.pdf
Hamidreza Soleimani
•
0 views
Will AI take over our jobs .pptx
Alfredo Mancera
•
0 views
如何办理一份高仿纽约州立大学宾汉姆顿分校毕业证成绩单?
aazepp
•
0 views
【本科生、研究生】美国德鲁大学毕业证文凭购买指南
sutseu
•
0 views
Networking Hardware Requirements.pptx
JhamaikaPaet
•
0 views
如何办理一份高仿东伦敦大学毕业证成绩单?
aazepp
•
0 views
Spring_Boot_Microservices-5_Day_Session.pptx
Prabhakaran Ravichandran
•
0 views
Azure Pizza as a Service Model
Carlo Sacchi
•
0 views
What are the Reactjs Properties
TutorialsFreak
•
0 views
如何办理一份高仿伦敦南岸大学毕业证成绩单?
aazepp
•
0 views
CyberEthics.ppt
ANKITKUMAR920995
•
0 views
Advertisement
Kernel Process Management
© 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?
Advertisement