Lecture 25 25. Lecture 25 Review of Last Lecture DLL’s DLL’s Processes Processes Threads Threads Memory Management Memory Management.

Slides:



Advertisements
Similar presentations
Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
Advertisements

Project 2 教學. Synchronization Functions in Windows HANDLE WINAPI CreateSemaphore ( __in_opt LPSECURITY_ATTRIBUTES lpSemaphoreAttributes, __in LONG lInitialCount,
Threads 1 CS502 Spring 2006 Threads CS-502 Spring 2006.
Process Management. External View of the OS Hardware fork() CreateProcess() CreateThread() close() CloseHandle() sleep() semctl() signal() SetWaitableTimer()
Threads CNS What is a thread?  an independent unit of execution within a process  a "lightweight process"  an independent unit of execution within.
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.
Chapter 4 Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Chapter 4: Threads Overview Multithreading.
chap13 Chapter 13 Programming in the Large.
Prepared by Fareeha Lecturer DCS IIUI 1 Windows API.
MultiThreaded Applications. What is Multithreaded Programming? Having your software appear to perform multiple tasks in parallel –Individual paths of.
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.
Lecture 11 Dynamic link libraries. Differences between static libraries and DLLs In static library code is added to the executable. In DLL, the code is.
1-1 © 2004 JMH Associates. All rights reserved. Windows Application Development Chapter 7 Windows Thread Management.
Windows thread programming
Source: Operating System Concepts by Silberschatz, Galvin and Gagne.
Operating System Concepts and Techniques Lecture 4 Thread M. Naghibzadeh Reference M. Naghibzadeh, Operating System Concepts and Techniques, First ed.,
Dynamic Link Libraries: Inside Out. Dynamic Link Libraries  About Dynamic-Link Libraries  Dynamic-Link Libraries Hands On  Dynamic Link Library Reference.
Writing a Run Time DLL The application loads the DLL using LoadLibrary() or LoadLibraryEx(). The standard search sequence is used by the operating system.
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.
Win32 Programming Lesson 19: Introduction to DLLs.
Windows Threading Colin Roby Jaewook Kim.
NCHU System & Network Lab Lab #6 Thread Management Operating System Lab.
W indows Programming Lecture 08. Brief History of Win Windows is announced 1984 Work begins on Word for Windows 1.0 November 1985: Windows 1.0.
Threads and Thread Synchronization. Introduction In windows the basic unit of execution is the thread. It is the smallest schedulable unit of execution.
7/9/ Realizing Concurrency using Posix Threads (pthreads) B. Ramamurthy.
Window Threads Chapter 7 Windows Thread Management.
Realizing Concurrency using the thread model
Chapter 3: Windows7 Part 5.
OPERATING SYSTEM CONCEPT AND PRACTISE
Process concept.
Realizing Concurrency using the thread model
Windows Programming Lecture 09.
Day 12 Threads.
Lecture 7 : Multithread programming
CS399 New Beginnings Jonathan Walpole.
Konstantin Bukin CSE791 – Advanced Windows Programming Summer 2001
Threads CSSE 332 Operating Systems Rose-Hulman Institute of Technology
Windows Concurrency Concepts and APIs
Security mechanisms and vulnerabilities in .NET
Threads and Locks.
Threads and Cooperation
CSC 253 Lecture 8.
Chapter 3: Windows7 Part 5.
Windows Internals Brown-Bag Seminar Chapter 1 – Concepts and Tools
Realizing Concurrency using Posix Threads (pthreads)
CSC 253 Lecture 8.
Linux Fork/Exec Example
Chapter 6 Methods: A Deeper Look
Chapter 9 Classes: A Deeper Look, Part 1
Realizing Concurrency using the thread model
UNIX Fork/Exec Example
Realizing Concurrency using the thread model
Chapter 05. Multithread.
UNIX Fork/Exec Example
Lecture Topics: 11/1 General Operating System Concepts Processes
Chien-Chung Shen CIS/UD
Operating Systems Lecture 3.
Realizing Concurrency using the thread model
Realizing Concurrency using Posix Threads (pthreads)
Realizing Concurrency using the thread model
Console A presentation by Inti Vincenzo Pizzoni.
Realizing Concurrency using Posix Threads (pthreads)
Linux Fork/Exec Example
26.
Following Malware Execution in IDA
More C++ Classes Systems Programming.
Threads CSE 2431: Introduction to Operating Systems
Presentation transcript:

Lecture 25 25

Lecture 25 Review of Last Lecture DLL’s DLL’s Processes Processes Threads Threads Memory Management Memory Management

Lecture 25 The Import Libraries.LIB Concept of an import library.LIB Concept of an import library.LIB Import Library is statically linked Import Library is statically linked How to create an import library? How to create an import library? Important System DLLs: Kernel32.dll, User32.dll, Gdi32.dll Important System DLLs: Kernel32.dll, User32.dll, Gdi32.dll Import Libraries of System DLLs: Kernel32.lib, User32.lib, Gdi32.lib Import Libraries of System DLLs: Kernel32.lib, User32.lib, Gdi32.lib

Lecture 25 Variable scope in DLLs Static variables have scope limited to the block in which they are declared. As a result, each process has its own instance of the DLL global and static variables by default. Static variables have scope limited to the block in which they are declared. As a result, each process has its own instance of the DLL global and static variables by default.

Lecture 25 Variable scope in DLLs Sharing variables across multiple processes #pragma data_seg(“OUR_DATA”) and its usage #pragma data_seg(“OUR_DATA”) and its usage Specifying a section in the executable in Visual C Using linker option: Using linker option: /section: OUR_DATA,rws Using pre-processor directive in source file: Using pre-processor directive in source file: #pragma comment(linker, “/SECTION: OUR_DATA, RWS”) Tip: “Quick View” utility can be used to verify shared section(s)

Lecture 25 Calling Conventions and DLLs Significance of Calling Conventions of the caller and the called function in a DLL Significance of Calling Conventions of the caller and the called function in a DLL C++ uses same calling convention / parameter passing as C, but performs name decoration C++ uses same calling convention / parameter passing as C, but performs name decoration extern “C” { … … function declarations … } prevents C++ name-decoration extern “C” { … … function declarations … } prevents C++ name-decoration

Lecture 25 Windows Hooks Win32 Hooks are a way to trap messages before they reach a single or any application Win32 Hooks are a way to trap messages before they reach a single or any application Application level hook Application level hook Hook that affects a single application, i.e. the one that installs it. The hook procedure can be in the same executable as the original application. Hook that affects a single application, i.e. the one that installs it. The hook procedure can be in the same executable as the original application. System level hook System level hook Hook that effects all applications in the system. This must be implemented separately in a DLL. This DLL is then loaded in the memory space of every running process, by the system. Hook that effects all applications in the system. This must be implemented separately in a DLL. This DLL is then loaded in the memory space of every running process, by the system.

Lecture 25 Resource-only DLLs Including resources in a DLL Including resources in a DLL LoadBitmap(), LoadString(), LoadMenu(), LoadIcon() LoadBitmap(), LoadString(), LoadMenu(), LoadIcon() HRSRC FindResource( HMODULE hModule, // module handle HMODULE hModule, // module handle LPCTSTR lpName, // resource name LPCTSTR lpName, // resource name LPCTSTR lpType // resource type LPCTSTR lpType // resource type); lpType can be RT_ICON, RT_MENU, RT_STRING etc. Use of resource-only DLLs for internationalisation Use of resource-only DLLs for internationalisation

Lecture 25 DLL versions Why versioning? The comctl32.dll example Why versioning? The comctl32.dll example The VERSIONINFO resource statement The VERSIONINFO resource statement

Lecture 25 VERSIONINFO resource statement VS_VERSION_INFO VERSIONINFO FILEVERSION 1,0,0,1 FILEVERSION 1,0,0,1 PRODUCTVERSION 1,0,0,1 PRODUCTVERSION 1,0,0,1 FILEFLAGSMASK 0x3fL FILEFLAGSMASK 0x3fL FILEFLAGS 0x1L FILEFLAGS 0x1L FILEOS 0x40004L FILEOS 0x40004L FILETYPE 0x1L FILETYPE 0x1L FILESUBTYPE 0x0L FILESUBTYPE 0x0LBEGIN BLOCK "StringFileInfo" BLOCK "StringFileInfo" BEGIN BEGIN BLOCK "040904b0" BLOCK "040904b0" BEGIN BEGIN VALUE "CompanyName", "Virtual University\0" VALUE "CompanyName", "Virtual University\0" VALUE "LegalCopyright", "Copyright © 2002 Virtual University\0" END END END ENDEND

Lecture 25 DLL versions BOOL GetFileVersionInfo( LPTSTR lptstrFilename, // file name LPTSTR lptstrFilename, // file name DWORD dwHandle, // ignored DWORD dwHandle, // ignored DWORD dwLen, // size of buffer DWORD dwLen, // size of buffer LPVOID lpData // version information buffer LPVOID lpData // version information buffer); HRESULT CALLBACK DllGetVersion( DLLVERSIONINFO *pdvi); HRESULT CALLBACK DllGetVersion( DLLVERSIONINFO *pdvi);

Lecture 25 Thread basics WinMain() is the primary thread. WinMain() is the primary thread. All other threads created in the process are secondary threads All other threads created in the process are secondary threads Usage of threads Usage of threads

Lecture 25 Thread basics User Interface (UI) and Worker threads User Interface (UI) and Worker threads User interface threads own one or more Windows and have their own message queue User interface threads own one or more Windows and have their own message queue Worker threads do not own any windows, and may not have a message queue Worker threads do not own any windows, and may not have a message queue

Lecture 25 Thread basics Relationship of a Process and threads; a Thread and windows in Windows Relationship of a Process and threads; a Thread and windows in Windows A process may consist of one or more threads A process may consist of one or more threads A thread may own zero or more windows A thread may own zero or more windows

Lecture 25 Creating Secondary Threads How to create secondary threads? How to create secondary threads? _beginthread() C runtime _beginthread() C runtime The CreateThread() Win32 API The CreateThread() Win32 API

Lecture 25 Creating Secondary Threads unsigned long _beginthread( void( __cdecl *start_address )( void * ), pointer to thread function unsigned stack_size, stack-size for new thread void *arglist ); argument list to be passed to thread

Lecture 25 Threads and message queuing The message routing: System message queue > Thread message queue > Window procedure (if a UI thread) The message routing: System message queue > Thread message queue > Window procedure (if a UI thread) Threads with and without a message queue Threads with and without a message queue UI threads always have a message queue UI threads always have a message queue When is the message queue created? When is the message queue created? The system creates a thread-specific message queue only when the thread makes its first call to one of the Win32 User or GDI functions The system creates a thread-specific message queue only when the thread makes its first call to one of the Win32 User or GDI functions

Lecture 25 The Thread Procedure Thread procedure can be any function with the following signatures (return value and parameter list): DWORD WINAPI ThreadProc( LPVOID lpParameter LPVOID lpParameter Thread data passed to the thread function );

Lecture 25 Creating Secondary Threads HANDLE CreateThread( LPSECURITY_ATTRIBUTES lpThreadAttributes,// Security Descriptor LPSECURITY_ATTRIBUTES lpThreadAttributes,// Security Descriptor DWORD dwStackSize, // initial stack size DWORD dwStackSize, // initial stack size LPTHREAD_START_ROUTINE lpStartAddress, // thread function LPTHREAD_START_ROUTINE lpStartAddress, // thread function LPVOID lpParameter, // thread argument LPVOID lpParameter, // thread argument DWORD dwCreationFlags, // creation option DWORD dwCreationFlags, // creation option LPDWORD lpThreadId // thread identifier LPDWORD lpThreadId // thread identifier);

Lecture 25 Secondary Thread Procedure The thread functions The thread functions A thread terminates when its thread procedure returns A thread terminates when its thread procedure returns Terminating a thread within a thread function: _endthread() and ExitThread() Terminating a thread within a thread function: _endthread() and ExitThread() Difference between return values of C runtime and Win32 thread creation functions Difference between return values of C runtime and Win32 thread creation functions The Thread Handles The Thread Handles

Lecture 25 More about Threads Thread handles and thread IDs Thread handles and thread IDs CREATE_SUSPENDED flag CREATE_SUSPENDED flag SuspendThread(), ResumeThread() SuspendThread(), ResumeThread() Sleep() call Sleep() call Advantages of Threads Advantages of Threads