Docsity

Operating System Kernel: Processes, Threads, Queues, and Interrupt Handling - Prof. Michae, Study notes of Computer Science

Toggle sidebar
bg1
CompSci 143A Spring, 2009 1
4. The OS Kernel
4.1 Kernel Definitions and Objects
4.2 Queue Structures
4.3 Threads
4.4 Implementing Processes and Threads
– Process and Thread Descriptors
– Implementing the Operations
4.5 Implementing Synchronization and Communication
Mechanisms
– Requesting and Releasing Resources
– Semaphores and Locks
– Building Monitor Primitives
– Clock and Time Management
– Communications Kernel
4.6 Interrupt Handling
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27

Partial preview of the text

Download Operating System Kernel: Processes, Threads, Queues, and Interrupt Handling - Prof. Michae and more Study notes Computer Science in PDF only on Docsity!

CompSci 143A

Spring, 2009

4. The OS Kernel

4.1 Kernel Definitions and Objects4.2 Queue Structures4.3 Threads4.4 Implementing Processes and Threads

  • Process and Thread Descriptors– Implementing the Operations

4.5 Implementing Synchronization and Communication

Mechanisms– Requesting and Releasing Resources– Semaphores and Locks– Building Monitor Primitives– Clock and Time Management– Communications Kernel 4.6 Interrupt Handling

CompSci 143A

Spring, 2009

Kernel Definitions and Objects

Basic set of objects, primitives,data structures, processes

Rest of OS is built on top of kernel

Kernel defines/provides

mechanisms

to implement various

policies

  • Process and thread management– Interrupt and trap handling– Resource management– Input/output

CompSci 143A

Spring, 2009

Queues

Multi-level queues (priority queues)– Support multiple priority levels– Implemented as

multiple single-level queues

  • Implemented as

heaps

CompSci 143A

Spring, 2009

Priority Queues: Multiple queues

Figure 4-3(a)

CompSci 143A

Spring, 2009

Processes and threads

•^

Process has one or more threads

-^

All threads in a process share:– Memory space– Other resources

-^

Each thread has its own:– CPU state

(registers, program counter)

  • Stack -^

Implemented in user space orkernel space

-^

Threads are efficient, but lackprotection from each other

Figure 4-

CompSci 143A

Spring, 2009

Process status types

Running / Ready / Blocked•

Running

: the process is currently running on a

processor

Ready

: the process is ready to run, waiting for a

processor

Blocked

: the process cannot proceed until it is

granted a particular resource (e.g., a lock, a file, asemaphore, a message, …)

Active / Suspended•

Internal process may

suspend

other processes to

examine or modify their state (e.g., preventdeadlock, detect runaway process, swap theprocess out of memory…)

CompSci 143A

Spring, 2009

Implementing Processes and Threads•^

State

Transition

Diagram

Figure 4-

CompSci 143A

Spring, 2009

Process Operations:

Create

Create(s0, m0, pi, pid){

p = Get_New_PCB(); pid = Get_New_PID();p->ID = pid;

p->CPU_State = s0;

p->Memory = m0;

p->Priority = pi;

p->Status.Type = ’ready_s’;p->Status.List = RL;p->Creation_Tree.Parent = self;p->Creation_Tree.Child = NULL;insert(self-> Creation_Tree.Child, p);insert(RL, p);Scheduler(); }

CompSci 143A

Spring, 2009

Process Operations:

Activate

Activate(pid){

p = Get_PCB(pid);if (p->Status.Type == ’ready_s’){

p->Status.Type = ’ready_a’;Scheduler(); } else p->Status.Type = ’blocked_a’; }

CompSci 143A

Spring, 2009

Process Operations:

Destroy

Destroy(pid){

p

=

Get_PCB(pid);

Kill_Tree(p);

Scheduler();}

Kill_Tree(p){

for

(each

q

in

p->Creation_Tree.Child)

Kill_Tree(q);

if

(p->Status.Type

==

’running

’)

{

cpu

=

p->Processor_ID;

Interrupt(cpu);

} Remove(p->Status.List,

p);

Release_all(p->Memory);Release_all(p->Other_Resources);Close_all(p->Open_Files);Delete_PCB(p);

}

CompSci 143A

Spring, 2009

Specific Instantiations of Resource Request, Release

P

and

V

operations on semaphores

Operations embedded in monitor procedures

Calls to manage clocks, timers, delays, timeouts

Send/receive operations

CompSci 143A

Spring, 2009

Implementing semaphores/locks

Special hardware instruction: test_and_set

Implementing binary semaphores

Implementing general semaphores withbusy waiting

Avoiding the busy wait: Implementinggeneral semaphores with blocking

CompSci 143A

Spring, 2009

Binary Semaphores

•^

Binary semaphore

sb

: only

or

•^

Also known as a

spin lock

or a

spinning lock

(“Spinning”

“Busy Waiting”)

-^

Two atomic operations:

Pb

and

Vb

. Behavior is:

Pb(sb): if (sb==1) sb=0;

else wait until sb becomes 1

Vb(sb): sb=1;

-^

Indivisible implementation of

Pb

and

Vb

using

TS

instruction:

Pb(sb): do (TS(R,sb)) while (!R);/wait loop/Vb(sb): sb=1;

-^

Note:

sb

is shared, but each process has its own

R

CompSci 143A

Spring, 2009

General Semaphores w/ busy wait

P(s) {

Inhibit_Interrupts;Pb(mutex_s);s = s-1;if (s < 0){

Vb(mutex_s);Enable_Interrupts;Pb(delay_s); } Vb(mutex_s);Enable_Interrupts; } V(s) {

Inhibit_Interrupts; Pb(mutex_s);s = s+1;if (s <= 0) Vb(delay_s);else Vb(mutex_s);Enable_Interrupts; }

-^

Inhibit_interrupt

prevents

deadlock due to contextswitching

-^

Two binary semaphores used:^ – delay_s

implements the

actual wait, and may beheld for a long time

  • mutex_s

needed to

implement critical sectionwith multiple CPUs, onlyheld for a few instructions

-^

Note than when

V

executes the

call

Pb(mutex_s)

, the

corresponding

Vb(mutex_s)

,

may be executed by

P