26.

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


Lesson 12: Kernel-mode Thread Sync (aka: Why I love Gentoo)
Multithread API’s Adam Piotrowski Grzegorz Jabłoński Lecture IV.
Multi-core Programming Programming with Posix Threads.
Correcting Threading Errors with Intel® Thread Checker for Explicit Threads Intel Software College.
Project 2 教學. Synchronization Functions in Windows HANDLE WINAPI CreateSemaphore ( __in_opt LPSECURITY_ATTRIBUTES lpSemaphoreAttributes, __in LONG lInitialCount,
CS444/CS544 Operating Systems Synchronization 2/28/2006 Prof. Searleman
5.6 Semaphores Semaphores –Software construct that can be used to enforce mutual exclusion –Contains a protected variable Can be accessed only via wait.
CS 284a, 8 October 1997 Copyright (c) , John Thornley1 CS 284a Lecture Wednesday, 8 October, 1997.
Recitation summary What you should know for the exam
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.
CS470 Lab 4 TA Notes. Objective Simulate the activities of a producer and consumer – Page 326 Use thread synchronization to solve the producer-consumer.
CS444/CS544 Operating Systems
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.
ISP – 6 th Recitation What’s void? Pointer to pointer/Array of pointers/Two dimensional array “Random” numbers Semaphores Code examples.
7-1 JMH Associates © 2003, All rights reserved Designing and Developing Reliable, Scaleable Multithreaded Windows Applications Chapter 10 Supplement Advanced.
SMP threads an Introduction to Posix Threads. Technical Definition 1.Independent stream of instructions that can be scheduled to run by an operating system.
ISP – 4 th Recitation Times System Errors Threads Waits Code examples.
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.
Programming Models using Windows* Threads Intel Software College.
Win32 Programming Lesson 9: Jobs & Thread Basics.
MultiThreaded Applications. What is Multithreaded Programming? Having your software appear to perform multiple tasks in parallel –Individual paths of.
Multi-core Programming Programming with Windows Threads.
1-1 © 2004 JMH Associates. All rights reserved. Windows Application Development Chapter 7 Windows Thread Management.
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.
Windows thread programming
Multithreaded Programming With the Win32 API Andrew Tucker Andrew Tucker Debugger Development Lead March 13, 1998.
ITEC 502 컴퓨터 시스템 및 실습 Chapter 2-1: Process Mi-Jung Choi DPNM Lab. Dept. of CSE, POSTECH.
Multithreading GSP-420.
Programming with Windows* Threads Intel Software College.
Windows Thread Management
Practical Sockets and Threads Derek Weitzel. Windows Threads Concurrent processing Concurrent processing Windows Create Thread Windows Create Thread HANDLE.
Fall 2002 CS 325 Class Notes Page 1 Lecture 25 Today –exec() in Unix –CreateProcess in Windows Announcements.
Windows CE Overview and Scheduling Presented by Dai Kawano.
Windows Threading Colin Roby Jaewook Kim.
Win32 Synchronization CS Spring Overview Kernel Synchronization - Spinlocks Executive Synchronization - Dispatcher Objects Wait Operations.
3/17/2016cse synchronization-p2 © Perkins, DW Johnson and University of Washington1 Synchronization Part 2 CSE 410, Spring 2008 Computer.
Threads and Thread Synchronization. Introduction In windows the basic unit of execution is the thread. It is the smallest schedulable unit of execution.
Lecture Lecture 25 Review of Last Lecture DLL’s DLL’s Processes Processes Threads Threads Memory Management Memory Management.
Window Threads Chapter 7 Windows Thread Management.
Chapter 4 – Thread Concepts
Chapter 3: Windows7 Part 5.
Win32 Threads and Thread Synchronization
Windows and C++11 Threads and Locks
Process Synchronization
Chapter 4 – Thread Concepts
Lecture 7 : Multithread programming
Konstantin Bukin CSE791 – Advanced Windows Programming Summer 2001
Threads and Thread Synchronization
Windows Concurrency Concepts and APIs
Tarek Abdelzaher Vikram Adve Marco Caccamo
Threads and Locks.
O.S. Programming Assignment
Chapter 3: Windows7 Part 5.
Waiting and Synchronization
30.
Chapter 05. Multithread.
Windows Development Dynadata Copyright, 2014 © DynaData S.A. 1/49.
Thread Synchronization including Mutual Exclusion
CSE 153 Design of Operating Systems Winter 19
Following Malware Execution in IDA
Window Application Development
Presentation transcript:

26

Review of Last Lecture DLLs and threads _beginthread() _endthread() CreateThread() Thread procedure: THREADPROC ExitThread() Supend/ResumeThread(), Sleep() Thread object and thread handles

Some OS concepts Co-operative vs. Pre-emptive multitasking CreateThread(): Thread object remain in the Win32 system until CloseHandle() is called for all handles to a thread.

CreateThread() enum Shape { RECTANGLE, ELLIPSE }; DWORD WINAPI drawThread(LPVOID shape); SYSTEMTIME st; HANDLE CreateThread( LPSECURITY_ATTRIBUTES lpThreadAttributes, // SD DWORD dwStackSize, // initial stack size LPTHREAD_START_ROUTINE lpStartAddress, // thread function LPVOID lpParameter, // thread argument DWORD dwCreationFlags, // creation option LPDWORD lpThreadId // thread identifier );

Threads example hThread1 = CreateThread(NULL, 0, drawThread, (LPVOID)RECTANGLE, CREATE_SUSPENDED, &dwThread1); hThread2 = CreateThread(NULL, 0, drawThread, (LPVOID)ELLIPSE, CREATE_SUSPENDED, &dwThread2); hDC = GetDC(hWnd); hBrushRectangle=CreateSolidBrush(RGB(170,220,160)); hBrushEllipse = CreateHatchBrush(HS_BDIAGONAL,RGB(175,180,225)); InitializeCriticalSection(&cs); srand( (unsigned)time(NULL) ); ResumeThread(hThread2); ResumeThread(hThread1);

Threads example DWORD WINAPI drawThread(LPVOID type) { int i; if((enum Shape)type == RECTANGLE) for(i=0; i<10000; ++i) EnterCriticalSection(&cs); SelectObject(hDC, hBrushRectangle); Rectangle(hDC, 50, 1, rand()%300, rand()%100); GetLocalTime(&st); LeaveCriticalSection(&cs); Sleep(10); }

Threads Synchronization The problem: GDI handle is shared among threads. Only one thread must draw using this handle, at one time SYSTEMTIME The solution: Critical Section Kernel Object

Critical Section Object InitializeCriticalSection EnterCriticalSection LeaveCriticalSection DeleteCriticalSection

Synchronisation Objects Other Kernel Objects basics Wait functions: Signalled and non-signalled states Mutex Object: Mutual Exclusion Event Object: Similar to a flag Semaphore Object: A counter based object

The Mutex Object Description DWORD WaitForSingleObject( HANDLE CreateMutex( LPSECURITY_ATTRIBUTES lpMutexAttributes, // SD BOOL bInitialOwner, // initial owner LPCTSTR lpName // object name ); DWORD WaitForSingleObject( HANDLE hHandle, // handle to object DWORD dwMilliseconds // time-out interval INFINITE, WAIT_TIMEOUT, WAIT_OBJECT_0

The Mutex Object When the Mutex object is no longer needed, call BOOL ReleaseMutex( HANDLE hMutex // handle to mutex ); When the Mutex object is no longer needed, call CloseHandle( handle );

The Mutex Object Named and un-named Mutex objects Opening a handle to a Mutex When we try to create Mutex with some name and a Mutex with that name already exists, GetLastError() returns ERROR_ALREADY_EXISTS

Threads example with Mutex hThread1 = CreateThread(NULL, 0, drawThread, (LPVOID)RECTANGLE, CREATE_SUSPENDED, &dwThread1); hThread2 = .. .. .. hBrushRectangle = CreateSolidBrush(RGB(170,220,160)); hBrushEllipse=CreateHatchBrush(HS_BDIAGONAL,RGB(175,180,225)); hMutex=CreateMutex(NULL, 0, NULL); srand( (unsigned)time(NULL) ); ResumeThread(hThread2);

Threads example for(i=0; i<10000; ++i) { Switch(WaitForSingleObject(hMutex, INFINITE)) case WAIT_OBJECT_0: SelectObject(hDC, hBrushRectangle); Rectangle(hDC, 50, 1, rand()%300, rand()%100); GetLocalTime(&st); ReleaseMutex(hMutex); Sleep(10); };

Synchronisation Objects Critical Section can’t be accessed outside a process while named Mutex objects can be! Problem: Detecting whether another instance of the same Win32 application is running In Win32 2nd parameter, hPrevInstance passed to WinMain() is always NULL

Multiple instance of the same application Create a Named Mutex with some unique name using CreateMutex() If there is error creating Mutex object, call GetLastError() If GetLastError() returns ERROR_ALREADY_EXISTS, another instance of the same application is already running

Event Object Description: CreateEvent() Manual Reset and Auto-reset events SetEvent(), ResetEvent() PulseEvent()

Event Object Description: Used as a flag to mark the occurrence of an event HANDLE CreateEvent( LPSECURITY_ATTRIBUTES lpEventAttributes, // Security Descriptor BOOL bManualReset, // reset type BOOL bInitialState, // initial state LPCTSTR lpName // object name );

Waiting for Multiple Objects DWORD WaitForMultipleObjects( DWORD nCount, // number of handles in array CONST HANDLE *lpHandles, // object-handle array BOOL fWaitAll, // wait option DWORD dwMilliseconds // time-out interval ); Returns: WAIT_OBJECT_0 to (WAIT_OBJECT_0 + nCount – 1)

Semaphore Object Description: Limiting the maximum number of threads in a system. Utilisation of a resource HANDLE CreateSemaphore( LPSECURITY_ATTRIBUTES lpSemaphoreAttributes, // SD LONG lInitialCount, // initial count LONG lMaximumCount, // maximum count LPCTSTR lpName // object name );

Semaphore Object lInitialCount Specifies an initial count for the semaphore object. This value must be greater than or equal to zero and less than or equal to lMaximumCount. The state of a semaphore is signaled when its count is greater than zero and nonsignaled when it is zero. The count is decreased by one whenever a wait function releases a thread that was waiting for the semaphore. The count is increased by a specified amount by calling the ReleaseSemaphore() function.

User Interface threads DWORD MsgWaitForMultipleObjects( DWORD nCount, // number of handles in array CONST HANDLE pHandles, // object-handle array BOOL fWaitAll, // wait option DWORD dwMilliseconds, // time-out interval DWORD dwWakeMask // input-event type );

Thread Local Storage Allocates a TLS index declspec (thread) int global; declspec (thread) static inside_fuction; DWORD TlsAlloc(VOID); Allocates a TLS index BOOL TlsSetValue( DWORD dwTlsIndex, // TLS index LPVOID lpTlsValue // value to store ); Stores a value in the calling thread's thread local storage (TLS) slot for the specified TLS index. Each thread of a process has its own slot for each TLS index.

Thread Local Storage LPVOID TlsGetValue( DWORD dwTlsIndex // TLS index ); BOOL TlsFree(

Advantage and Disadvantage of DLLs Advantages Common services can be grouped ISVs (Independent Software Vendors) can ship DLLs independently Lesser code to be written by programmers Disadvantages DLLs are slower than statically linked code Versioning may be cumbersome C-runtime routines are statically linked with every EXE and DLL. When a single process loads multiple DLLs, statically linked C-runtime gets duplicated.