The OS kernel: Implementing processes and Threads Kernel Definitions and Objects Queue Structures Threads Implementing Processes and Threads Implementing.

Slides:



Advertisements
Similar presentations
Operating Systems Semaphores II
Advertisements

More on Processes Chapter 3. Process image _the physical representation of a process in the OS _an address space consisting of code, data and stack segments.
CAS3SH3 Midterm Review. The midterm 50 min, Friday, Feb 27 th Materials through CPU scheduling closed book, closed note Types of questions: True & False,
Project: Processes and Resource Management Textbook: pages Lubomir Bic.
Real-Time Library: RTX
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
 2004 Deitel & Associates, Inc. All rights reserved. 1 Chapter 3 – Process Concepts Outline 3.1 Introduction 3.1.1Definition of Process 3.2Process States:
Informationsteknologi Wednesday, September 26, 2007 Computer Systems/Operating Systems - Class 91 Today’s class Mutual exclusion and synchronization 
Chapter 5 Processes and Threads Copyright © 2008.
6/9/2015B.Ramamurthy1 Process Description and Control B.Ramamurthy.
Process Description and Control
Figure 2.8 Compiler phases Compiling. Figure 2.9 Object module Linking.
Concurrency: Deadlock and Starvation Chapter 6. Revision Describe three necessary conditions for deadlock Which condition is the result of the three necessary.
Page 1 Processes and Threads Chapter Processes 2.2 Threads 2.3 Interprocess communication 2.4 Classical IPC problems 2.5 Scheduling.
Introduction to Operating Systems – Windows process and thread management In this lecture we will cover Threads and processes in Windows Thread priority.
3.5 Interprocess Communication Many operating systems provide mechanisms for interprocess communication (IPC) –Processes must communicate with one another.
Inter Process Communication:  It is an essential aspect of process management. By allowing processes to communicate with each other: 1.We can synchronize.
Concurrency: Mutual Exclusion, Synchronization, Deadlock, and Starvation in Representative Operating Systems.
3.5 Interprocess Communication
Processes 1 CS502 Spring 2006 Processes Week 2 – CS 502.
Ceng Operating Systems Chapter 2.1 : Processes Process concept Process scheduling Interprocess communication Deadlocks Threads.
Chapter 11 Operating Systems
1 Concurrency: Deadlock and Starvation Chapter 6.
Chapter 6 Implementing Processes, Threads, and Resources.
1 Process Description and Control Chapter 3 = Why process? = What is a process? = How to represent processes? = How to control processes?
Race Conditions CS550 Operating Systems. Review So far, we have discussed Processes and Threads and talked about multithreading and MPI processes by example.
1/26/2007CSCI 315 Operating Systems Design1 Processes Notice: The slides for this lecture have been largely based on those accompanying the textbook Operating.
Operating Systems1 4. The OS Kernel 4.1 Kernel Definitions and Objects 4.2 Queue Structures 4.3 Threads 4.4 Implementing Processes and Threads –Process.
Process Description and Control Chapter 3. Major Requirements of an OS Interleave the execution of several processes to maximize processor utilization.
1 The OS Kernel Kernel Definitions and Objects Queue Structures Threads Implementing Processes and Threads –Process and Thread Descriptors –Implementing.
Chapter 3: Processes. 3.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Chapter 3: Processes Process Concept Process Scheduling Operations.
Chapter 3: Processes Process Concept Process Scheduling Operations on Processes Interprocess Communication Examples of IPC Systems Communication in Client-Server.
Introduction to Processes CS Intoduction to Operating Systems.
Object Oriented Analysis & Design SDL Threads. Contents 2  Processes  Thread Concepts  Creating threads  Critical sections  Synchronizing threads.
CompSci 143ASpringr, The OS Kernel 4.1 Kernel Definitions and Objects 4.2 Queue Structures 4.3 Threads 4.4 Implementing Processes and Threads.
ICS 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.
Operating Systems Principles Process Management and Coordination Lecture 4: The Operating System Kernel: Implementing Processes and Threads 主講人:虞台文.
Processes and Threads CS550 Operating Systems. Processes and Threads These exist only at execution time They have fast state changes -> in memory and.
Lecture 3 Process Concepts. What is a Process? A process is the dynamic execution context of an executing program. Several processes may run concurrently,
CE Operating Systems Lecture 11 Windows – Object manager and process management.
1 Chapter 2.1 : Processes Process concept Process concept Process scheduling Process scheduling Interprocess communication Interprocess communication Threads.
1 Announcements The fixing the bug part of Lab 4’s assignment 2 is now considered extra credit. Comments for the code should be on the parts you wrote.
REVIEW OF COMMONLY USED DATA STRUCTURES IN OS. NEEDS FOR EFFICIENT DATA STRUCTURE Storage complexity & Computation complexity matter Consider the problem.
ITEC 502 컴퓨터 시스템 및 실습 Chapter 2-1: Process Mi-Jung Choi DPNM Lab. Dept. of CSE, POSTECH.
CSE 451: Operating Systems Section 5 Midterm review.
Chapter 2 Processes and Threads Introduction 2.2 Processes A Process is the execution of a Program More specifically… – A process is a program.
1 Computer Systems II Introduction to Processes. 2 First Two Major Computer System Evolution Steps Led to the idea of multiprogramming (multiple concurrent.
Processes, Threads, and Process States. Programs and Processes  Program: an executable file (before/after compilation)  Process: an instance of a program.
Process Description and Control Chapter 3. Source Modified slides from Missouri U. of Science and Tech.
Chapter 2 Process Management. 2 Objectives After finish this chapter, you will understand: the concept of a process. the process life cycle. process states.
Processes. Process Concept Process Scheduling Operations on Processes Interprocess Communication Communication in Client-Server Systems.
1 Process Description and Control Chapter 3. 2 Process A program in execution An instance of a program running on a computer The entity that can be assigned.
Mutual Exclusion -- Addendum. Mutual Exclusion in Critical Sections.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Mutual Exclusion Mutexes, Semaphores.
Advanced Operating Systems CS6025 Spring 2016 Processes and Threads (Chapter 2)
Process Synchronization. Concurrency Definition: Two or more processes execute concurrently when they execute different activities on different devices.
Process concept.
Process Management Process Concept Why only the global variables?
Chapter 3: Process Concept
OPERATING SYSTEMS CS3502 Fall 2017
Chapter 3 – Process Concepts
Processes Overview: Process Concept Process Scheduling
Semaphore Originally called P() and V() wait (S) { while S <= 0
Process Description and Control
Lecture 2 Part 2 Process Synchronization
Chapter 2 Processes and Threads 2.1 Processes 2.2 Threads
CSE 153 Design of Operating Systems Winter 19
Chapter 3: Process Concept
Chapter 3: Process Management
Presentation transcript:

The OS kernel: Implementing processes and Threads Kernel Definitions and Objects Queue Structures Threads Implementing Processes and Threads Implementing Synchronization and Communication Mechanisms Interrupt Handling

Kernel Definitions and Objects Process and thread management Interrupt and trap handling Resource management Input and output OS kernel: a basic set of objects, primitive operations, data structures, and processes from which the remainder of the system may be constructed

Kernel psps p1p1 pjpj q1q1 pnpn qmqm …… … Interactions with kernel objects A Process Creation Hierarchy

Queue structures Queues in OSs --queue for PCB --queues for hardware managers --queues for synchronization --queues for shared software components Implementations of Queues --single-level queues --priority queues

Implementations of queues Types of queues: --FIFO --priority based Elements of queues Operations related to queues --insert( queue Q, element x) --remove( queue Q, element x) --empty( queue Q) Header or descriptor of queues

Single-Level Queues Circular array Linked list Queue pointer front rear... Occupied portion... header Queue pointer front rear... header null...

Priority queues Fixed-number priorities Queue pointer front rear i N-1...

Binary heap

Threads The normal implementation of processes results in much runtime overhead. To alleviate this problem, multiple “lightweight” scheduling units is implemented within a process. These units share the same resources as there host process

Create a new thread; Initiate or make a thread ready; Destroy or terminate a thread; Delay or terminate a thread Delay or put a thread to sleep for a given amount of time; Synchronize threads through semaphores, events or condition variables; Perform lower-level operations, such as blocking, suspending, or scheduling a thread Operations on threads:

Implementing processes and threads Process and thread descriptors Implementing operations on processes Operations on threads

PCB Identification State vector Processors Status information Creation tree other information

Structure of process descriptor PCB ID Cpu_state Processor_ID memory Open_files Other_resources list type priority parent child... processors resources State vector status Creation_tree Other information

State vector

Status information running Ready_a Ready_s Blocked_a Blocked_s

Implementing operations on processes Create Create(so,mo,pi,pid){ p=Get_New_PCB(); pid=Get_New_PID(); p->ID=pid; p->CPU_State=s0; p->Memory=m0; p->Priority=pi; p->State.Type=“ready_s”; p->Creation_tree.Parent=self; p->Creation_Tree.child=NULL; insert(self->Creation_tree.child,p); insert(RL,p); Scheduler(); }

Suspend suspend(pid){ p=Get_PCB(pid); s=p->Status.Type; if((s==“blocked_a”)||(s==“blocked_s”)) p->status.Type=“block_s”; else p->status.Type=“ready_s”; if(s==“running”){ cpu=p->Processor_ID; p->CPU_State=Interrupt(cpu); Scheduler(); }

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”; }

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); }

Implementing synchronization and communication mechanisms Semaphores and locks Monitor primitives Clock and time management Communication

General version of the Request/Release Request(res){ if(Free(res)) Allocate(res,self); else{ Block(self,res); Scheduler(); } Release(res){ Deallocate(res,self); if(Process_Blocked_on(res,pr){ Allocate(res,pr); Unblock(pr,res); Scheduler(); }

Semaphores and locks BT : carry appointed bit to CF; BTC : carry appointed bit to CF, and reverse it; BTR : carry appointed bit to CF, and change it to 0; BTS : carry appointed bit to CF, and change it to 1; Bit Test Instruction :

Spin Locks on Binary Semaphores Pb(sb): do TS(R,sb); while(!R);/*wait loop*/ Vb(sb): sb=1;

General Semaphores with spin locks P p(s){ s=s-1; if(s<0){ Pb(delay_s; } V v(s){ s=s+1; if(s<=0) Vb(delay_s); else } Pb(mutex_s); Vb(mutex_s); Pb(mutex_s); Vb(mutex_s);

Avoiding the Busy-Wait P(s){ Inhibit_Interrupts; Pb(mutex_s); s=s-1; if(s<0){ Block(self, Ls); Vb(mutex_s); Enable_Interrupts; Scheduler(); } else{ Vb(mutex_s); Enable_Interrupts; } V(s){ Inhibit_Interrupts; Pb(mutex_s); s=s+1; if(s<=0){ Unblock(q, Ls); Vb(mutex_s); Enable_Interrupts; Scheduler(); } else{ Vb(mutex_s); Enable_Interrupts; }

Primitive and atomic operation Priority Interrupt request Switching tasks on single CPU Switching tasks on multiple CPU

struct semaphore { atomic_t count; int sleepers; wait_queue_head_t wait; #if WAITQUEUE_DEBUG long __magic; #endif }; static inline void down(struct semaphore * sem) { #if WAITQUEUE_DEBUG CHECK_MAGIC(sem->__magic); #endif __asm__ __volatile__( "# atomic down operation\n\t" LOCK "decl %0\n\t" /* -- sem->count */ "js 2f\n" "1:\n" LOCK_SECTION_START("") "2:\tcall __down_failed\n\t" "jmp 1b\n" LOCK_SECTION_END :"=m" (sem->count) :"c" (sem) :"memory"); }

asm( ".text\n" ".align 4\n" ".globl __down_failed\n" "__down_failed:\n\t" #if defined(CONFIG_FRAME_POINTER) "pushl %ebp\n\t" "movl %esp,%ebp\n\t" #endif "pushl %eax\n\t" "pushl %edx\n\t" "pushl %ecx\n\t" "call __down\n\t" "popl %ecx\n\t" "popl %edx\n\t" "popl %eax\n\t" #if defined(CONFIG_FRAME_POINTER) "movl %ebp,%esp\n\t" "popl %ebp\n\t" #endif "ret" );

void __down(struct semaphore * sem) { struct task_struct *tsk = current; DECLARE_WAITQUEUE(wait, tsk); tsk->state = TASK_UNINTERRUPTIBLE; add_wait_queue_exclusive(&sem->wait, &wait); spin_lock_irq(&semaphore_lock); sem->sleepers++; for (;;) { int sleepers = sem->sleepers; /* * Add "everybody else" into it. They aren't * playing, because we own the spinlock. */ if (!atomic_add_negative(sleepers - 1, &sem->count)) { sem->sleepers = 0; break; } sem->sleepers = 1;/* us - see -1 above */ spin_unlock_irq(&semaphore_lock); schedule(); tsk->state = TASK_UNINTERRUPTIBLE; spin_lock_irq(&semaphore_lock); } spin_unlock_irq(&semaphore_lock); remove_wait_queue(&sem->wait, &wait); tsk->state = TASK_RUNNING; wake_up(&sem->wait); }

static inline void spin_lock(spinlock_t *lock) { #if SPINLOCK_DEBUG __label__ here; here: if (lock->magic != SPINLOCK_MAGIC) { printk("eip: %p\n", &&here); BUG(); } #endif __asm__ __volatile__( spin_lock_string :"=m" (lock->lock) : : "memory"); } #define spin_lock_string \ "\n1:\t" \ "lock ; decb %0\n\t" \ "js 2f\n" \ LOCK_SECTION_START("") \ "2:\t" \ "cmpb $0,%0\n\t" \ "rep;nop\n\t" \ "jle 2b\n\t" \ "jmp 1b\n" \ LOCK_SECTION_END

#define spin_lock_irq(lock)do { local_irq_disable(); spin_lock(lock); } while (0)

Monitor primitives Body of each procedure: p(mutex); procedure_body; if(urgentcnt) V(urgent); else V(mutex); C.wait: condcnt_c=condcnt_c+1; if(urgentcnt) V(urgent); else V(mutex); P(condsem_c) condcnt_c=condcnt-1; C.signal: if(condcnt_c){ urgentcnt=urgentcnt+1; V(condsem_c); P(urgent); urgentcnt=urgentcnt-1; }

Clock and Time Management Wall clock timer -- Update_clock -- Get_Time -- SetClock(tnew) Countdown Timers -- Set_Timer(tdel) -- Delay(tdel) Delay(tdel){ Set_timer(tdel); P(delsem); } -- TimeOut() TimeOut( ){ V(delsem): }

Implementing logical timers Logical timer -- tn=Create_LTimer(); -- Destroy_LTimer(tn); -- SetLTimer(tn,tdel);

Using a Priority Queue with Absolute Wakeup Times Hardware timers Wall-clockcountdown Timer queue TQ a p1115p2135p3140p4150 b p1115p2135p3138p4140 TQ p5150

Using a Priority Queue with Time Differences Hardware timers countdown 12 Timer queue TQ a p115p220p35p410 b p115p220p33p42 TQ p510

Communication primitives Generic form of message-passing primitives -- send(p,m) -- receive(q, m) Two issues must be resolved -- how does the sender know that sbuf has been copied and may be reused? -- how does the system know that the contents of rbuf are no longer needed by the receiver and may be overwritten? Solutions -- blocking -- nonblocking (system buffers)

Interrupt Handling Interrupt concept -- an event occurring at an unpredictable time that forces a transfer of control out of the normal processing sequence of a computer -- the purpose of interrupt handling is to remove the notion of asynchronous events from higher levels of the kernel, the OS, and applications Common operations on interrupts -- enable -- disable -- inhibit

More uniform abstract model of interrupt handling Hardware device interrupt monitor Fn() IH() P Fn()... Init c.wait... c.signal OS call c