Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Hands-On System Programming with Linux

You're reading from   Hands-On System Programming with Linux Explore Linux system programming interfaces, theory, and practice

Arrow left icon
Product type Paperback
Published in Oct 2018
Publisher Packt
ISBN-13 9781788998475
Length 794 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Authors (2):
Arrow left icon
Tigran Aivazian Tigran Aivazian
Author Profile Icon Tigran Aivazian
Tigran Aivazian
Kaiwan N. Billimoria Kaiwan N. Billimoria
Author Profile Icon Kaiwan N. Billimoria
Kaiwan N. Billimoria
Arrow right icon
View More author details
Toc

Table of Contents (21) Chapters Close

Preface 1. Linux System Architecture 2. Virtual Memory FREE CHAPTER 3. Resource Limits 4. Dynamic Memory Allocation 5. Linux Memory Issues 6. Debugging Tools for Memory Issues 7. Process Credentials 8. Process Capabilities 9. Process Execution 10. Process Creation 11. Signaling - Part I 12. Signaling - Part II 13. Timers 14. Multithreading with Pthreads Part I - Essentials 15. Multithreading with Pthreads Part II - Synchronization 16. Multithreading with Pthreads Part III 17. CPU Scheduling on Linux 18. Advanced File I/O 19. Troubleshooting and Best Practices 20. Other Books You May Enjoy

Execution contexts within the kernel

Kernel code always executes in one of two contexts:

  • Process
  • Interrupt
It's easy to get confused here. Remember, this discussion applies to the context in which kernel code executes, not userspace code.

Process context

Now we understand that one can invoke kernel services by issuing a system call. When this occurs, the calling process runs the kernel code of the system call in kernel mode. This is termed process context kernel code is now running in the context of the process that invoked the system call.

Process context code has the following attributes:

  • Always triggered by a process (or thread) issuing a system call
  • Top-down approach
  • Synchronous execution of kernel code by a process

Interrupt context

At first glance, there appears to be no other way that kernel code executes. Well, think about this scenario: the network receive path. A network packet destined for your Ethernet MAC address arrives at the hardware adapter, the hardware detects that it's meant for it, collects it, and buffers it. It now must let the OS know; more technically, it must let the Network Interface Card (NIC) device driver know, so that it can fetch and process packets as they arrive. It kicks the NIC driver into action by asserting a hardware interrupt.

Recall that device drivers reside in kernel-space, and therefore their code runs in Supervisor or kernel Mode. The (kernel privilege) driver code Interrupt service routine (ISR) now executes, fetches the packet, and sends it up the OS network protocol stack for processing.

The NIC driver's ISR code is kernel code, and it is has run but in what context? It's obviously not in the context of any particular process. In fact, the hardware interrupt probably interrupted some process. Thus, we just call this interrupt context.

The interrupt context code has the following attributes:

  • Always triggered by a hardware interrupt (not a software interrupt, fault or exception; that's still process context)
  • Bottom-up approach
  • Asynchronous execution of kernel code by an interrupt
If, at some point, you do report a kernel bug, it helps if you point out the execution context.

Technically, within interrupt context, we have further distinctions, such as hard-IRQs and softirqs, bottom halves, and tasklets. However, this discussion goes beyond the scope of this book.

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €18.99/month. Cancel anytime