SlideShare une entreprise Scribd logo
1  sur  31
@ 2021-21 Embitude Trainings <info@embitude.in>
All Rights Reserved
Kernel Process Management
@ 2021-21 Embitude Trainings <info@embitude.in>
All Rights Reserved
What to Expect
●
Need for synchronization
●
Atomic Variables
●
Mutex
●
Semaphore
●
Spinlocks
●
Making process wait
●
Basic wait mechanism
●
Wait queues
●
Poll & Select
@ 2021-21 Embitude Trainings <info@embitude.in>
All Rights Reserved
Need for Synchronization
●
Multiple tasks run in the system at the same
●
Might be sharing the resources
●
May result in memory leak or data corruption
●
Avoiding the global variables could resolve the
issue
●
If can’t be avoided, need to use the
synchronization mechanism
@ 2021-21 Embitude Trainings <info@embitude.in>
All Rights Reserved
●
Operations that gurantee indivisibility &
uninterruptibility
●
CPU instructions that provide the atomic read-
modify-write operations on a memory location
●
Handy for manipulation of shared data
atomically in the multithreaded scenario
●
Linux kernel uses this for reference counting in
shared data structures, wait-notify flags
Atomic Operations
@ 2021-21 Embitude Trainings <info@embitude.in>
All Rights Reserved
Atomic Integer Operations
●
Generic atomic operation interfaces include support for integer
& bitwise operations
●
Integer operations operate on atomic_t & atomic64_t
●
Operations
●
ATOMIC_INIT(i)
●
atomic_read(v)
●
atomic_set(v, i)
●
atomic_add(init i, atomic_t *v)
●
atomic_sub(int i, atomic_t *v)
●
atomic_inc(atomic_t *v)
●
atomic_dec(atomic_t *v)
@ 2021-21 Embitude Trainings <info@embitude.in>
All Rights Reserved
Read Modiy Write & Bitwise operations
●
Read Modify Write
– bool atomic_sub_and_test(int i, atomic_t *v)
– bool atomic_dec_and_test(atomic_t *v)
– bool atomic_dec_and_test(atomic_t *v)
●
Atomic bitwise operations
– set_bit(int nr, volatile unsigned long addr *)
– clear_bit(int nr, volatile unsigned logn addr *)
– int test_and_set_bit(int nr, volatile unsigned long *addr)
@ 2021-21 Embitude Trainings <info@embitude.in>
All Rights Reserved
●
#include <linux/mutex.h>
●
struct mutex
●
Initialization
– Statically
●
DEFINE_MUTEX(mutex)
– Dynamically
●
mutex_init
●
Operations
– mutex_lock()
– mutex_unlock()
– mutex_lock_interruptible()
– mutex_trylock()
Mutex
@ 2021-21 Embitude Trainings <info@embitude.in>
All Rights Reserved
Semaphore
●
Counter
– Maintains the count of the resources
●
Two operations
– up/post
– down/wait
●
Process calling the down/wait blocks if the value is 0
@ 2021-21 Embitude Trainings <info@embitude.in>
All Rights Reserved
Semaphore APIs
●
#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)
@ 2021-21 Embitude Trainings <info@embitude.in>
All Rights Reserved
Reader/Writer Semaphore
●
#include <linux/rwsem.h>
●
struct rw_semaphore
●
Allows one writer or unlimited number of readers to hold the semaphore
●
Initialization
– void int_rwsem(struct rw_semaphore *rwsem)
●
Operations for readers
– void down_read(struct rw_semaphore *rwsem)
– int down_read_trylock(struct rw_semaphore *rwsem)
– void up_read(struct rw_semaphore *rwsem)
●
Operations for writers
– void down_write(struct rw_semaphore *rwsem)
– int down_write_trylock(struct rw_semaphore *rwsem)
– void up_write(struct rw_semaphore *rwsem)
@ 2021-21 Embitude Trainings <info@embitude.in>
All Rights Reserved
Spinlocks
●
Two operations
– Lock
– Unlock
●
Difference from the mutex
– Doesn’t puts the process to sleep, if lock is
not available
– Instead spins
– That’s why the name
@ 2021-21 Embitude Trainings <info@embitude.in>
All Rights Reserved
Spinlock APIs
●
#include <linux/spinlock.h>
●
Type: spinlock_t
●
spin_lock_init(&my_slock)
●
spin_lock(&my_slock)
●
spin_unlock(&my_slock)
@ 2021-21 Embitude Trainings <info@embitude.in>
All Rights Reserved
Spinlock APIs...
●
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
@ 2021-21 Embitude Trainings <info@embitude.in>
All Rights Reserved
Spin unlock APIs
●
void spin_unlock_irqrestore(&my_slock, flags)
●
Unlocks & restores the irqs
●
void spin_unlock_irq(&my_slock)
●
void spin_unlock_bh(&my_slock)
@ 2021-21 Embitude Trainings <info@embitude.in>
All Rights Reserved
Kernel 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
@ 2021-21 Embitude Trainings <info@embitude.in>
All Rights Reserved
Kernel Threads...
●
Exists in Kernel space
●
Used to perform some background tasks
●
Are descended from kthreadd
●
Can be viewed in user space with command
– ps -fx or ps -mx
●
Examples
– khubd
– ksoftirqd
– kworker
@ 2021-21 Embitude Trainings <info@embitude.in>
All Rights Reserved
Creating a Kernel thread
●
#include <kthread.h>
●
kthread_create(int (*fn)(void *data), const char name[],
...)
●
Parameters
– fn – The function that the thread has to execute
– data – The ‘data’ to the passed to the function
– name – The name by which the thread will be
recognized in the kernel
●
Returns
– Pointer to the structure of type task_struct
@ 2021-21 Embitude Trainings <info@embitude.in>
All Rights Reserved
Waking Up & Stopping a Thread
●
wake_up_process(struct task_struct *)
– Functiion passed to the 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 the thread function
@ 2021-21 Embitude Trainings <info@embitude.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
functions
●
name – The name by which the thread
would be recognized in the kernel
@ 2021-21 Embitude Trainings <info@embitude.in>
All Rights Reserved
Making Process Wait
●
While dealing with hardware, process needs to
wait for an event:
– Specified amount of time
– Data from file I/O
– Waiting on semaphore or mutex
●
Task moves out of run queue and waits for an
event
●
Task can be in TASK_INTERRUPTIBLE or
TASK_UNINTERRUPTIBLE state
@ 2021-21 Embitude Trainings <info@embitude.in>
All Rights Reserved
Basic wait mechanism
●
schedule()
– Used by the processes to voluntarily
relinquish the CPU
– Trigger the scheduler
●
Set_current_state(state)
– Sets the state of the process to:
●
TASK_INTERRUPTIBLE
●
TASK_UNINTERRUPTIBLE
●
TASK_RUNNING
@ 2021-21 Embitude Trainings <info@embitude.in>
All Rights Reserved
Waking up
●
wake_up_process(struct task_struct *ts)
– Wakes up the process & changes the
process state to TASK_RUNNING
@ 2021-21 Embitude Trainings <info@embitude.in>
All Rights Reserved
Wait Queues
●
Queue of sleeping processes waiting for an event
●
#include <linux/wait.h>
●
Data structure
– wait_queue_head_t
●
Created statically
– DECLARE_WAIT_QUEUE_HEAD(wait_queue_na
me)
●
Created dynamically
– wait_queue_head_t my_queue
– init_waitqueue_head(&my_queue)
@ 2021-21 Embitude Trainings <info@embitude.in>
All Rights Reserved
Variants for wait
●
Wait_event(queue, condition)
●
Wait_event_interruptible(queue, condition)
●
Wait_event_timeout(queue, condition, timeout)
●
Wait_event_interruptible_timeout(queue,
condition, timeout)
@ 2021-21 Embitude Trainings <info@embitude.in>
All Rights Reserved
Waking up the Process
●
Wake up family of functions
●
wake_up(wait_queue_head_t *)
– Wakes up all the processes waiting on the
queue
●
wakes_up_interruptible(wait_queue_head_t *)
– Wakes up only the processes performing the
interruptible sleep
@ 2021-21 Embitude Trainings <info@embitude.in>
All Rights Reserved
Waiting using wait queue entry
●
Intialize 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
●
Clean up
– Void finish_wait()
●
Pointer to wait_queue_head_t
●
Pointer to wait_queue_t
@ 2021-21 Embitude Trainings <info@embitude.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 to sleep
●
For large number of processes on wait queue,
this ‘thundering herd’ may degrade the system
performance
@ 2021-21 Embitude Trainings <info@embitude.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 wait queue
●
Can’t be performed using wait_event()
@ 2021-21 Embitude Trainings <info@embitude.in>
All Rights Reserved
Poll & 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 inputs 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
@ 2021-21 Embitude Trainings <info@embitude.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)
●
Adds the wait_queues to the 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, POLLWRNORM
@ 2021-21 Embitude Trainings <info@embitude.in>
All Rights Reserved
What all did we learn?
●
Need for synchronization
●
Atomic Variables
●
Mutex
●
Semaphore
●
Spinlocks
●
Making process wait
●
Basic wait mechanism
●
Wait queues
●
Poll & Select

Contenu connexe

Tendances

Linux Kernel and Driver Development Training
Linux Kernel and Driver Development TrainingLinux Kernel and Driver Development Training
Linux Kernel and Driver Development TrainingStephan Cadene
 
Linux Kernel Booting Process (1) - For NLKB
Linux Kernel Booting Process (1) - For NLKBLinux Kernel Booting Process (1) - For NLKB
Linux Kernel Booting Process (1) - For NLKBshimosawa
 
HKG15-505: Power Management interactions with OP-TEE and Trusted Firmware
HKG15-505: Power Management interactions with OP-TEE and Trusted FirmwareHKG15-505: Power Management interactions with OP-TEE and Trusted Firmware
HKG15-505: Power Management interactions with OP-TEE and Trusted FirmwareLinaro
 
Vmlinux: anatomy of bzimage and how x86 64 processor is booted
Vmlinux: anatomy of bzimage and how x86 64 processor is bootedVmlinux: anatomy of bzimage and how x86 64 processor is booted
Vmlinux: anatomy of bzimage and how x86 64 processor is bootedAdrian Huang
 
Linux Ethernet device driver
Linux Ethernet device driverLinux Ethernet device driver
Linux Ethernet device driver艾鍗科技
 
Yocto Project introduction
Yocto Project introductionYocto Project introduction
Yocto Project introductionYi-Hsiu Hsu
 
qemu + gdb: The efficient way to understand/debug Linux kernel code/data stru...
qemu + gdb: The efficient way to understand/debug Linux kernel code/data stru...qemu + gdb: The efficient way to understand/debug Linux kernel code/data stru...
qemu + gdb: The efficient way to understand/debug Linux kernel code/data stru...Adrian Huang
 
Understanding the Android System Server
Understanding the Android System ServerUnderstanding the Android System Server
Understanding the Android System ServerOpersys inc.
 
Linux Kernel MMC Storage driver Overview
Linux Kernel MMC Storage driver OverviewLinux Kernel MMC Storage driver Overview
Linux Kernel MMC Storage driver OverviewRajKumar Rampelli
 

Tendances (20)

File System Modules
File System ModulesFile System Modules
File System Modules
 
Linux Kernel and Driver Development Training
Linux Kernel and Driver Development TrainingLinux Kernel and Driver Development Training
Linux Kernel and Driver Development Training
 
Embedded linux network device driver development
Embedded linux network device driver developmentEmbedded linux network device driver development
Embedded linux network device driver development
 
Linux Kernel Booting Process (1) - For NLKB
Linux Kernel Booting Process (1) - For NLKBLinux Kernel Booting Process (1) - For NLKB
Linux Kernel Booting Process (1) - For NLKB
 
BeagleBone Black Bootloaders
BeagleBone Black BootloadersBeagleBone Black Bootloaders
BeagleBone Black Bootloaders
 
systemd
systemdsystemd
systemd
 
HKG15-505: Power Management interactions with OP-TEE and Trusted Firmware
HKG15-505: Power Management interactions with OP-TEE and Trusted FirmwareHKG15-505: Power Management interactions with OP-TEE and Trusted Firmware
HKG15-505: Power Management interactions with OP-TEE and Trusted Firmware
 
Linux device drivers
Linux device drivers Linux device drivers
Linux device drivers
 
Vmlinux: anatomy of bzimage and how x86 64 processor is booted
Vmlinux: anatomy of bzimage and how x86 64 processor is bootedVmlinux: anatomy of bzimage and how x86 64 processor is booted
Vmlinux: anatomy of bzimage and how x86 64 processor is booted
 
SPI Drivers
SPI DriversSPI Drivers
SPI Drivers
 
Linux Ethernet device driver
Linux Ethernet device driverLinux Ethernet device driver
Linux Ethernet device driver
 
Yocto Project introduction
Yocto Project introductionYocto Project introduction
Yocto Project introduction
 
qemu + gdb: The efficient way to understand/debug Linux kernel code/data stru...
qemu + gdb: The efficient way to understand/debug Linux kernel code/data stru...qemu + gdb: The efficient way to understand/debug Linux kernel code/data stru...
qemu + gdb: The efficient way to understand/debug Linux kernel code/data stru...
 
BusyBox for Embedded Linux
BusyBox for Embedded LinuxBusyBox for Embedded Linux
BusyBox for Embedded Linux
 
Understanding the Android System Server
Understanding the Android System ServerUnderstanding the Android System Server
Understanding the Android System Server
 
Linux Internals - Part II
Linux Internals - Part IILinux Internals - Part II
Linux Internals - Part II
 
A practical guide to buildroot
A practical guide to buildrootA practical guide to buildroot
A practical guide to buildroot
 
Spi drivers
Spi driversSpi drivers
Spi drivers
 
Memory model
Memory modelMemory model
Memory model
 
Linux Kernel MMC Storage driver Overview
Linux Kernel MMC Storage driver OverviewLinux Kernel MMC Storage driver Overview
Linux Kernel MMC Storage driver Overview
 

Similaire à Kernel Process Management

C++ CoreHard Autumn 2018. Concurrency and Parallelism in C++17 and C++20/23 -...
C++ CoreHard Autumn 2018. Concurrency and Parallelism in C++17 and C++20/23 -...C++ CoreHard Autumn 2018. Concurrency and Parallelism in C++17 and C++20/23 -...
C++ CoreHard Autumn 2018. Concurrency and Parallelism in C++17 and C++20/23 -...corehard_by
 
Embedded_ PPT_4-5 unit_Dr Monika-edited.pptx
Embedded_ PPT_4-5 unit_Dr Monika-edited.pptxEmbedded_ PPT_4-5 unit_Dr Monika-edited.pptx
Embedded_ PPT_4-5 unit_Dr Monika-edited.pptxProfMonikaJain
 
Kernel Recipes 2019 - ftrace: Where modifying a running kernel all started
Kernel Recipes 2019 - ftrace: Where modifying a running kernel all startedKernel Recipes 2019 - ftrace: Where modifying a running kernel all started
Kernel Recipes 2019 - ftrace: Where modifying a running kernel all startedAnne Nicolas
 
Tracing and profiling my sql (percona live europe 2019) draft_1
Tracing and profiling my sql (percona live europe 2019) draft_1Tracing and profiling my sql (percona live europe 2019) draft_1
Tracing and profiling my sql (percona live europe 2019) draft_1Valerii Kravchuk
 
Input and Output Devices and Systems
Input and Output Devices and SystemsInput and Output Devices and Systems
Input and Output Devices and SystemsNajma Alam
 
Userspace adaptive spinlocks with rseq
Userspace adaptive spinlocks with rseqUserspace adaptive spinlocks with rseq
Userspace adaptive spinlocks with rseqIgalia
 
Linux /proc filesystem for MySQL DBAs - FOSDEM 2021
Linux  /proc filesystem for MySQL DBAs - FOSDEM 2021Linux  /proc filesystem for MySQL DBAs - FOSDEM 2021
Linux /proc filesystem for MySQL DBAs - FOSDEM 2021Valeriy Kravchuk
 
Java util concurrent
Java util concurrentJava util concurrent
Java util concurrentRoger Xia
 
Process scheduling
Process schedulingProcess scheduling
Process schedulingHao-Ran Liu
 
20150918 klug el performance tuning-v1.4
20150918 klug el performance tuning-v1.420150918 klug el performance tuning-v1.4
20150918 klug el performance tuning-v1.4Jinkoo Han
 
Kernel Timing Management
Kernel Timing ManagementKernel Timing Management
Kernel Timing Managementpradeep_tewani
 
RTOS implementation
RTOS implementationRTOS implementation
RTOS implementationRajan Kumar
 
Effective service and resource management with systemd
Effective service and resource management with systemdEffective service and resource management with systemd
Effective service and resource management with systemdDavid Timothy Strauss
 
Linux Du Jour
Linux Du JourLinux Du Jour
Linux Du Jourmwedgwood
 
When the OS gets in the way
When the OS gets in the wayWhen the OS gets in the way
When the OS gets in the wayMark Price
 
Virtual Machine Introspection with Xen
Virtual Machine Introspection with XenVirtual Machine Introspection with Xen
Virtual Machine Introspection with XenTamas K Lengyel
 
Troubleshooting MySQL from a MySQL Developer Perspective
Troubleshooting MySQL from a MySQL Developer PerspectiveTroubleshooting MySQL from a MySQL Developer Perspective
Troubleshooting MySQL from a MySQL Developer PerspectiveMarcelo Altmann
 

Similaire à Kernel Process Management (20)

Operating System lab
Operating System labOperating System lab
Operating System lab
 
C++ CoreHard Autumn 2018. Concurrency and Parallelism in C++17 and C++20/23 -...
C++ CoreHard Autumn 2018. Concurrency and Parallelism in C++17 and C++20/23 -...C++ CoreHard Autumn 2018. Concurrency and Parallelism in C++17 and C++20/23 -...
C++ CoreHard Autumn 2018. Concurrency and Parallelism in C++17 and C++20/23 -...
 
Embedded_ PPT_4-5 unit_Dr Monika-edited.pptx
Embedded_ PPT_4-5 unit_Dr Monika-edited.pptxEmbedded_ PPT_4-5 unit_Dr Monika-edited.pptx
Embedded_ PPT_4-5 unit_Dr Monika-edited.pptx
 
Kernel Recipes 2019 - ftrace: Where modifying a running kernel all started
Kernel Recipes 2019 - ftrace: Where modifying a running kernel all startedKernel Recipes 2019 - ftrace: Where modifying a running kernel all started
Kernel Recipes 2019 - ftrace: Where modifying a running kernel all started
 
Tracing and profiling my sql (percona live europe 2019) draft_1
Tracing and profiling my sql (percona live europe 2019) draft_1Tracing and profiling my sql (percona live europe 2019) draft_1
Tracing and profiling my sql (percona live europe 2019) draft_1
 
Input and Output Devices and Systems
Input and Output Devices and SystemsInput and Output Devices and Systems
Input and Output Devices and Systems
 
Userspace adaptive spinlocks with rseq
Userspace adaptive spinlocks with rseqUserspace adaptive spinlocks with rseq
Userspace adaptive spinlocks with rseq
 
Linux /proc filesystem for MySQL DBAs - FOSDEM 2021
Linux  /proc filesystem for MySQL DBAs - FOSDEM 2021Linux  /proc filesystem for MySQL DBAs - FOSDEM 2021
Linux /proc filesystem for MySQL DBAs - FOSDEM 2021
 
Java util concurrent
Java util concurrentJava util concurrent
Java util concurrent
 
Process scheduling
Process schedulingProcess scheduling
Process scheduling
 
20150918 klug el performance tuning-v1.4
20150918 klug el performance tuning-v1.420150918 klug el performance tuning-v1.4
20150918 klug el performance tuning-v1.4
 
Threads in Java
Threads in JavaThreads in Java
Threads in Java
 
Kernel Timing Management
Kernel Timing ManagementKernel Timing Management
Kernel Timing Management
 
RTOS implementation
RTOS implementationRTOS implementation
RTOS implementation
 
Effective service and resource management with systemd
Effective service and resource management with systemdEffective service and resource management with systemd
Effective service and resource management with systemd
 
Linux Du Jour
Linux Du JourLinux Du Jour
Linux Du Jour
 
Threads
ThreadsThreads
Threads
 
When the OS gets in the way
When the OS gets in the wayWhen the OS gets in the way
When the OS gets in the way
 
Virtual Machine Introspection with Xen
Virtual Machine Introspection with XenVirtual Machine Introspection with Xen
Virtual Machine Introspection with Xen
 
Troubleshooting MySQL from a MySQL Developer Perspective
Troubleshooting MySQL from a MySQL Developer PerspectiveTroubleshooting MySQL from a MySQL Developer Perspective
Troubleshooting MySQL from a MySQL Developer Perspective
 

Dernier

W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...panagenda
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️Delhi Call girls
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfkalichargn70th171
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantAxelRicardoTrocheRiq
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...OnePlan Solutions
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...kellynguyen01
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...ICS
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comFatema Valibhai
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxComplianceQuest1
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVshikhaohhpro
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...MyIntelliSource, Inc.
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...MyIntelliSource, Inc.
 
Active Directory Penetration Testing, cionsystems.com.pdf
Active Directory Penetration Testing, cionsystems.com.pdfActive Directory Penetration Testing, cionsystems.com.pdf
Active Directory Penetration Testing, cionsystems.com.pdfCionsystems
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsAlberto González Trastoy
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdfWave PLM
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfkalichargn70th171
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Steffen Staab
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerThousandEyes
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsJhone kinadey
 

Dernier (20)

W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service Consultant
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
 
Active Directory Penetration Testing, cionsystems.com.pdf
Active Directory Penetration Testing, cionsystems.com.pdfActive Directory Penetration Testing, cionsystems.com.pdf
Active Directory Penetration Testing, cionsystems.com.pdf
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 

Kernel Process Management

  • 1. @ 2021-21 Embitude Trainings <info@embitude.in> All Rights Reserved Kernel Process Management
  • 2. @ 2021-21 Embitude Trainings <info@embitude.in> All Rights Reserved What to Expect ● Need for synchronization ● Atomic Variables ● Mutex ● Semaphore ● Spinlocks ● Making process wait ● Basic wait mechanism ● Wait queues ● Poll & Select
  • 3. @ 2021-21 Embitude Trainings <info@embitude.in> All Rights Reserved Need for Synchronization ● Multiple tasks run in the system at the same ● Might be sharing the resources ● May result in memory leak or data corruption ● Avoiding the global variables could resolve the issue ● If can’t be avoided, need to use the synchronization mechanism
  • 4. @ 2021-21 Embitude Trainings <info@embitude.in> All Rights Reserved ● Operations that gurantee indivisibility & uninterruptibility ● CPU instructions that provide the atomic read- modify-write operations on a memory location ● Handy for manipulation of shared data atomically in the multithreaded scenario ● Linux kernel uses this for reference counting in shared data structures, wait-notify flags Atomic Operations
  • 5. @ 2021-21 Embitude Trainings <info@embitude.in> All Rights Reserved Atomic Integer Operations ● Generic atomic operation interfaces include support for integer & bitwise operations ● Integer operations operate on atomic_t & atomic64_t ● Operations ● ATOMIC_INIT(i) ● atomic_read(v) ● atomic_set(v, i) ● atomic_add(init i, atomic_t *v) ● atomic_sub(int i, atomic_t *v) ● atomic_inc(atomic_t *v) ● atomic_dec(atomic_t *v)
  • 6. @ 2021-21 Embitude Trainings <info@embitude.in> All Rights Reserved Read Modiy Write & Bitwise operations ● Read Modify Write – bool atomic_sub_and_test(int i, atomic_t *v) – bool atomic_dec_and_test(atomic_t *v) – bool atomic_dec_and_test(atomic_t *v) ● Atomic bitwise operations – set_bit(int nr, volatile unsigned long addr *) – clear_bit(int nr, volatile unsigned logn addr *) – int test_and_set_bit(int nr, volatile unsigned long *addr)
  • 7. @ 2021-21 Embitude Trainings <info@embitude.in> All Rights Reserved ● #include <linux/mutex.h> ● struct mutex ● Initialization – Statically ● DEFINE_MUTEX(mutex) – Dynamically ● mutex_init ● Operations – mutex_lock() – mutex_unlock() – mutex_lock_interruptible() – mutex_trylock() Mutex
  • 8. @ 2021-21 Embitude Trainings <info@embitude.in> All Rights Reserved Semaphore ● Counter – Maintains the count of the resources ● Two operations – up/post – down/wait ● Process calling the down/wait blocks if the value is 0
  • 9. @ 2021-21 Embitude Trainings <info@embitude.in> All Rights Reserved Semaphore APIs ● #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)
  • 10. @ 2021-21 Embitude Trainings <info@embitude.in> All Rights Reserved Reader/Writer Semaphore ● #include <linux/rwsem.h> ● struct rw_semaphore ● Allows one writer or unlimited number of readers to hold the semaphore ● Initialization – void int_rwsem(struct rw_semaphore *rwsem) ● Operations for readers – void down_read(struct rw_semaphore *rwsem) – int down_read_trylock(struct rw_semaphore *rwsem) – void up_read(struct rw_semaphore *rwsem) ● Operations for writers – void down_write(struct rw_semaphore *rwsem) – int down_write_trylock(struct rw_semaphore *rwsem) – void up_write(struct rw_semaphore *rwsem)
  • 11. @ 2021-21 Embitude Trainings <info@embitude.in> All Rights Reserved Spinlocks ● Two operations – Lock – Unlock ● Difference from the mutex – Doesn’t puts the process to sleep, if lock is not available – Instead spins – That’s why the name
  • 12. @ 2021-21 Embitude Trainings <info@embitude.in> All Rights Reserved Spinlock APIs ● #include <linux/spinlock.h> ● Type: spinlock_t ● spin_lock_init(&my_slock) ● spin_lock(&my_slock) ● spin_unlock(&my_slock)
  • 13. @ 2021-21 Embitude Trainings <info@embitude.in> All Rights Reserved Spinlock APIs... ● 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
  • 14. @ 2021-21 Embitude Trainings <info@embitude.in> All Rights Reserved Spin unlock APIs ● void spin_unlock_irqrestore(&my_slock, flags) ● Unlocks & restores the irqs ● void spin_unlock_irq(&my_slock) ● void spin_unlock_bh(&my_slock)
  • 15. @ 2021-21 Embitude Trainings <info@embitude.in> All Rights Reserved Kernel 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
  • 16. @ 2021-21 Embitude Trainings <info@embitude.in> All Rights Reserved Kernel Threads... ● Exists in Kernel space ● Used to perform some background tasks ● Are descended from kthreadd ● Can be viewed in user space with command – ps -fx or ps -mx ● Examples – khubd – ksoftirqd – kworker
  • 17. @ 2021-21 Embitude Trainings <info@embitude.in> All Rights Reserved Creating a Kernel thread ● #include <kthread.h> ● kthread_create(int (*fn)(void *data), const char name[], ...) ● Parameters – fn – The function that the thread has to execute – data – The ‘data’ to the passed to the function – name – The name by which the thread will be recognized in the kernel ● Returns – Pointer to the structure of type task_struct
  • 18. @ 2021-21 Embitude Trainings <info@embitude.in> All Rights Reserved Waking Up & Stopping a Thread ● wake_up_process(struct task_struct *) – Functiion passed to the 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 the thread function
  • 19. @ 2021-21 Embitude Trainings <info@embitude.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 functions ● name – The name by which the thread would be recognized in the kernel
  • 20. @ 2021-21 Embitude Trainings <info@embitude.in> All Rights Reserved Making Process Wait ● While dealing with hardware, process needs to wait for an event: – Specified amount of time – Data from file I/O – Waiting on semaphore or mutex ● Task moves out of run queue and waits for an event ● Task can be in TASK_INTERRUPTIBLE or TASK_UNINTERRUPTIBLE state
  • 21. @ 2021-21 Embitude Trainings <info@embitude.in> All Rights Reserved Basic wait mechanism ● schedule() – Used by the processes to voluntarily relinquish the CPU – Trigger the scheduler ● Set_current_state(state) – Sets the state of the process to: ● TASK_INTERRUPTIBLE ● TASK_UNINTERRUPTIBLE ● TASK_RUNNING
  • 22. @ 2021-21 Embitude Trainings <info@embitude.in> All Rights Reserved Waking up ● wake_up_process(struct task_struct *ts) – Wakes up the process & changes the process state to TASK_RUNNING
  • 23. @ 2021-21 Embitude Trainings <info@embitude.in> All Rights Reserved Wait Queues ● Queue of sleeping processes waiting for an event ● #include <linux/wait.h> ● Data structure – wait_queue_head_t ● Created statically – DECLARE_WAIT_QUEUE_HEAD(wait_queue_na me) ● Created dynamically – wait_queue_head_t my_queue – init_waitqueue_head(&my_queue)
  • 24. @ 2021-21 Embitude Trainings <info@embitude.in> All Rights Reserved Variants for wait ● Wait_event(queue, condition) ● Wait_event_interruptible(queue, condition) ● Wait_event_timeout(queue, condition, timeout) ● Wait_event_interruptible_timeout(queue, condition, timeout)
  • 25. @ 2021-21 Embitude Trainings <info@embitude.in> All Rights Reserved Waking up the Process ● Wake up family of functions ● wake_up(wait_queue_head_t *) – Wakes up all the processes waiting on the queue ● wakes_up_interruptible(wait_queue_head_t *) – Wakes up only the processes performing the interruptible sleep
  • 26. @ 2021-21 Embitude Trainings <info@embitude.in> All Rights Reserved Waiting using wait queue entry ● Intialize 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 ● Clean up – Void finish_wait() ● Pointer to wait_queue_head_t ● Pointer to wait_queue_t
  • 27. @ 2021-21 Embitude Trainings <info@embitude.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 to sleep ● For large number of processes on wait queue, this ‘thundering herd’ may degrade the system performance
  • 28. @ 2021-21 Embitude Trainings <info@embitude.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 wait queue ● Can’t be performed using wait_event()
  • 29. @ 2021-21 Embitude Trainings <info@embitude.in> All Rights Reserved Poll & 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 inputs 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
  • 30. @ 2021-21 Embitude Trainings <info@embitude.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) ● Adds the wait_queues to the 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, POLLWRNORM
  • 31. @ 2021-21 Embitude Trainings <info@embitude.in> All Rights Reserved What all did we learn? ● Need for synchronization ● Atomic Variables ● Mutex ● Semaphore ● Spinlocks ● Making process wait ● Basic wait mechanism ● Wait queues ● Poll & Select