ISP – 3 rd Recitation “The joy of Windows API” Processes Threads Handles Relevant functions A simple code example.

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)
Win32 Programming Lesson 8: Processes. Where are we?  We’re starting to have some foundational understanding of Windows  But really, we don’t know how.
1 JMH Associates © 2004, All rights reserved Chapter 6 Process Management.
ISP – 7 th Recitation Mid semester!!! Semaphores – reminder Events Code examples.
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.
ISP – 4 th Recitation Times System Errors Threads Waits Code examples.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 3: Processes.
Threads CNS What is a thread?  an independent unit of execution within a process  a "lightweight process"  an independent unit of execution within.
© 2004, D. J. Foreman 2-1 Concurrency, Processes and Threads.
Fork and Exec Unix Model Tutorial 3. Process Management Model The Unix process management model is split into two distinct operations : 1. The creation.
The Programming Interface. Main Points Creating and managing processes – fork, exec, wait Performing I/O – open, read, write, close Communicating between.
Win32 Programming Lesson 9: Jobs & Thread Basics.
Concurrency, Mutual Exclusion and Synchronization.
MultiThreaded Applications. What is Multithreaded Programming? Having your software appear to perform multiple tasks in parallel –Individual paths of.
The process concept (section 3.1, 3.3 and demos)  Process: An entity capable of requesting and using computer resources (memory, CPU cycles, files, etc).
Windows Operating System Internals - by David A. Solomon and Mark E. Russinovich with Andreas Polze Unit OS4: Scheduling and Dispatch 4.2. Windows Processes.
Win32 Programming Lesson 10: Thread Scheduling and Priorities.
IT253: Computer Organization Lecture 3: Memory and Bit Operations Tonga Institute of Higher Education.
Processes and Threads CS550 Operating Systems. Processes and Threads These exist only at execution time They have fast state changes -> in memory and.
Introduction to the Windows API n API - Application Programming Interface n an API is the software interface for things such as the OS n an API is the.
1-1 © 2004 JMH Associates. All rights reserved. Windows Application Development Chapter 7 Windows Thread Management.
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.
1 Server Design Discuss Design issues for Servers Review Server Creation in Windows.
ITEC 502 컴퓨터 시스템 및 실습 Chapter 2-1: Process Mi-Jung Choi DPNM Lab. Dept. of CSE, POSTECH.
CE Operating Systems Lecture 10 Processes and process management in Linux.
Multithreading GSP-420.
1 Program5 Due Friday, March Prog4 user_thread... amount = … invoke delegate transact (amount)... mainThread... Total + = amount … user_thread...
C o n f i d e n t i a l 1 Course: BCA Semester: III Subject Code : BC 0042 Subject Name: Operating Systems Unit number : 1 Unit Title: Overview of Operating.
Writing a Run Time DLL The application loads the DLL using LoadLibrary() or LoadLibraryEx(). The standard search sequence is used by the operating system.
CPS4200 Unix Systems Programming Chapter 2. Programs, Processes and Threads A program is a prepared sequence of instructions to accomplish a defined task.
Linux Processes Travis Willey Jeff Mihalik. What is a process? A process is a program in execution A process includes: –program counter –stack –data section.
Silberschatz, Galvin and Gagne  Operating System Concepts Process Concept An operating system executes a variety of programs:  Batch system.
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Processes and Threads.
Fall 2002 CS 325 Class Notes Page 1 Lecture 25 Today –exec() in Unix –CreateProcess in Windows Announcements.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Unix System Calls and Posix Threads.
Department of Computer Science and Software Engineering
Threads A thread is an alternative model of program execution
Windows CE Portable Modular Real-time Small footprint Embedded market.
Notes on Processes, Context, Context Switching The following slides contain partially quoted statements from various Wikipedia pages.
Processes and Threads MICROSOFT.  Process  Process Model  Process Creation  Process Termination  Process States  Implementation of Processes  Thread.
Win32 Synchronization CS Spring Overview Kernel Synchronization - Spinlocks Executive Synchronization - Dispatcher Objects Wait Operations.
CS241 Systems Programming Discussion Section Week 2 Original slides by: Stephen Kloder.
3/17/2016cse synchronization-p2 © Perkins, DW Johnson and University of Washington1 Synchronization Part 2 CSE 410, Spring 2008 Computer.
CS241 Systems Programming Discussion Section Week 2 Original slides by: Stephen Kloder.
Mutual Exclusion -- Addendum. Mutual Exclusion in Critical Sections.
Chapter 3 The Programming Interface Chien-Chung Shen CIS/UD
1 Module 3: Processes Reading: Chapter Next Module: –Inter-process Communication –Process Scheduling –Reading: Chapter 4.5, 6.1 – 6.3.
Threads and Thread Synchronization. Introduction In windows the basic unit of execution is the thread. It is the smallest schedulable unit of execution.
Window Threads Chapter 7 Windows Thread Management.
User-Written Functions
PROCESS MANAGEMENT IN MACH
Windows Programming Lecture 09.
Concurrency, Processes and Threads
Windows Concurrency Concepts and APIs
UNIX PROCESSES.
Java Programming: Guided Learning with Early Objects
O.S. Programming Assignment
Remote Process Explorer
Waiting and Synchronization
Chapter 05. Multithread.
Tutorial: The Programming Interface
Windows Development Dynadata Copyright, 2014 © DynaData S.A. 1/49.
Concurrency, Processes and Threads
Console A presentation by Inti Vincenzo Pizzoni.
CS510 Operating System Foundations
26.
Outline Process Management Process manager Hardware process
Presentation transcript:

ISP – 3 rd Recitation “The joy of Windows API” Processes Threads Handles Relevant functions A simple code example

Process Wikipedia definition: A process is an instance of a computer program that is being sequentially executed by a computer system that has the ability to run several computer programs concurrently. A process runs in its own context and switching between processes requires context switching.

Thread Wikipedia definition: Threads are a way for a program to split itself into two or more simultaneously (or pseudo- simultaneously) running tasks. A thread is contained inside a process and different threads in the same process share some resources while different processes do not. Switching between threads of the same process requires less context switching.

An important reminder… This is not Physics… This is not Math… Programming languages and APIs, in particular, are made by humans. Understanding how to use it is important. Contemplating why something is done in a certain way and not differently is often futile.

Windows Objects and Signals Each resource the OS controls is considered an “object”. Examples: Processes, threads, mutexes, semaphores, etc. Each object is referred to in code by a “handle”. Each object can be in a signaled and non-signaled state. The meaning of the state changes for each object. Today we’re discussing processes Non-Signaled = Running Signaled = No longer running

Handle “Smart” pointer - an abstract reference to objects managed by the OS Handles don’t contain a memory address (unlike pointers). They contain a different numerical identifier. Handles should be stored, but they should never be directly changed by the programmer.

Creating a process Using CreateProcess(): Syntax : BOOL CreateProcess( LPCWSTR pszImageName, LPCWSTR pszCmdLine, LPSECURITY_ATTRIBUTES psaProcess, LPSECURITY_ATTRIBUTES psaThread, BOOL fInheritHandles, DWORD fdwCreate, LPVOID pvEnvironment, LPWSTR pszCurDir, LPSTARTUPINFOW psiStartInfo, LPPROCESS_INFORMATION pProcInfo ); Ouch… Thankfully, we don’t care about most of the parameters…

Creating a process Using createprocess(): Example : retVal=CreateProcess( NULL, // No module name (use command line). command, // Command line. NULL, // Process handle not inheritable. NULL, // Thread handle not inheritable. FALSE, // Set handle inheritance to FALSE. NORMAL_PRIORITY_CLASS, // creation/priority flags. NULL, // Use parent's environment block. NULL, // Use parent's starting directory. &startinfo, // Pointer to STARTUPINFO structure. &procinfo // Pointer to PROCESS_INFORMATION structure. ) ; RED = Not too relevant for our purposes, can keep it like in this example Green = Interesting stuff we have to understand

Creating a process createprocess() interesting parameters: LPCWSTR pszCmdLineLPCWSTR pszCmdLine – A string containing the command line we’re going to execute to create the process. (“c:\isp\ex1.exe inf.txt opf.txt”) LPSTARTUPINFOW psiStartInfoLPSTARTUPINFOW psiStartInfo – A pointer to a STARTUPINFO struct variable containing various “Microsoft Windows” parameters. (window size, screen coordinates, etc) LPPROCESS_INFORMATION pProcInfoLPPROCESS_INFORMATION pProcInfo – A Pointer to a PROCESS_INFORMATION struct variable containing the handles and IDs of the process and thread that were just created. Input Output

PROCESS_INFORMATION Definition: typedef struct _PROCESS_INFORMATION { HANDLE hProcess; HANDLE hThread; DWORD dwProcessId; DWORD dwThreadId; } PROCESS_INFORMATION; hProcess - A handle to the newly created process. hThread - A handle to the primary thread of the newly created process. dwProcessId - A value that can be used to identify a process. dwThreadId - A value that can be used to identify a thread. Note : DWORD = 32bits unsinged integer

WaitForSingleObject() WaitForSingleObject() is used for waiting for an object to become “signaled”. Syntax : DWORD WaitForSingleObject( HANDLE hProcess, DWORD dwMilliseconds ); Reminder: Each object has a signaled and non signaled state and for each object the meaning is slightly different and depends on its function.

WaitForSingleObject() dwMilliseconds is a timeout period (in ms): 0 means that WaitForSingleObject() is used for polling the status. INFINITE means that our code is waiting for the object to become signaled, even infinitely. Other values are used as a timeout. If the object is not signaled then wait until signaled or timeout occurs. Return values : WAIT_OBJECT_0 (0) – The object is in a signaled state. WAIT_TIMEOUT (102h) – Timeout has elapsed, the object is nonsignaled. WAIT_FAILED (0xFFFFFFFF) – The function has failed

TerminateProcess() TerminateProcess() is used to brutally close a process. Syntax : BOOL TerminateProcess( HANDLE hProcess, UINT uExitCode ); Returns 0 if failed, nonzero if successful NOTE : Processes that end by themselves shouldn’t normally be terminated brutally like that.

GetExitCodeProcess() GetExitCodeProcess() tells us the exit code of a process. Syntax: BOOL CloseHandle(HANDLE hObject, *DWORD lpExitCode ); DWORD = unsigned long Note: lpExitCode gets the exit code value of the process as specified by the process itself or the TerminateProcess() function. If the process is still active, a value of STILL_ACTIVE (259) is returned.

CloseHandle() CloseHandle() is used to close a handle. Syntax: BOOL CloseHandle(HANDLE hObject ); Note: It’s imperative to close all handles of an object. If a handle remains, the OS continues to keep record of the object.