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.

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

1 Processes and Threads Creation and Termination States Usage Implementations.
Real-Time Library: RTX
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 18.
1 Created by Another Process Reason: modeling concurrent sub-tasks Fetch large amount data from network and process them Two sub-tasks: fetching  processing.
15-213/ Intro to Computer Systems by btan with reference to Spring 10’s slides.
CSC 501 Lecture 2: Processes. Von Neumann Model Both program and data reside in memory Execution stages in CPU: Fetch instruction Decode instruction Execute.
Processes CSCI 444/544 Operating Systems Fall 2008.
ISP – 3 rd Recitation “The joy of Windows API” Processes Threads Handles Relevant functions A simple code example.
Honors Compilers Addressing of Local Variables Mar 19 th, 2002.
Introduction to Operating Systems – Windows process and thread management In this lecture we will cover Threads and processes in Windows Thread priority.
Process Management. External View of the OS Hardware fork() CreateProcess() CreateThread() close() CloseHandle() sleep() semctl() signal() SetWaitableTimer()
OS Spring’03 Introduction Operating Systems Spring 2003.
Process in Unix, Linux and Windows CS-3013 C-term Processes in Unix, Linux, and Windows CS-3013 Operating Systems (Slides include materials from.
Chapter 6 Implementing Processes, Threads, and Resources.
OS Spring’04 Introduction Operating Systems Spring 2004.
Slide 6-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6.
Processes CS 416: Operating Systems Design, Spring 2001 Department of Computer Science Rutgers University
Slide 6-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6.
Chapter 8 Windows Outline Programming Windows 2000 System structure Processes and threads in Windows 2000 Memory management The Windows 2000 file.
Win32 Programming Lesson 9: Jobs & Thread Basics.
Process in Unix, Linux, and Windows CS-3013 A-term Processes in Unix, Linux, and Windows CS-3013 Operating Systems (Slides include materials from.
Win32 Programming Lesson 13: Thread Pooling (Wow, Java is good for something…)
Introduction to Processes CS Intoduction to Operating Systems.
CSC 501 Lecture 2: Processes. Process Process is a running program a program in execution an “instantiation” of a program Program is a bunch of instructions.
Chapter 41 Processes Chapter 4. 2 Processes  Multiprogramming operating systems are built around the concept of process (also called task).  A process.
Win32 Programming Lesson 20: Advanced DLL Techniques.
Win32 Programming Lesson 22: DLL Magic Part Deux All your base are belong to us…
Processes and Threads CS550 Operating Systems. Processes and Threads These exist only at execution time They have fast state changes -> in memory and.
Lecture 2 Foundations and Definitions Processes/Threads.
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.
Hardware process When the computer is powered up, it begins to execute fetch-execute cycle for the program that is stored in memory at the boot strap entry.
ITEC 502 컴퓨터 시스템 및 실습 Chapter 2-1: Process Mi-Jung Choi DPNM Lab. Dept. of CSE, POSTECH.
Win32 Programming Lesson 8a: Jobs. Where are we?  Grouping processes into logical groups: Jobs  However, processes don’t retain a parent- child relationship.
Processes CS 6560: Operating Systems Design. 2 Von Neuman Model Both text (program) and data reside in memory Execution cycle Fetch instruction Decode.
Lecture 5: Threads process as a unit of scheduling and a unit of resource allocation processes vs. threads what to program with threads why use threads.
1 Computer Systems II Introduction to Processes. 2 First Two Major Computer System Evolution Steps Led to the idea of multiprogramming (multiple concurrent.
Department of Computer Science and Software Engineering
Win32 Programming Lesson 17: Memory Mapped Files (Finally, cool stuff again, all this work is getting tedious!)
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Operating Systems Processes and Threads.
Hardware process When the computer is powered up, it begins to execute fetch-execute cycle for the program that is stored in memory at the boot strap entry.
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 and Threads MICROSOFT.  Process  Process Model  Process Creation  Process Termination  Process States  Implementation of Processes  Thread.
By Anand George SourceLens.org Copyright. All rights reserved. Content Owner - Meera R (meera at sourcelens.org)
CPS110: Implementing threads on a uni-processor Landon Cox January 29, 2008.
Lecture 5 Page 1 CS 111 Online Process Creation Processes get created (and destroyed) all the time in a typical computer Some by explicit user command.
Threads prepared and instructed by Shmuel Wimer Eng. Faculty, Bar-Ilan University 1July 2016Processes.
A process is a program in execution A running system consists of multiple processes – OS processes Processes started by the OS to do “system things” –
WORKING OF SCHEDULER IN OS
Chapter 3: Windows7 Part 5.
Processes and threads.
Process concept.
PROCESS MANAGEMENT IN MACH
Lesson One – Creating a thread
Linux Processes & Threads
Process Creation Processes get created (and destroyed) all the time in a typical computer Some by explicit user command Some by invocation from other running.
Operating Systems: A Modern Perspective, Chapter 6
Threads and Locks.
Processes in Unix, Linux, and Windows
Chapter 3: Windows7 Part 5.
Processes in Unix, Linux, and Windows
System Structure and Process Model
Chapter 2 Processes and Threads 2.1 Processes 2.2 Threads
Processes in Unix, Linux, and Windows
Implementing Processes, Threads, and Resources
CS510 Operating System Foundations
Processes Creation and Threads
Foundations and Definitions
Presentation transcript:

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 to get any work done (doesn’t that sound like your Sophomore year?)

Executing Code  The Operating System organizes executing code into a logical hierarchy Processes Threads Fibers

Definition  A Process is defined as An instance of a running program, which consists of two components:  A kernel object used by the operating system to manage the process  An address space which contains all the DLLs, executable code and data used by the process Processes are inert: to accomplish anything, a process must have at least one thread that runs in its context

Threads  Each thread executes “simultaneously” (in some sense of the word… why, why not?)  Each thread has its own stack frame, and set of CPU registers

Scheduling  The OS schedules some time for each thread  Each thread gets a time slice or quantum from the CPU which provides the illusion of concurrency  We can think of it as a “round robin” scheduler… but it is significantly much more complicated  When a process is created, it is generally created with one thread, called the primary thread – this thread can create other threads

Windows Applications  Two flavors GUI (all that Windowsy stuff) CUI (command line – the One True Way™)  Of course, life isn’t so simple – it’s not a binary decision  Every program needs an entry point – WinMain, wWinMain etc.  The Operating System doesn’t call this – it calls a special startup function

Startup Code  The Runtime Libraries do a number of things Retrieve a pointer to the command line parameters of the process Retrieve a pointer to the new environment variables of the process Initialize the RTL’s global variables Initialize the heap for the RTL Call constructors for global and static objects

A Process’ Instance Handle  Every DLL or executable gets its own Instance Handle, which is unique  Used when we load resources (like icons)  Can get the handle of a DLL with something like: HMODULE GetModuleHandle(PCTSTR pszModule);

The Command Line  Can use the global variables __argv  Can call down to PTSTR GetCommandLine()  Can go from here to an argv-like structure using: PWSTR CommandLineToArgvW( PWSTR pszCmdLine, int* pNumArgs );

Free alloced memory  int nNumArgs; PWSTR *ppArgv = CommandLineToArgvW( GetCommandLineW(), &nNumArgs); // Use the arguments… if (*ppArgv[1] == L'x') { } // Free the memory block HeapFree(GetProcessHeap(), 0, ppArg v);

Environment Variables  Associated on a per-process basis  Stored in the environment block  Stored in the form: VarName1=Value1\0 VarName2=Value2\0 … VarNameX=ValueX\0 \0

Associated Functions  GetEnvironmentVariable( PCTSTR pszName, PTSTR pszValue, DWORD dwSize);  Often in the form “%USERPROFILE%\My Documents”  Windows supplies a helper function for this case

ExpandEnvironmentString  ExpandEnvironmentStrings( PCTSTR pszSrc, PTSTR pszDst, DWORD nSize);  Where nSize is the maximum space available for expansion

ErrorModes  A process can choose to trap certain errors itself instead of having the Operating System trap those errors  UINT SetErrorMode(UINT fuErrorMode)

CurrentDirectory and Directory  Example: CreateFile  DWORD GetCurrentDirectory ( DWORD cchCurDir, PTSTR pszCurDir);  BOOL SetCurrentDirectory(PCTSTR pszCurDir);  Current Directories are also stored in the environment variables

Finally…  GetVersion and GetVersionEx  Read the history, it’s somewhat amusing

Creating a Process  Simple: use CreateProcess: BOOL CreateProcess( PCTSTR pszApplicationName, PTSTR pszCommandLine, PSECURITY_ATTRIBUTES psaProcess, PSECURITY_ATTRIBUTES psaThread, BOOL bInheritHandles, DWORD fdwCreate, PVOID pvEnvironment, PCTSTR pszCurDir, PSTARTUPINFO psaStartInfo, PPROCESS_INFORMATION ppiProcInfo); I could write out the usage, but let’s just look it up…

Terminating a Process  Four ways The primary thread’s entry-point function returns (YES) One thread in the process calls ExitProcess (NO) A thread in another process calls TerminateProcess (NO) All the threads just happen to die on their own (Never happens)

Entry point returns  When the primary thread dies Any C++ objects are destroyed using destructors Memory from the stack is correctly freed The process’ exit code is set The process’ kernel object is decremented

ExitProcess  Fine from an OS perspective  Horrible from the RTL perspective as Destructors aren’t called – you can prove this to yourself if you want to

ChildProcesses PROCESS_INFORMATION pi; DWORD dwExitCode; // Spawn the child process. BOOL fSuccess = CreateProcess(..., &pi); if (fSuccess) { // Close the thread handle as // soon as it is no longer needed! CloseHandle(pi.hThread); // Suspend our execution until // the child has terminated. WaitForSingleObject(pi.hProcess, INFINITE); // The child process terminated; //get its exit code. GetExitCodeProcess(pi.hProcess, &dwExitCode); // Close the process handle as soon as it // is no longer needed. CloseHandle(pi.hProcess); }

Detaching a Process  In the previous example, the processes are linked – the child process won’t be destroyed until the Parent reads the exit code  However, you can detach a process by closing the associated handles in the parent process

Enumerating Processes  Assignment time!  Easily create a simple application which enumerates all the processes on a machine. You may use a command-line application if you wish, or a GUI. Print out as much information about each process as you can.  This looks really nice in.NET… but that’s a little more tricky.  Your call – CLI is okay, but a nice GUI gets you extra marks…