SlideShare a Scribd company logo
1 of 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?

More Related Content

What's hot (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
 

Viewers also liked (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
 

Similar to 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
 

Similar to 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
 

More from 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
 

Recently uploaded

Bitdefender-CSG-Report-creat7534-interactive
Bitdefender-CSG-Report-creat7534-interactiveBitdefender-CSG-Report-creat7534-interactive
Bitdefender-CSG-Report-creat7534-interactivestartupro
 
Introduction-to-Wazuh-and-its-integration.pptx
Introduction-to-Wazuh-and-its-integration.pptxIntroduction-to-Wazuh-and-its-integration.pptx
Introduction-to-Wazuh-and-its-integration.pptxmprakaash5
 
2024 April Patch Tuesday
2024 April Patch Tuesday2024 April Patch Tuesday
2024 April Patch TuesdayIvanti
 
WomenInAutomation2024: AI and Automation for eveyone
WomenInAutomation2024: AI and Automation for eveyoneWomenInAutomation2024: AI and Automation for eveyone
WomenInAutomation2024: AI and Automation for eveyoneUiPathCommunity
 
Generative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfGenerative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfIngrid Airi González
 
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesHow to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesThousandEyes
 
Which standard is best for your content?
Which standard is best for your content?Which standard is best for your content?
Which standard is best for your content?Rustici Software
 
HCI Lesson 1 - Introduction to Human-Computer Interaction.pdf
HCI Lesson 1 - Introduction to Human-Computer Interaction.pdfHCI Lesson 1 - Introduction to Human-Computer Interaction.pdf
HCI Lesson 1 - Introduction to Human-Computer Interaction.pdfROWELL MARQUINA
 
Green paths: Learning from publishers’ sustainability journeys - Tech Forum 2024
Green paths: Learning from publishers’ sustainability journeys - Tech Forum 2024Green paths: Learning from publishers’ sustainability journeys - Tech Forum 2024
Green paths: Learning from publishers’ sustainability journeys - Tech Forum 2024BookNet Canada
 
Automation Ops Series: Session 3 - Solutions management
Automation Ops Series: Session 3 - Solutions managementAutomation Ops Series: Session 3 - Solutions management
Automation Ops Series: Session 3 - Solutions managementDianaGray10
 
Laying the Data Foundations for Artificial Intelligence!
Laying the Data Foundations for Artificial Intelligence!Laying the Data Foundations for Artificial Intelligence!
Laying the Data Foundations for Artificial Intelligence!Memoori
 
React Native vs Ionic - The Best Mobile App Framework
React Native vs Ionic - The Best Mobile App FrameworkReact Native vs Ionic - The Best Mobile App Framework
React Native vs Ionic - The Best Mobile App FrameworkPixlogix Infotech
 
Deliver Latency Free Customer Experience
Deliver Latency Free Customer ExperienceDeliver Latency Free Customer Experience
Deliver Latency Free Customer ExperienceOpsTree solutions
 
Accelerating Enterprise Software Engineering with Platformless
Accelerating Enterprise Software Engineering with PlatformlessAccelerating Enterprise Software Engineering with Platformless
Accelerating Enterprise Software Engineering with PlatformlessWSO2
 
A Glance At The Java Performance Toolbox
A Glance At The Java Performance ToolboxA Glance At The Java Performance Toolbox
A Glance At The Java Performance ToolboxAna-Maria Mihalceanu
 
Transcript: New from BookNet Canada for 2024: BNC SalesData and LibraryData -...
Transcript: New from BookNet Canada for 2024: BNC SalesData and LibraryData -...Transcript: New from BookNet Canada for 2024: BNC SalesData and LibraryData -...
Transcript: New from BookNet Canada for 2024: BNC SalesData and LibraryData -...BookNet Canada
 
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotesMuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotesManik S Magar
 
A PowerPoint Presentation on Vikram Lander pptx
A PowerPoint Presentation on Vikram Lander pptxA PowerPoint Presentation on Vikram Lander pptx
A PowerPoint Presentation on Vikram Lander pptxatharvdev2010
 
Women in Automation 2024: Career session - explore career paths in automation
Women in Automation 2024: Career session - explore career paths in automationWomen in Automation 2024: Career session - explore career paths in automation
Women in Automation 2024: Career session - explore career paths in automationDianaGray10
 
Bridging Between CAD & GIS: 6 Ways to Automate Your Data Integration
Bridging Between CAD & GIS:  6 Ways to Automate Your Data IntegrationBridging Between CAD & GIS:  6 Ways to Automate Your Data Integration
Bridging Between CAD & GIS: 6 Ways to Automate Your Data Integrationmarketing932765
 

Recently uploaded (20)

Bitdefender-CSG-Report-creat7534-interactive
Bitdefender-CSG-Report-creat7534-interactiveBitdefender-CSG-Report-creat7534-interactive
Bitdefender-CSG-Report-creat7534-interactive
 
Introduction-to-Wazuh-and-its-integration.pptx
Introduction-to-Wazuh-and-its-integration.pptxIntroduction-to-Wazuh-and-its-integration.pptx
Introduction-to-Wazuh-and-its-integration.pptx
 
2024 April Patch Tuesday
2024 April Patch Tuesday2024 April Patch Tuesday
2024 April Patch Tuesday
 
WomenInAutomation2024: AI and Automation for eveyone
WomenInAutomation2024: AI and Automation for eveyoneWomenInAutomation2024: AI and Automation for eveyone
WomenInAutomation2024: AI and Automation for eveyone
 
Generative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfGenerative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdf
 
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesHow to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
 
Which standard is best for your content?
Which standard is best for your content?Which standard is best for your content?
Which standard is best for your content?
 
HCI Lesson 1 - Introduction to Human-Computer Interaction.pdf
HCI Lesson 1 - Introduction to Human-Computer Interaction.pdfHCI Lesson 1 - Introduction to Human-Computer Interaction.pdf
HCI Lesson 1 - Introduction to Human-Computer Interaction.pdf
 
Green paths: Learning from publishers’ sustainability journeys - Tech Forum 2024
Green paths: Learning from publishers’ sustainability journeys - Tech Forum 2024Green paths: Learning from publishers’ sustainability journeys - Tech Forum 2024
Green paths: Learning from publishers’ sustainability journeys - Tech Forum 2024
 
Automation Ops Series: Session 3 - Solutions management
Automation Ops Series: Session 3 - Solutions managementAutomation Ops Series: Session 3 - Solutions management
Automation Ops Series: Session 3 - Solutions management
 
Laying the Data Foundations for Artificial Intelligence!
Laying the Data Foundations for Artificial Intelligence!Laying the Data Foundations for Artificial Intelligence!
Laying the Data Foundations for Artificial Intelligence!
 
React Native vs Ionic - The Best Mobile App Framework
React Native vs Ionic - The Best Mobile App FrameworkReact Native vs Ionic - The Best Mobile App Framework
React Native vs Ionic - The Best Mobile App Framework
 
Deliver Latency Free Customer Experience
Deliver Latency Free Customer ExperienceDeliver Latency Free Customer Experience
Deliver Latency Free Customer Experience
 
Accelerating Enterprise Software Engineering with Platformless
Accelerating Enterprise Software Engineering with PlatformlessAccelerating Enterprise Software Engineering with Platformless
Accelerating Enterprise Software Engineering with Platformless
 
A Glance At The Java Performance Toolbox
A Glance At The Java Performance ToolboxA Glance At The Java Performance Toolbox
A Glance At The Java Performance Toolbox
 
Transcript: New from BookNet Canada for 2024: BNC SalesData and LibraryData -...
Transcript: New from BookNet Canada for 2024: BNC SalesData and LibraryData -...Transcript: New from BookNet Canada for 2024: BNC SalesData and LibraryData -...
Transcript: New from BookNet Canada for 2024: BNC SalesData and LibraryData -...
 
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotesMuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
 
A PowerPoint Presentation on Vikram Lander pptx
A PowerPoint Presentation on Vikram Lander pptxA PowerPoint Presentation on Vikram Lander pptx
A PowerPoint Presentation on Vikram Lander pptx
 
Women in Automation 2024: Career session - explore career paths in automation
Women in Automation 2024: Career session - explore career paths in automationWomen in Automation 2024: Career session - explore career paths in automation
Women in Automation 2024: Career session - explore career paths in automation
 
Bridging Between CAD & GIS: 6 Ways to Automate Your Data Integration
Bridging Between CAD & GIS:  6 Ways to Automate Your Data IntegrationBridging Between CAD & GIS:  6 Ways to Automate Your Data Integration
Bridging Between CAD & GIS: 6 Ways to Automate Your Data Integration
 

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?