Advertisement

More Related Content

Advertisement

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?
Advertisement