You are on page 1of 27

Processes and Scheduling

• • • • • • Processes Process Descriptor and Task Structure Process Creation Kernel Implementation of Threads Process Termination Process Scheduling
– – – – Processor Bound and I/O Bound Policy Linux Scheduling Algorithm Preemption and Context Switching

• System Calls

• The process is one of the fundamental abstractions in Unix operating systems • Processes provide two virtualizations: a virtualized processor and virtual memory. • Linux has a unique implementation of threads: It does not differentiate between threads and processes. • Thread is just a special kind of process. • The system identifies processes by a unique process identification value or PID.

Process Descriptor and Task Structure
• The kernel stores the list of processes in a circular doubly linked list called the task list • Each element in the task list is a process descriptor of the type struct task_struct. (<linux/sched.h>). • The process descriptor contains all the information about a specific process. • The task_struct structure is allocated via the slab allocator to provide object reuse

Process Descriptor and Task Structure
• struct thread_info, was created that lives at bottom of kernel stack which in turn points to task_struct • Process descriptor of the currently executing task is accessed via the current macro. • current->pid to retrieve the pid

Task Structure
• • • • • • • • Process attributes Process relationships Process memory space File management Signal management Process credentials Resource limits Scheduling related fields

Process Kernel Stack and Process Descriptor

Process States

Process Creation
• fork() creates a child process that is a copy of the current task • Differs from the parent only in its PID and PPID • exec(), loads a new executable into the address space and begins executing it. • The combination of fork() followed by exec() is similar to the single function most operating systems provide.

Process Creation
• Copy-On-Write • Linux implements fork() via the clone() system call. • The vfork() system call has the same effect as fork(), except that the page table entries of the parent process are not copied.

Process Creation
• Thread clone(CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND, 0); • Process with fork( ) clone(SIGCHLD, 0); • Process with vfork( ) clone(CLONE_VFORK | CLONE_VM | SIGCHLD, 0);

Kernel Threads • Kernel to perform some operations in background • Address space pointer is null • To create a new thread kernel_thread function is used, this in turn uses clone system call

Process Destruction
• process destruction occurs when the process calls the exit() system call

Processor-bound and I/O-bound
• I/O bound process spends much of its time submitting and waiting on I/O requests • Processor-bound process spend much of their time executing code • Scheduling should satisfy both the processes

Process Priority
• Dynamic priority-based scheduling • The Linux kernel implements two separate priority ranges. • The first is the nice value, a number from -20 to +19 with a default of 0. • The second range is the real-time priority. Default range from 0 to 99 • Dynamic Priority is based on static priority and interactivity

Process Scheduling
• • • • Implement fully O(1) scheduling. Provide good interactive performance. The basic data structure in the scheduler is the runqueue. The runqueue is the list of runnable processes on a given processor

Process Scheduling
• Each runqueue contains two priority arrays, the active and the expired array. • Priority arrays are the data structures that provide O(1)

Process Scheduling

Priority Arrays in a Runqueue

Preemption and Context Switching
• User preemption • Kernel preemption

System Calls
• System calls provide a layer between the hardware and user-space processes. • system calls ensure system security and stability • A single common layer between user-space and the rest of the system allows for the virtualized system provided to processes

Application, C Library and Kernel

System Call Handling
• User-space applications cannot execute kernel code directly • The mechanism to signal the kernel is a software interrupt: Incur an exception and then the system will switch to kernel mode and execute the exception handler. • Each system call is assigned a syscall number • The kernel keeps a list of all registered system calls in the system call table, stored in sys_call_table. • On x86, the syscall number is fed to the kernel via the eax register • The system_call() function checks the validity of the given system call number

System Call Handling

System Call Handling
• System call parameters are stored in registers. On x86, the registers ebx, ecx, edx, esi, and edi contain, in order, the first five arguments. • In the unlikely case of six or more arguments, a single register is used to hold a pointer to user-space where all the parameters are stored. • The return value is sent to user-space also via register. On x86, it is written into the eax register.

Verifying the Parameters
• System calls must carefully verify all their parameters to ensure that they are valid and legal. • File I/O syscalls must check whether the file descriptor is valid. Process-related functions must check whether the provided PID is valid. • For writing into user-space, the method copy_to_user() is provided. • For reading from user-space, the method copy_from_user() is provided • The kernel is in process context during the execution of a system call. The current pointer points to the current task, which is the process that issued the syscall.

Thank You