Linux Device Drivers

Linux Device Drivers

Character drivers
IO & Memory
Linux Kernel
Process Management
Process Address space

Linux Scheduler
Memory Management
System Calls
Kernel Synchronization
Linux Inter Process Communications

Serial Ports
Parallel Ports
Introduction to Hardware
Linux Timers
DMA in Linux
Linux Threads
Linux Thread Synchronization

Linux Multi Threading
Debugging in Linux
GDB GNU Debugger
KDB Kernel Debugger
KGDB Kernel GNU Debugger
Example Ethernet Driver

Process Management


     Program -A program is an executable file. It is created by a link editor and resides on a disk file. The program is executed by exec system call.

     Process A process is an instance of a program. The new process is created by issuing fork system call.

     Each process has own area of memory, protected against other process

     Internal view -running as one task and has access to all resources.




A part of task runs in user mode (less privileged)


The concepts of tasks and process are same


A task running in privileged mode, can take one of the number of states: running, interrupt routine, system call, waiting, return from system calls, ready.



Process Control Block


     Description of a process in process structure

     All processes are entered in a doubly linked list and has family relationship

     Each process has its own process ID (pid)



     Inter process communication like System V




task_struct structure


     Process ID: process id and parent pid

     Scheduling: priority, CPU utilization, time spent sleeping

     Process state: run state of the process, status flags, wait info if




     Signal state: signals pending delivery, current mask, and actions

     Tracing information

     Machine-dependent state

     Real-time timer and CPU utilization counters


task_struct structure


     Process group ID: process group and session for this process

     User credentials: real, effective, and saved UID and GID

     Memory Management: virtual address space management (page tables)

     File descriptors: pointers to open file entries and cwd

     Resource Accounting: system/user time used, memory allocation, paging and disk I/O, etc.

     Statistics (kept in user structure): accounting information.

     Signal actions (kept in user structure): pointers to signal handlers, list of blocked signals, etc


Process Identifiers


Every process has a unique process ID, a non-negative integer


Process Ids are reused. As processes terminate, their Ids become candidate for reuse. Most Unix systems has algorithms to delay reuse.


Process ID 0 is usually the scheduler process, known as swapper.


Process ID 1 is the init process, invoked by the kernel at the end of the bootstrap procedure.


#include <unistd.h>

pid_t getpid (void);

returns process ID of the calling process

pid_t getppid (void);

returns parent process ID of the calling process


Process Control


The system calls for process management (create, terminate and priority) are:

fork(), exec(), exit(), wait(), and nice()


fork System call:


The only way in which a new process is created by Linux is for an existing process to execute the fork system call.




The fork system call creates a copy of a process that was executing. The process that executed the fork is called the parent process and the new process is called the child process. The fork system call is called once (by the parent process) but it returns twice (once in the parent and once in the child). The only difference in the returns from the fork system call is that the return value in the parent process is the process id of the newly created child process, while the return value in the child process is zero. If fork it is not successful, -1 is returned.


exec System call


The only way in which a program is executed by Linux is for an existing process to issue the exec system call. The exec system call replaces the current process with the new program. The process does not change. There are six different versions of the exec function:

int execlp(char *filename, char *arg0, char arg1,..... , char *argn, (char *) 0);

int execl(char *pathname, char *arg0, char arg1,..... , char *argn, (char *) 0);

int execle(char *pathname, char *arg0, char arg1,..... , char *argn, (char *) 0, char **envp);

int execvp(char *filename, char **argv);

int execv(char *pathname, char **argv,);

int execve(char *pathname, char **argv, char **envp);


The exec functions return to the caller only if an error occurs. Otherwise control passes to the start of the new program.


wait System call


A process can wait for one of its child process to finish by executing the wait system call.

int wait(int*status);


The value returned by wait is the process id of the child process that terminated. If the process that calls wait function does not have any child processes, wait returns a value of -1 immediately. If the process that calls wait has one or more child processes that has not yet terminated, then the calling process is suspended by the kernel until one of its child processes terminates.


Exit functions


A process can terminate normally in five ways:


1.     Executing a return from the main function

2.     Calling the exit function

3.     Calling the _exit or _Exit function

4.     Executing a return from the start routine of last thread in the process

5.     Calling the pthread_exitfunction from the last thread in the process