Docsity

Lecture Slides on Processes - Operating Systems | CS 326, Study notes of Operating Systems

Partial preview of the text

Download Lecture Slides on Processes - Operating Systems | CS 326 and more Study notes Operating Systems in PDF only on Docsity!

usf CSCS

UNIVERSITY of SAN FRANCISCO department of computer science

Processes

CS 326 Operating Systems Greg Benson

Making Processes Work

We need to understand how an OS kernel implements processes and threads

We will look at the following issues:

How do the kernel and processes execute

How to implement processes and threads

Scheduling processes and threads

Synchronizing processes and threads

Process Execution at 10,000 ft

Time A B Process

Process-Kernel Execution

At all times a CPU is executing some code:

It executes user code in processes/threads

It executes kernel code

Think about this:

The CPU PC (program counter, instruction pointers) is always pointing to an instruction

In order to switch from one process to another we must execute some kernel code

Process-Kernel Switching

‣ How and when do we transfer control from user processes to the kernel? ‣ When a process makes a system call (e.g., write(...)) ‣ In response to a hardware interrupt ‣ Timer, keyboard, mouse, network, hard disk, etc. ‣ Hardware exceptions such as divide by zero and memory address fault ‣ Anytime control is transferred to the kernel a process switch can occur ‣ This is called a^ mode switch^ as we are switched from user mode to kernel mode ‣ Note: a process cannot directly switch to another process; control must be transferred to the kernel first

Process Execution at 100 feet

Time A B Process Kernel timer syscall network

Quantum Length Issues

How to pick the quantum length?

What is good and bad about a short quantum?

What is good and bad about a long quantum?

In practice, I/O bound processes do not use an entire quantum, they just run frequently

Although Linux has a default quantum of 100ms, the system can give lower or higher values depending on the nature of the process

Process States

A process can be in one of three states:

running - executing instructions

ready - waiting to execute

blocked - waiting for something Running Ready Blocked

Implementing Processes

Recall that a process consists of

Memory for code, data, and stack in user space

Kernel state and stack

The kernel represents each process as a C struct called the process control block (PCB)

State, stack pointer, register values

Process id, CPU time used, memory map

Open file descriptors

Kernel Process Lists

The kernel maintains lists of processes

The ready list consists of all ready, but not running processes

Blocked processes are put onto special lists for particular event, e.g. a timer list

A big part of kernel process management is maintaining the kernel process lists

Asynchronous Context Switch

save current state

intr_entry: save registers (push) setup kernel env push stack pointer call intr_handler intr_exit: restore registers (pop) iret

possibly return to a

different thread

Context vs Mode Switch

Not all mode switches cause a context switch

A mode switch allows the kernel to run:

Handle interrupts (e.g., incoming data)

Check to see if scheduling is required

A context switch (done in kernel):

Changes to new process/thread context

More on Threads

Processes

n Threads + resources (mem, files, etc.)

In modern OSes, n >= 1

Linux, Mac OS X, Solaris, Windows etc.

In Pintos, n = 1, a process is a thread (as in traditional UNIX)

However, we could change this if we wanted

Multiprocessors

If a computer has more than one processor or “core” in a multi-core system, the OS needs to schedule threads onto processors

Not too much different than single processor schedule, except

Need to deal with multiple threads in the kernel

Need synchronization (more later)

Need a more complex scheduling policy