Lesson 12: Kernel-mode Thread Sync (aka: Why I love Gentoo)

Slides:



Advertisements
Similar presentations
Process A process is usually defined as an instance of a running program and consists of two components: A kernel object that the operating system uses.
Advertisements

CSC 360- Instructor: K. Wu Overview of Operating Systems.
Lab 9 CIS 370 Umass Dartmouth.  A pipe is typically used as a one-way communications channel which couples one related process to another.  UNIX deals.
Synchronization and Deadlocks
 Read about Therac-25 at  [  [
Interprocess Communication
 A quantum is the amount of time a thread gets to run before Windows checks.  Length: Windows 2000 / XP: 2 clock intervals Windows Server systems: 12.
Chapter 2: Processes Topics –Processes –Threads –Process Scheduling –Inter Process Communication (IPC) Reference: Operating Systems Design and Implementation.
Avishai Wool lecture Introduction to Systems Programming Lecture 4 Inter-Process / Inter-Thread Communication.
1 CS318 Project #3 Preemptive Kernel. 2 Continuing from Project 2 Project 2 involved: Context Switch Stack Manipulation Saving State Moving between threads,
CS 284a, 8 October 1997 Copyright (c) , John Thornley1 CS 284a Lecture Wednesday, 8 October, 1997.
Concurrency: Deadlock and Starvation Chapter 6. Revision Describe three necessary conditions for deadlock Which condition is the result of the three necessary.
ISP – 3 rd Recitation “The joy of Windows API” Processes Threads Handles Relevant functions A simple code example.
ISP – 7 th Recitation Mid semester!!! Semaphores – reminder Events Code examples.
Introduction to Operating Systems – Windows process and thread management In this lecture we will cover Threads and processes in Windows Thread priority.
CS470 Lab 4 TA Notes. Objective Simulate the activities of a producer and consumer – Page 326 Use thread synchronization to solve the producer-consumer.
Concurrency: Mutual Exclusion, Synchronization, Deadlock, and Starvation in Representative Operating Systems.
CS444/CS544 Operating Systems Synchronization 2/19/2007 Prof. Searleman
ISP – 5 th Recitation Mutexes Code example. Mutex Wikipedia definition: Mutual exclusion (often abbreviated to mutex) algorithms are used in concurrent.
7-1 JMH Associates © 2003, All rights reserved Designing and Developing Reliable, Scaleable Multithreaded Windows Applications Chapter 10 Supplement Advanced.
ISP – 4 th Recitation Times System Errors Threads Waits Code examples.
Race Conditions CS550 Operating Systems. Review So far, we have discussed Processes and Threads and talked about multithreading and MPI processes by example.
3-1 JMH Associates © 2004, All rights reserved Windows Application Development Chapter 9 Synchronization Performance Impact and Guidelines.
Threads CNS What is a thread?  an independent unit of execution within a process  a "lightweight process"  an independent unit of execution within.
Win32 Programming Lesson 13: Thread Pooling (Wow, Java is good for something…)
Object Oriented Analysis & Design SDL Threads. Contents 2  Processes  Thread Concepts  Creating threads  Critical sections  Synchronizing threads.
Win32 Programming Lesson 10: Thread Scheduling and Priorities.
Multi-core Programming Programming with Windows Threads.
计算机系 信息处理实验室 Lecture 4 System Mechanisms (2)
Threads and Thread Synchronization Advanced Windows Programming Series 1.
Win32 Programming Lesson 7: Kernel Objects. Abstract  Many of the concepts we’ll look at today won’t make complete sense until you use them  However,
1 Confidential Enterprise Solutions Group Process and Threads.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Mutual Exclusion.
Windows thread programming
Multithreaded Programming With the Win32 API Andrew Tucker Andrew Tucker Debugger Development Lead March 13, 1998.
Lecture 8 Page 1 CS 111 Online Other Important Synchronization Primitives Semaphores Mutexes Monitors.
IPC Programming. Process Model Processes can be organized into a parent-child hierarchy. Consider the following example code: /* */
Windows Thread Management
Concurrency & Context Switching Process Control Block What's in it and why? How is it used? Who sees it? 5 State Process Model State Labels. Causes of.
Discussion Week 2 TA: Kyle Dewey. Overview Concurrency Process level Thread level MIPS - switch.s Project #1.
Windows CE Overview and Scheduling Presented by Dai Kawano.
Win32 Synchronization CS Spring Overview Kernel Synchronization - Spinlocks Executive Synchronization - Dispatcher Objects Wait Operations.
Mutual Exclusion -- Addendum. Mutual Exclusion in Critical Sections.
Window Threads Chapter 7 Windows Thread Management.
Semaphores Synchronization tool (provided by the OS) that does not require busy waiting. Logically, a semaphore S is an integer variable that, apart from.
Process Synchronization: Semaphores
Background on the need for Synchronization
Andy Wang COP 5611 Advanced Operating Systems
Outline Other synchronization primitives
Other Important Synchronization Primitives
Threads and Thread Synchronization
Threads and Locks.
O.S. Programming Assignment
Critical Section and Critical Resources
Critical Section and Critical Resources
Waiting and Synchronization
Midterm review: closed book multiple choice chapters 1 to 9
Threading And Parallel Programming Constructs
Chapter 05. Multithread.
Lecture 2 Part 2 Process Synchronization
Concurrency: Mutual Exclusion and Process Synchronization
Windows Development Dynadata Copyright, 2014 © DynaData S.A. 1/49.
26.
Process Synchronization
Window Application Development
Process Synchronization
EECE.4810/EECE.5730 Operating Systems
Chapter 3: Process Management
Presentation transcript:

Lesson 12: Kernel-mode Thread Sync (aka: Why I love Gentoo) Win32 Programming Lesson 12: Kernel-mode Thread Sync (aka: Why I love Gentoo)

Where are we? Yay! We can communicate between threads But, we’ve only talked about doing this in User-mode until now Sometimes, you need to go kernel-mode Why should we avoid this?

Limitations Interlocked functions never put the thread in a WAIT state Critical Sections only work within a particular process – don’t work between processes But… kernel-objects take a lot of effort… and I do mean a lot

Signaled/Non-signaled Objects Can use almost any kernel object as a method of thread synchronization Each object can be in a signaled or non-signaled state State change is controlled by what type of object it is Can put a thread in a wait state and wait for a signaling event

Signaling Powerful, as you can make sure you don’t execute until a particular event happens Thread puts itself into the WAIT state and won’t be scheduled until the object is signaled

Wait Functions Voluntarily go into a WAIT state until OBJECT is signaled WaitForSingleObject( HANDLE hObject, DWORD dwMilliseconds); Can use INFINITE E.g. WaitForSingleObject(hProcess, INFINITE) Waits forever until a process exits

Example DWORD dw = WaitForSingleObject(hProcess, 5000); switch (dw) {    case WAIT_OBJECT_0:       // The process terminated.       break;    case WAIT_TIMEOUT:       // The process did not terminate within 5000 milliseconds.       break;    case WAIT_FAILED:       // Bad call to function (invalid handle?)       break; }

Multiple Objects… DWORD WaitForMultipleObjects(    DWORD dwCount,     CONST HANDLE* phObjects,    BOOL fWaitAll,    DWORD dwMilliseconds);  phObject points to an array of Objects Wait until one gets signaled or wait for all

Oops WaitForMultipleObjects works atomically So, if two threads are waiting on the same objects what happens?

Event Kernel Objects Most basic of objects Two kinds: manual (reset all waiting threads) and auto (reset only one waiting thread) HANDLE CreateEvent(    PSECURITY_ATTRIBUTES psa,    BOOL fManualReset,    BOOL fInitialState,    PCTSTR pszName);

Example Look at the Event example in the book… Then let’s look at the next one…

Waitable Timer Kernel Objects Used when you want to pause for a period of time (or regular intervals) Easy to create: HANDLE CreateWaitableTimer(    PSECURITY_ATTRIBUTES psa,    BOOL fManualReset,    PCTSTR pszName);

Setting the Timer… BOOL SetWaitableTimer(    HANDLE hTimer,    const LARGE_INTEGER *pDueTime,    LONG lPeriod,    PTIMERAPCROUTINE pfnCompletionRoutine,      PVOID pvArgToCompletionRoutine,      BOOL fResume );

Semaphore Kernel Objects Used for Resource counting Contains Maximum resource count Current resource count Consider Server which can only handle 5 connections Need to keep track of how many connections are open

Creating a Semaphore Fairly simple: HANDLE CreateSemaphore(    PSECURITY_ATTRIBUTE psa,    LONG lInitialCount,     LONG lMaximumCount,     PCTSTR pszName ); Examples are a little tricky – look this up in the book…

MutEx Lets a thread have mutually-exclusive access to a particular resource HANDLE CreateMutex(    PSECURITY_ATTRIBUTES psa,    BOOL fInitialOwner,    PCTSTR pszName); BOOL ReleaseMutex(HANDLE hMutex);

MutEx v. Critical Section

Other Thread Sync Functions DWORD WaitForInputIdle(    HANDLE hProcess,    DWORD dwMilliseconds); Suspend yourself until hProcess has no input to deal with Can be used (sneakily) to feed messages into another process… *hehe*

SignalObjectAndWait Signal a kernel object and wait on another kernel object DWORD SignalObjectAndWait(    HANDLE hObjectToSignal,     HANDLE hObjectToWaitOn,    DWORD dwMilliseconds,    BOOL fAlertable);

Assignment Create a simple console application which calculates the prime factors of a given 32-bit unsigned int You should prompt for input from the console window and the command line However… you may use a maximum of 3 threads in your program, so you can be working out the answers in parallel Output for 8 should be 2\n2\n2\n And the entry/answer thread must always remain responsive Output must be printed VERTICALLY… if there’s more than 10 lines, you should pause and wait for keyboard input At no point when both threads are waiting to output should you be using CPU time – everything should be in the WAIT state 