SlideShare ist ein Scribd-Unternehmen logo
1 von 25
© 2010-15 Sysplay Workshops <workshop@sysplay.in>
All Rights Reserved.
Kernel Programming
2© 2010-15 Sysplay Workshops <workshop@sysplay.in>
All Rights Reserved.
What to Expect?
How to do programming in “Kernel C” for
Achieving Concurrency
Keeping Time
Providing Delays
Timer Control
3© 2010-15 Sysplay Workshops <workshop@sysplay.in>
All Rights Reserved.
Concurrency
4© 2010-15 Sysplay Workshops <workshop@sysplay.in>
All Rights Reserved.
Concurrency with Locking
Mutexes
Header: <linux/mutex.h>
Type: struct mutex
APIs
DEFINE_MUTEX
mutex_is_locked
mutex_lock, mutex_trylock, mutex_unlock
Semaphores
Header: <linux/semaphore.h>
Type: struct semaphore
APIs
sema_init
down, down_trylock, down_interruptible, up
5© 2010-15 Sysplay Workshops <workshop@sysplay.in>
All Rights Reserved.
Concurrency w/ Locking (cont.)
Spin Locks
Header <linux/spinlock.h>
Type: spinlock_t
APIs
spin_lock_init
spin_[try]lock, spin_unlock
Reader-Writer Locks
Header: <linux/spinlock.h>
Type: rwlock_t
APIs
read_lock, read_unlock
write_lock, write_unlock
6© 2010-15 Sysplay Workshops <workshop@sysplay.in>
All Rights Reserved.
Concurrency without Locking
Atomic Variables
Header: <asm-generic/atomic.h>
Type: atomic_t
Macros
ATOMIC_INIT
atomic_read, atomic_set
atomic_add, atomic_sub, atomic_inc, atomic_dec
atomic_xchg
7© 2010-15 Sysplay Workshops <workshop@sysplay.in>
All Rights Reserved.
Concurrency w/o Locking (cont.)
Atomic Bit Operations
Header: <linux/bitops.h>
APIs
rol8, rol16, rol32, ror8, ror16, ror32
find_first_bit, find_first_zero_bit
find_last_bit
find_next_bit, find_next_zero_bit
Header: <asm-generic/bitops.h>
APIs
set_bit, clear_bit, change_bit
test_and_set_bit, test_and_clear_bit, test_and_change_bit
8© 2010-15 Sysplay Workshops <workshop@sysplay.in>
All Rights Reserved.
Wait Queues
Wait Queues
Header: <linux/wait.h>
Wait Queue Head APIs
DECLARE_WAIT_QUEUE_HEAD(wq);
wait_event_interruptible(wq, cond);
wait_event_interruptible_timeout(wq, cond, timeout);
wake_up_interruptible(&wq);
... (non-interruptible set)
Wait Queue APIs
DECLARE_WAITQUEUE(w, current);
add_wait_queue(&wq, &w);
remove_wait_queue(&wq, &w);
9© 2010-15 Sysplay Workshops <workshop@sysplay.in>
All Rights Reserved.
Time Keeping
10© 2010-15 Sysplay Workshops <workshop@sysplay.in>
All Rights Reserved.
Time since Bootup
tick – Kernel's unit of time. Also called jiffy
HZ – ticks per second
Defined in Header: <linux/param.h>
Typically, 1000 for desktops, 100 for embedded systems
1 tick = 1ms (desktop), 10ms (embedded systems)
Variables: jiffies & jiffies_64
Header: <linux/jiffies.h>
APIs
time_after, time_before, time_in_range, ...
get_jiffies_64, ...
msec_to_jiffies, timespec_to_jiffies, timeval_to_jiffies, …
jiffies_to_msec, jiffies_to_timespec, jiffies_to_timeval, ...
11© 2010-15 Sysplay Workshops <workshop@sysplay.in>
All Rights Reserved.
Time since Bootup (cont.)
Platform specific “Time Stamp Counter”
On x86
Header: <asm/msr.h>
API: rdtsc(ul low_tsc_ticks, ul high_tsc_ticks);
Getting it generically
Header: <linux/timex.h>
API: read_current_timer(unsigned long *timer_val);
12© 2010-15 Sysplay Workshops <workshop@sysplay.in>
All Rights Reserved.
Absolute Time
Header: <linux/time.h>
APIs
mktime(y, m, d, h, m, s) – Seconds since Epoch
void do_gettimeofday(struct timeval *tv);
struct timespec current_kernel_time(void);
13© 2010-15 Sysplay Workshops <workshop@sysplay.in>
All Rights Reserved.
Delays
14© 2010-15 Sysplay Workshops <workshop@sysplay.in>
All Rights Reserved.
Long Delays
Busy wait: cpu_relax
while (time_before(jiffies, j1))
cpu_relax();
Yielding: schedule/schedule_timeout
while (time_before(jiffies, j1))
schedule();
15© 2010-15 Sysplay Workshops <workshop@sysplay.in>
All Rights Reserved.
Short Delays but Busy Waiting
Header: <linux/delay.h>
Arch. specific Header: <asm/delay.h>
APIs
void ndelay(unsigned long ndelays);
void udelay(unsigned long udelays);
void mdelay(unsigned long mdelays);
16© 2010-15 Sysplay Workshops <workshop@sysplay.in>
All Rights Reserved.
Long Delays: Back to Yielding
Header: <linux/delay.h>
APIs
void msleep(unsigned int millisecs);
unsigned long msleep_interruptible(unsigned int millisecs);
void ssleep(unsigned int secs);
17© 2010-15 Sysplay Workshops <workshop@sysplay.in>
All Rights Reserved.
Timers
18© 2010-15 Sysplay Workshops <workshop@sysplay.in>
All Rights Reserved.
Kernel Timers
Back end of the various delays
Header: <linux/timer.h>
Type: struct timer_list
APIs
void init_timer(struct timer_list *); /* Nullifies */
struct timer_list TIMER_INITIALIZER(f, t, p);
void add_timer(struct timer_list *);
void del_timer(struct timer_list *);
int mod_timer(struct timer_list *, unsigned long);
int del_timer_sync(struct timer_list *);
19© 2010-15 Sysplay Workshops <workshop@sysplay.in>
All Rights Reserved.
Tasklets
Timers without specific Timing
Header: <linux/interrupt.h>
Type: struct tasklet_struct
APIs
void tasklet_init(struct tasklet_struct *t, void (*func)
(unsigned long), unsigned long data);
void tasklet_kill(struct tasklet_struct *t);
DECLARE_TASKLET(name, func, data);
tasklet_enable(t), tasklet_disable(t)
tasklet_[hi_]schedule(t);
20© 2010-15 Sysplay Workshops <workshop@sysplay.in>
All Rights Reserved.
Work Queues
In context of “Special Kernel Thread”
Header: <linux/workqueue.h>
Types: struct workqueue_struct, struct work_struct
Work Queue APIs
q = create_workqueue(name);
q = create_singlethread_workqueue(name);
flush_workqueue(q);
destroy_workqueue(q);
Work APIs
DECLARE_WORK(w, void (*function)(void *), void *data);
INIT_WORK(w, void (*function)(void *), void *data);
Combined APIs
int queue_work(q, &w);
int queue_delayed_work(q, &w, d);
int cancel_delayed_work(&w);
Global Shared Work Queue API
schedule_work(&w);
21© 2010-15 Sysplay Workshops <workshop@sysplay.in>
All Rights Reserved.
Helper Interfaces
22© 2010-15 Sysplay Workshops <workshop@sysplay.in>
All Rights Reserved.
Other Helper Interfaces in Latest
Kernels
User Mode Helper
Linked Lists
Hash Lists
Notifier Chains
Completion Interface
Kthread Helpers
23© 2010-15 Sysplay Workshops <workshop@sysplay.in>
All Rights Reserved.
24© 2010-15 Sysplay Workshops <workshop@sysplay.in>
All Rights Reserved.
What to Expect?
How to do programming in “Kernel C” for
Achieving Concurrency
With & without Locking
Wait Queues
Keeping Time
Relative & Absolute
Providing Delays
Long and Short
Busy Wait and Yielding
Timer Control
Kernel Timers
Tasklets
Work Queues
25© 2010-15 Sysplay Workshops <workshop@sysplay.in>
All Rights Reserved.
Any Queries?

Weitere ähnliche Inhalte

Was ist angesagt? (20)

Bootloaders
BootloadersBootloaders
Bootloaders
 
SPI Drivers
SPI DriversSPI Drivers
SPI Drivers
 
BeagleBone Black Bootloaders
BeagleBone Black BootloadersBeagleBone Black Bootloaders
BeagleBone Black Bootloaders
 
Processes
ProcessesProcesses
Processes
 
Linux DMA Engine
Linux DMA EngineLinux DMA Engine
Linux DMA Engine
 
Shell Scripting
Shell ScriptingShell Scripting
Shell Scripting
 
Kernel Timing Management
Kernel Timing ManagementKernel Timing Management
Kernel Timing Management
 
Embedded Software Design
Embedded Software DesignEmbedded Software Design
Embedded Software Design
 
Character Drivers
Character DriversCharacter Drivers
Character Drivers
 
Linux Kernel Overview
Linux Kernel OverviewLinux Kernel Overview
Linux Kernel Overview
 
Video Drivers
Video DriversVideo Drivers
Video Drivers
 
Synchronization
SynchronizationSynchronization
Synchronization
 
Linux User Space Debugging & Profiling
Linux User Space Debugging & ProfilingLinux User Space Debugging & Profiling
Linux User Space Debugging & Profiling
 
System Calls
System CallsSystem Calls
System Calls
 
Signals
SignalsSignals
Signals
 
Embedded Storage Management
Embedded Storage ManagementEmbedded Storage Management
Embedded Storage Management
 
BeagleBoard-xM Booting Process
BeagleBoard-xM Booting ProcessBeagleBoard-xM Booting Process
BeagleBoard-xM Booting Process
 
Mobile Hacking using Linux Drivers
Mobile Hacking using Linux DriversMobile Hacking using Linux Drivers
Mobile Hacking using Linux Drivers
 
Processes
ProcessesProcesses
Processes
 
Low-level Accesses
Low-level AccessesLow-level Accesses
Low-level Accesses
 

Andere mochten auch (9)

Audio Drivers
Audio DriversAudio Drivers
Audio Drivers
 
USB Drivers
USB DriversUSB Drivers
USB Drivers
 
SPI Drivers
SPI DriversSPI Drivers
SPI Drivers
 
PCI Drivers
PCI DriversPCI Drivers
PCI Drivers
 
Interrupts
InterruptsInterrupts
Interrupts
 
Serial Drivers
Serial DriversSerial Drivers
Serial Drivers
 
Platform Drivers
Platform DriversPlatform Drivers
Platform Drivers
 
Network Drivers
Network DriversNetwork Drivers
Network Drivers
 
I2C Drivers
I2C DriversI2C Drivers
I2C Drivers
 

Ähnlich wie Kernel Programming

리눅스 드라이버 #2
리눅스 드라이버 #2리눅스 드라이버 #2
리눅스 드라이버 #2Sangho Park
 
Dan Norris: Exadata security
Dan Norris: Exadata securityDan Norris: Exadata security
Dan Norris: Exadata securityKyle Hailey
 
Node.js primer for ITE students
Node.js primer for ITE studentsNode.js primer for ITE students
Node.js primer for ITE studentsQuhan Arunasalam
 
OUGLS 2016: How profiling works in MySQL
OUGLS 2016: How profiling works in MySQLOUGLS 2016: How profiling works in MySQL
OUGLS 2016: How profiling works in MySQLGeorgi Kodinov
 
Quick and Easy Device Drivers for Embedded Linux Using UIO
Quick and Easy Device Drivers for Embedded Linux Using UIOQuick and Easy Device Drivers for Embedded Linux Using UIO
Quick and Easy Device Drivers for Embedded Linux Using UIOChris Simmonds
 
Kernel Timing Management
Kernel Timing ManagementKernel Timing Management
Kernel Timing Managementpradeep_tewani
 
Mysql performance tuning
Mysql performance tuningMysql performance tuning
Mysql performance tuningPhilip Zhong
 
Oracle Solaris 11.1 New Features
Oracle Solaris 11.1 New FeaturesOracle Solaris 11.1 New Features
Oracle Solaris 11.1 New FeaturesOrgad Kimchi
 
101 1.3 runlevels , shutdown, and reboot
101 1.3 runlevels , shutdown, and reboot101 1.3 runlevels , shutdown, and reboot
101 1.3 runlevels , shutdown, and rebootAcácio Oliveira
 
101 1.3 runlevels, shutdown, and reboot v2
101 1.3 runlevels, shutdown, and reboot v2101 1.3 runlevels, shutdown, and reboot v2
101 1.3 runlevels, shutdown, and reboot v2Acácio Oliveira
 
SHARE.ORG Orlando 2015
SHARE.ORG Orlando 2015SHARE.ORG Orlando 2015
SHARE.ORG Orlando 2015Filipe Miranda
 
Introduction to Embedded Systems
Introduction to Embedded SystemsIntroduction to Embedded Systems
Introduction to Embedded SystemsAnil Kumar Pugalia
 
Hangover - Que pasó ayer? Troubleshooting con vistas ASH & S-ASH
Hangover - Que pasó ayer?   Troubleshooting con vistas ASH & S-ASHHangover - Que pasó ayer?   Troubleshooting con vistas ASH & S-ASH
Hangover - Que pasó ayer? Troubleshooting con vistas ASH & S-ASHRoy Salazar
 

Ähnlich wie Kernel Programming (20)

리눅스 드라이버 #2
리눅스 드라이버 #2리눅스 드라이버 #2
리눅스 드라이버 #2
 
Embedded Applications
Embedded ApplicationsEmbedded Applications
Embedded Applications
 
Introduction to Linux
Introduction to LinuxIntroduction to Linux
Introduction to Linux
 
Dan Norris: Exadata security
Dan Norris: Exadata securityDan Norris: Exadata security
Dan Norris: Exadata security
 
Node.js primer for ITE students
Node.js primer for ITE studentsNode.js primer for ITE students
Node.js primer for ITE students
 
OUGLS 2016: How profiling works in MySQL
OUGLS 2016: How profiling works in MySQLOUGLS 2016: How profiling works in MySQL
OUGLS 2016: How profiling works in MySQL
 
Quick and Easy Device Drivers for Embedded Linux Using UIO
Quick and Easy Device Drivers for Embedded Linux Using UIOQuick and Easy Device Drivers for Embedded Linux Using UIO
Quick and Easy Device Drivers for Embedded Linux Using UIO
 
Kernel Timing Management
Kernel Timing ManagementKernel Timing Management
Kernel Timing Management
 
Linux scheduler
Linux schedulerLinux scheduler
Linux scheduler
 
Mysql performance tuning
Mysql performance tuningMysql performance tuning
Mysql performance tuning
 
Oracle Solaris 11.1 New Features
Oracle Solaris 11.1 New FeaturesOracle Solaris 11.1 New Features
Oracle Solaris 11.1 New Features
 
101 1.3 runlevels , shutdown, and reboot
101 1.3 runlevels , shutdown, and reboot101 1.3 runlevels , shutdown, and reboot
101 1.3 runlevels , shutdown, and reboot
 
101 1.3 runlevels, shutdown, and reboot v2
101 1.3 runlevels, shutdown, and reboot v2101 1.3 runlevels, shutdown, and reboot v2
101 1.3 runlevels, shutdown, and reboot v2
 
SHARE.ORG Orlando 2015
SHARE.ORG Orlando 2015SHARE.ORG Orlando 2015
SHARE.ORG Orlando 2015
 
Introduction to Embedded Systems
Introduction to Embedded SystemsIntroduction to Embedded Systems
Introduction to Embedded Systems
 
Hangover - Que pasó ayer? Troubleshooting con vistas ASH & S-ASH
Hangover - Que pasó ayer?   Troubleshooting con vistas ASH & S-ASHHangover - Que pasó ayer?   Troubleshooting con vistas ASH & S-ASH
Hangover - Que pasó ayer? Troubleshooting con vistas ASH & S-ASH
 
Vx works RTOS
Vx works RTOSVx works RTOS
Vx works RTOS
 
Sjug aug 2010_cloud
Sjug aug 2010_cloudSjug aug 2010_cloud
Sjug aug 2010_cloud
 
Linux Porting
Linux PortingLinux Porting
Linux Porting
 
Inter Process Communication
Inter Process CommunicationInter Process Communication
Inter Process Communication
 

Mehr von Anil Kumar Pugalia (15)

File System Modules
File System ModulesFile System Modules
File System Modules
 
Embedded C
Embedded CEmbedded C
Embedded C
 
Playing with R L C Circuits
Playing with R L C CircuitsPlaying with R L C Circuits
Playing with R L C Circuits
 
References
ReferencesReferences
References
 
Functional Programming with LISP
Functional Programming with LISPFunctional Programming with LISP
Functional Programming with LISP
 
Power of vi
Power of viPower of vi
Power of vi
 
gcc and friends
gcc and friendsgcc and friends
gcc and friends
 
"make" system
"make" system"make" system
"make" system
 
Hardware Design for Software Hackers
Hardware Design for Software HackersHardware Design for Software Hackers
Hardware Design for Software Hackers
 
RPM Building
RPM BuildingRPM Building
RPM Building
 
Linux Network Management
Linux Network ManagementLinux Network Management
Linux Network Management
 
Timers
TimersTimers
Timers
 
Threads
ThreadsThreads
Threads
 
Linux Memory Management
Linux Memory ManagementLinux Memory Management
Linux Memory Management
 
Linux File System
Linux File SystemLinux File System
Linux File System
 

Kürzlich hochgeladen

Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
[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
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxOnBoard
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptxLBM Solutions
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Paola De la Torre
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
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
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksSoftradix Technologies
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAndikSusilo4
 
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
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 

Kürzlich hochgeladen (20)

Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
[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
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptx
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping Elbows
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptx
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
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
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other Frameworks
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & Application
 
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
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 

Kernel Programming

  • 1. © 2010-15 Sysplay Workshops <workshop@sysplay.in> All Rights Reserved. Kernel Programming
  • 2. 2© 2010-15 Sysplay Workshops <workshop@sysplay.in> All Rights Reserved. What to Expect? How to do programming in “Kernel C” for Achieving Concurrency Keeping Time Providing Delays Timer Control
  • 3. 3© 2010-15 Sysplay Workshops <workshop@sysplay.in> All Rights Reserved. Concurrency
  • 4. 4© 2010-15 Sysplay Workshops <workshop@sysplay.in> All Rights Reserved. Concurrency with Locking Mutexes Header: <linux/mutex.h> Type: struct mutex APIs DEFINE_MUTEX mutex_is_locked mutex_lock, mutex_trylock, mutex_unlock Semaphores Header: <linux/semaphore.h> Type: struct semaphore APIs sema_init down, down_trylock, down_interruptible, up
  • 5. 5© 2010-15 Sysplay Workshops <workshop@sysplay.in> All Rights Reserved. Concurrency w/ Locking (cont.) Spin Locks Header <linux/spinlock.h> Type: spinlock_t APIs spin_lock_init spin_[try]lock, spin_unlock Reader-Writer Locks Header: <linux/spinlock.h> Type: rwlock_t APIs read_lock, read_unlock write_lock, write_unlock
  • 6. 6© 2010-15 Sysplay Workshops <workshop@sysplay.in> All Rights Reserved. Concurrency without Locking Atomic Variables Header: <asm-generic/atomic.h> Type: atomic_t Macros ATOMIC_INIT atomic_read, atomic_set atomic_add, atomic_sub, atomic_inc, atomic_dec atomic_xchg
  • 7. 7© 2010-15 Sysplay Workshops <workshop@sysplay.in> All Rights Reserved. Concurrency w/o Locking (cont.) Atomic Bit Operations Header: <linux/bitops.h> APIs rol8, rol16, rol32, ror8, ror16, ror32 find_first_bit, find_first_zero_bit find_last_bit find_next_bit, find_next_zero_bit Header: <asm-generic/bitops.h> APIs set_bit, clear_bit, change_bit test_and_set_bit, test_and_clear_bit, test_and_change_bit
  • 8. 8© 2010-15 Sysplay Workshops <workshop@sysplay.in> All Rights Reserved. Wait Queues Wait Queues Header: <linux/wait.h> Wait Queue Head APIs DECLARE_WAIT_QUEUE_HEAD(wq); wait_event_interruptible(wq, cond); wait_event_interruptible_timeout(wq, cond, timeout); wake_up_interruptible(&wq); ... (non-interruptible set) Wait Queue APIs DECLARE_WAITQUEUE(w, current); add_wait_queue(&wq, &w); remove_wait_queue(&wq, &w);
  • 9. 9© 2010-15 Sysplay Workshops <workshop@sysplay.in> All Rights Reserved. Time Keeping
  • 10. 10© 2010-15 Sysplay Workshops <workshop@sysplay.in> All Rights Reserved. Time since Bootup tick – Kernel's unit of time. Also called jiffy HZ – ticks per second Defined in Header: <linux/param.h> Typically, 1000 for desktops, 100 for embedded systems 1 tick = 1ms (desktop), 10ms (embedded systems) Variables: jiffies & jiffies_64 Header: <linux/jiffies.h> APIs time_after, time_before, time_in_range, ... get_jiffies_64, ... msec_to_jiffies, timespec_to_jiffies, timeval_to_jiffies, … jiffies_to_msec, jiffies_to_timespec, jiffies_to_timeval, ...
  • 11. 11© 2010-15 Sysplay Workshops <workshop@sysplay.in> All Rights Reserved. Time since Bootup (cont.) Platform specific “Time Stamp Counter” On x86 Header: <asm/msr.h> API: rdtsc(ul low_tsc_ticks, ul high_tsc_ticks); Getting it generically Header: <linux/timex.h> API: read_current_timer(unsigned long *timer_val);
  • 12. 12© 2010-15 Sysplay Workshops <workshop@sysplay.in> All Rights Reserved. Absolute Time Header: <linux/time.h> APIs mktime(y, m, d, h, m, s) – Seconds since Epoch void do_gettimeofday(struct timeval *tv); struct timespec current_kernel_time(void);
  • 13. 13© 2010-15 Sysplay Workshops <workshop@sysplay.in> All Rights Reserved. Delays
  • 14. 14© 2010-15 Sysplay Workshops <workshop@sysplay.in> All Rights Reserved. Long Delays Busy wait: cpu_relax while (time_before(jiffies, j1)) cpu_relax(); Yielding: schedule/schedule_timeout while (time_before(jiffies, j1)) schedule();
  • 15. 15© 2010-15 Sysplay Workshops <workshop@sysplay.in> All Rights Reserved. Short Delays but Busy Waiting Header: <linux/delay.h> Arch. specific Header: <asm/delay.h> APIs void ndelay(unsigned long ndelays); void udelay(unsigned long udelays); void mdelay(unsigned long mdelays);
  • 16. 16© 2010-15 Sysplay Workshops <workshop@sysplay.in> All Rights Reserved. Long Delays: Back to Yielding Header: <linux/delay.h> APIs void msleep(unsigned int millisecs); unsigned long msleep_interruptible(unsigned int millisecs); void ssleep(unsigned int secs);
  • 17. 17© 2010-15 Sysplay Workshops <workshop@sysplay.in> All Rights Reserved. Timers
  • 18. 18© 2010-15 Sysplay Workshops <workshop@sysplay.in> All Rights Reserved. Kernel Timers Back end of the various delays Header: <linux/timer.h> Type: struct timer_list APIs void init_timer(struct timer_list *); /* Nullifies */ struct timer_list TIMER_INITIALIZER(f, t, p); void add_timer(struct timer_list *); void del_timer(struct timer_list *); int mod_timer(struct timer_list *, unsigned long); int del_timer_sync(struct timer_list *);
  • 19. 19© 2010-15 Sysplay Workshops <workshop@sysplay.in> All Rights Reserved. Tasklets Timers without specific Timing Header: <linux/interrupt.h> Type: struct tasklet_struct APIs void tasklet_init(struct tasklet_struct *t, void (*func) (unsigned long), unsigned long data); void tasklet_kill(struct tasklet_struct *t); DECLARE_TASKLET(name, func, data); tasklet_enable(t), tasklet_disable(t) tasklet_[hi_]schedule(t);
  • 20. 20© 2010-15 Sysplay Workshops <workshop@sysplay.in> All Rights Reserved. Work Queues In context of “Special Kernel Thread” Header: <linux/workqueue.h> Types: struct workqueue_struct, struct work_struct Work Queue APIs q = create_workqueue(name); q = create_singlethread_workqueue(name); flush_workqueue(q); destroy_workqueue(q); Work APIs DECLARE_WORK(w, void (*function)(void *), void *data); INIT_WORK(w, void (*function)(void *), void *data); Combined APIs int queue_work(q, &w); int queue_delayed_work(q, &w, d); int cancel_delayed_work(&w); Global Shared Work Queue API schedule_work(&w);
  • 21. 21© 2010-15 Sysplay Workshops <workshop@sysplay.in> All Rights Reserved. Helper Interfaces
  • 22. 22© 2010-15 Sysplay Workshops <workshop@sysplay.in> All Rights Reserved. Other Helper Interfaces in Latest Kernels User Mode Helper Linked Lists Hash Lists Notifier Chains Completion Interface Kthread Helpers
  • 23. 23© 2010-15 Sysplay Workshops <workshop@sysplay.in> All Rights Reserved.
  • 24. 24© 2010-15 Sysplay Workshops <workshop@sysplay.in> All Rights Reserved. What to Expect? How to do programming in “Kernel C” for Achieving Concurrency With & without Locking Wait Queues Keeping Time Relative & Absolute Providing Delays Long and Short Busy Wait and Yielding Timer Control Kernel Timers Tasklets Work Queues
  • 25. 25© 2010-15 Sysplay Workshops <workshop@sysplay.in> All Rights Reserved. Any Queries?