Windows thread programming

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.
Correcting Threading Errors with Intel® Thread Checker for Explicit Threads Intel Software College.
1 3. Controlling Robot Car by Wireless Sensor The ultimate objective of the project is to control the car motion using the steering wheel The only connection.
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
1 Lecture-4 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems/Headington.
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
Using the OS. Basic Abstractions Idea Program Result Idea Program Result Program Result … …
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
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.
ISP – 4 th Recitation Times System Errors Threads Waits Code examples.
Threads© Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid Computer Science Department.
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.
1 JMH Associates © 2004, All rights reserved Chapter 1 Getting Started with Win32/64.
Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University.
CSE 380 – Computer Game Programming Render Threading Portal, by Valve,
Multi process-Multi Threaded Amir Averbuch Nezer J. Zaidenberg Amir Averbuch Nezer J. Zaidenberg.
Win32 Programming Lesson 13: Thread Pooling (Wow, Java is good for something…)
Multi-core Programming Programming with Windows Threads.
Thread Synchronization with Semaphores
2.3 InterProcess Communication (IPC)
1-1 © 2004 JMH Associates. All rights reserved. Windows Application Development Chapter 7 Windows Thread Management.
Multithreaded Programming With the Win32 API Andrew Tucker Andrew Tucker Debugger Development Lead March 13, 1998.
CS345 Operating Systems Threads Assignment 3. Process vs. Thread process: an address space with 1 or more threads executing within that address space,
Multithreading GSP-420.
Programming with Windows* Threads Intel Software College.
Writing a Run Time DLL The application loads the DLL using LoadLibrary() or LoadLibraryEx(). The standard search sequence is used by the operating system.
1 Pthread Programming CIS450 Winter 2003 Professor Jinhua Guo.
Windows Thread Management
Synchronizing Threads with Semaphores
Practical Sockets and Threads Derek Weitzel. Windows Threads Concurrent processing Concurrent processing Windows Create Thread Windows Create Thread HANDLE.
Shan Gao Fall 2007 Department of Computer Science Georgia State University.
Fall 2002 CS 325 Class Notes Page 1 Lecture 25 Today –exec() in Unix –CreateProcess in Windows Announcements.
Notes on Processes, Context, Context Switching The following slides contain partially quoted statements from various Wikipedia pages.
Windows Threading Colin Roby Jaewook Kim.
PThread Synchronization. Thread Mechanisms Birrell identifies four mechanisms commonly used in threading systems –Thread creation –Mutual exclusion (mutex)
Multi-dimensional Arrays and other Array Oddities Rudra Dutta CSC Spring 2007, Section 001.
Win32 Synchronization CS Spring Overview Kernel Synchronization - Spinlocks Executive Synchronization - Dispatcher Objects Wait Operations.
Microsoft TechDayshttp:// Сычев Игорь Microsoft Student Partner.
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.
Process Synchronization
SECTION 6 Performance Tuning.
Threads in C Caryl Rahn.
Lecture 7 : Multithread programming
Konstantin Bukin CSE791 – Advanced Windows Programming Summer 2001
Windows Concurrency Concepts and APIs
O.S. Programming Assignment
Linux Fork/Exec Example
EE/CSE 576 HW 1 Notes.
UNIX Fork/Exec Example
Waiting and Synchronization
30.
Embedded Programming in C
Chapter 05. Multithread.
UNIX Fork/Exec Example
EE/CSE 576 HW 1 Notes.
Linux Fork/Exec Example
26.
ECE/CSE 576 HW 1 Notes.
Presentation transcript:

Windows thread programming HOMEWORK 2 Windows thread programming

GOAL Implement a simple image sharpening program on Windows Use multithread programming to make your program faster

IMAGE SHARPENING Asharp = A + k(A – Ablur) k = const. Origin signal Unsharp mask Blurred signal Sharpened signal Origin signal

FILTERING Image origin y mask Image f(x,y) Mask coefficient showing coordinate arrangement m(-1,-1) m(-1,0) m(-1,1) m(0,-1) m(0,0) m(0,1) f(x-1,y-1) f(x-1,y) f(x-1,y+1) f(x,y-1) f(x,y) f(x,y+1) f(x+1,y-1) f(x+1,y) f(x+1,y+1) Pixels of image section under mask m(1,-1) m(1,0) m(1,1)

SMOOTHING FILTER 1  

ORIGINAL IMAGE

SHARPENED IMAGE

IMAGE CONTENT imgWidth, imgHeight Unsigned char Pic_in[x] array Each pixel is represented by three values R G B R G B….. Accessing the ith row, jth col pixel : pic_in[(i*imgWidth+j)*3+color] color = 0,1,2 …………….. R G B Pic_in imgWidth*imgHeight*3

REQUIREMENT We will give you an example code of smoothing and sharpening filter, and you need to modify it. You can only write your own image processing code between the timing function (of course you can declare global variables or local variables anywhere) QueryPerformanceFrequency(&ticksPerSecond); QueryPerformanceCounter(&start_tick); //your code… QueryPerformanceCounter(&end_tick); elapsed = ((double) (end_tick.QuadPart - start_tick.QuadPart) / ticksPerSecond.QuadPart);

REQUIREMENT You can only use our library to read the input image (24bit uncompressed BMP file) The format of the mask file: 1st line : FILTER SIZE (3x3 5x5 …) 2nd line : filtering times for smoothing 3rd line: mask number start from upper left (but all of them will be 1)

REQUIREMENT You must use more than 1 thread You can get basic score if your program is faster than the example The man who can write the fastest program, will get the highest score in this project! The demo computer spec: Intel Core i5, 4GB ram, windows7 32bit ……

REQUIREMENT Test the program in different number of threads Make a line chart of “Elapsed Time” and “Number of Threads” in your report Upload your code and report to the FTP

CREATE THREAD AND SYNCHRONIZATION FUNCTIONS IN WINDOWS Windows thread programming

CREATE THREAD AND SYNCHRONIZATION FUNCTIONS IN WINDOWS #include <windows.h> #include <process.h>

CREATE THREAD IN WINDOWS HANDLE WINAPI CreateThread( __in_opt LPSECURITY_ATTRIBUTES lpThreadAttributes, __in SIZE_T dwStackSize, __in LPTHREAD_START_ROUTINE lpStartAddress, __in_opt LPVOID lpParameter, __in DWORD dwCreationFlags, __out_opt LPDWORD lpThreadId ); EX: to create a thread to execute function ABC CreateThread( NULL, 0, (LPTHREAD_START_ROUTINE)ABC, NULL, 0, &ThreadID );

SYNCHRONIZATION FUNCTIONS IN WINDOWS HANDLE WINAPI CreateSemaphore ( __in_opt LPSECURITY_ATTRIBUTES lpSemaphoreAttributes, __in LONG lInitialCount, __in LONG lMaximumCount, __in_opt LPCTSTR lpName );  EX:The max value for the semaphore Sem is 5, and its initial value is 0 HANDLE Sem; Sem = CreateSemaphore( NULL, 0, 5, NULL );

SYNCHRONIZATION FUNCTIONS IN WINDOWS DWORD WINAPI WaitForSingleObject ( __in HANDLE hHandle, __in DWORD dwMilliseconds );  EX:to wait for a semaphore WaitForSingleObject( Sem, INFINITE );

SYNCHRONIZATION FUNCTIONS IN WINDOWS DWORD WINAPI WaitForMultipleObject ( __in DWORD nCount, __in const HANDLE *lpHandles, __in BOOL bWaitAll, __in DWORD dwMilliseconds );  EX:to wait for two threads HANDLE Thread[2]; WaitForMultipleObject( 2, Thread, TRUE, INFINITE );

SYNCHRONIZATION FUNCTIONS IN WINDOWS BOOL WINAPI ReleaseSemaphore ( __in HANDLE hSemaphore, __in LONG lReleaseCount, __out_opt LPLONG lpPreviousCount ); EX:to signal a semaphore ReleaseSemaphore( Sem, 1, NULL ); WaitForSingleObject( Sem, INFINITE );

EX:HW1 IN WINDOWS //global variables int top, down; int buf[5]; HANDLE full, empty; int main(void) { top = down = 0; full = CreateSemaphore( NULL, 0, 5, NULL ); empty = CreateSemaphore( NULL, 5, 5, NULL ); int j, *tid_arg; srand((int)time(0)); printf("start\n"); tid_arg = (int *) malloc(sizeof(int)); *tid_arg = 1; DWORD tid; HANDLE Thread[2]; Thread[0] = CreateThread( NULL, 0, (LPTHREAD_START_ROUTINE)producer, (void*)tid_arg, 0, &tid ); Thread[1] = CreateThread( NULL, 0, (LPTHREAD_START_ROUTINE)consumer, (void*)tid_arg, 0, &tid ); WaitForMultipleObjects(2, Thread, TRUE, INFINITE); printf("finish\n"); system("pause"); return 0; }

EX:HW1 IN WINDOWS void * producer(void *arg) { printf("p_in\n"); int i, input, in_data; input = *((int *) arg); for (i=0; i<12; i++) { WaitForSingleObject( empty, INFINITE ); Sleep(rand()%5); in_data = rand()%256; printf(">>p(%d) =(%d),buf[%d]\n", i, in_data, top); buf[top] = in_data; top = (top+1)%5; ReleaseSemaphore(full, 1, NULL); } return 0;

EX:HW1 IN WINDOWS void * consumer(void *arg) { printf("c_in\n"); int i, input, out_data; input = *((int *) arg); for (i=0;i<12;i++){ WaitForSingleObject( full, INFINITE ); Sleep(rand()%10); out_data = buf[down]; printf(">>c(%d) =(%d),buf[%d]\n", i, out_data, down); down = (down+1)%5; ReleaseSemaphore(empty, 1, NULL); } return 0;