Windows Operating System Internals - by David A. Solomon and Mark E. Russinovich with Andreas Polze Unit OS2: Operating System Principles 2.4. The Windows.

Slides:



Advertisements
Similar presentations
Files in C Rohit Khokher.
Advertisements

Files in C Rohit Khokher. Files in C Real life situations involve large volume of data and in such cases, the console oriented I/O operations pose two.
MPI and C-Language Seminars Seminar Plan (1/3)  Aim: Introduce the ‘C’ Programming Language.  Plan to cover: Basic C, and programming techniques.
Windows Operating System Internals - by David A. Solomon and Mark E. Russinovich with Andreas Polze Unit OS1: Overview of Operating Systems 1.1. Windows.
© Neeraj Suri EU-NSF ICT March 2006 Budapesti Műszaki és Gazdaságtudományi Egyetem Méréstechnika és Információs Rendszerek Tanszék Zoltán Micskei
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Introduction Data files –Can be created, updated,
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Lecture 2: OS Programming Interface T.Yang, CS
Chapter 11 C File Processing Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Fundamentals of Strings and Characters Characters.
Web siteWeb site ExamplesExamples 1 Mode of Operation Protected mode  4 GB  32-bit address  Windows, Linux Real-address mode  1 MB space  20-bit address.
Guide To UNIX Using Linux Third Edition
1 JMH Associates © 2004, All rights reserved Chapter 1 Getting Started with Win32/64.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 2: Operating-System Structures Modified from the text book.
Slide 6-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6.
Console and File I/O - Basics Rudra Dutta CSC Spring 2007, Section 001.
An Introduction to C Programming (assuming that you already know Java; this is not an introduction to C++)
POSIX: Files Introduction to Operating Systems: Discussion 1 Read Solaris System Interface Guide: Ch. 5.1 Basic File I/O.
Budapesti Műszaki és Gazdaságtudományi Egyetem Méréstechnika és Információs Rendszerek Tanszék Scheduling in Windows Zoltan Micskei
Slide 6-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 6.
Windows Operating System Internals - by David A. Solomon and Mark E. Russinovich with Andreas Polze Unit OS8: File System 8.5. Windows File and Directory.
Introduction (Processes and Files)
7.3. Windows Security Descriptors
Windows Operating System Internals - by David A. Solomon and Mark E. Russinovich with Andreas Polze Unit OS6: Device Management 6.1. Principles of I/O.
Advanced UNIX progamming Fall 2002 Instructor: Ashok Srinivasan Lecture 5 Acknowledgements: The syllabus and power point presentations are modified versions.
C for Java Programmers Tomasz Müldner Copyright:  Addison-Wesley Publishing Company, 2000 Introduction to C Muldner, Chapters 1, 2.
CP104 Introduction to Programming File I/O Lecture 33 __ 1 File Input/Output Text file and binary files File Input/output File input / output functions.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 2: Operating-System Structures T.Yang, 2012 Partially based on the.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 2: Operating-System Structures.
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,
22. FILE INPUT/OUTPUT. File Pointers and Streams Declarations of functions that perform file I/O appear in. Each function requires a file pointer as a.
 2007 Pearson Education, Inc. All rights reserved C File Processing.
ITEC 502 컴퓨터 시스템 및 실습 Chapter 2-1: Process Mi-Jung Choi DPNM Lab. Dept. of CSE, POSTECH.
UNIX Files File organization and a few primitives.
File IO and command line input CSE 2451 Rong Shi.
Unit OS11: Performance Evaluation Lab Manual.
System calls for Process management
Unit OS A: Windows Networking A.4. Lab Manual. 2 Copyright Notice © David A. Solomon and Mark Russinovich These materials are part of the Windows.
Introduction to Programming
Chapter 7 Files By C. Shing ITEC Dept Radford University.
FILE IO in ‘C’ by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
Windows Operating System Internals - by David A. Solomon and Mark E. Russinovich with Andreas Polze Unit OS3: Concurrency 3.3. Advanced Windows Synchronization.
System Programming Course introduction Getting Started …
Gramming An Introduction to C Programming (assuming that you already know Java; this is not an introduction to C++)
Windows Operating System Internals - by David A. Solomon and Mark E. Russinovich with Andreas Polze Unit OS1: Overview of Operating Systems 1.1. Windows.
C Programming Day 2. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Union –mechanism to create user defined data types.
Real Numbers Device driver process within the operating system that interacts with I/O controller logical record 1 logical record 2 logical record 3.
An Introduction to C Programming (assuming that you already know Java; this is not an introduction to C++)
Error handling I/O Man pages
Chapter 3: Windows7 Part 5.
Chapter 22 – part a Stream refer to any source of input or any destination for output. Many small programs, obtain all their input from one stream usually.
Lecture 11 File input/output
Unit OS7: Security 7.4. Quiz Windows Operating System Internals - by David A. Solomon and Mark E. Russinovich with Andreas Polze.
Unit OSC: Interoperability
An Introduction to C Programming
UNIX System Overview.
Unit OS4: Scheduling and Dispatch
Unit OS11: Performance Evaluation
Unit OS A: Windows Networking
Unit OS2: Operating System Principles
Unit OSB: Comparing the Linux and Windows Kernels
Chapter 3: Windows7 Part 5.
Windows APIs File Processing Copyright © 2016 Curt Hill.
Processes in Unix, Linux, and Windows
Exam 4 review Copyright © 2008 W. W. Norton & Company.
CSE 333 – Section 3 POSIX I/O Functions.
Text and Binary File Processing
File Input and Output.
Advanced UNIX progamming
Windows System Programming Third Edition
C Language B. DHIVYA 17PCA140 II MCA.
Presentation transcript:

Windows Operating System Internals - by David A. Solomon and Mark E. Russinovich with Andreas Polze Unit OS2: Operating System Principles 2.4. The Windows API - Naming, Conventions, Types

2 Copyright Notice © David A. Solomon and Mark Russinovich These materials are part of the Windows Operating System Internals Curriculum Development Kit, developed by David A. Solomon and Mark E. Russinovich with Andreas Polze Microsoft has licensed these materials from David Solomon Expert Seminars, Inc. for distribution to academic organizations solely for use in academic environments (and not for commercial use)

3 Roadmap for Section 2.3. Windows API principles Portable programming - the standard C library Comparing UNIX and Windows programming styles: an example program

4 Windows API - Overview APIs to Windows systems evolved over time: Win16 - introduced with Windows 2.0 Win32 - introduced with Windows NT, Windows 95 Win64 – introduced with Windows 64-bit edition “Windows API” summarizes all of the above In these slides, Windows API refers to Win32 and Win64

5 Windows API - major functionality File System and Character I/O Direct File Access and File Attributes Structured Exception Handling Memory Management and Memory-Mapped Files Security Process Management Inter-process Communication Threads and Scheduling, Windows Synchronization

6 Windows API Principles System resources are kernel objects referenced by a handle (handle vs. UNIX file descriptors & PIDs) Kernel objects must be manipulated via Windows API Objects – files, processes, threads, IPC pipes, memory mappings, events – have security attributes Windows API is rich & flexible: convenience functions often combine common sequences of function calls Windows API offers numerous synchronization and communication mechanisms

7 Windows API principles (contd.) Thread is unit of executions (instead of UNIX process) A process can contain one or more threads Function names are long and descriptive (as in VMS) -WaitForSingleObject() -WaitForMultipleObjects()

8 Windows API Naming Conventions Predefined data types are in uppercase BOOL (32 bit object to store single logical value) HANDLE DWORD (32 bit unsigned integer) LPTSTRLPSECURITY_ATTRIBUTE Prefix to identify pointer & const pointer LPTSTR(defined as TCHAR *) LPCTSTR(defined as const TCHAR *) (Unicode: TCHAR may be 1-byte char or 2-byte wchar_t) See \$MSDEV\INCLUDE\WINDOWS.H, WINNT.H, WINBASE.H (MSDEV=C:\Program Files\Microsoft Visual Studio\VC98\)

9 64-bit vs. 32-bit Windows APIs Pointers and types derived from pointer, e.g. handles, are 64-bit long A few others go 64, e.g. WPARAM, LPARAM, LRESULT, SIZE_T Rest are the same, e.g., 32-bit INT, DWORD, LONG Only five replacement APIs! Four for Window/Class Data Replaced by Polymorphic (_ptr) versions Updated constants used by these APIs One (_ptr) version for flat scroll bars properties API Data Model intlongpointer Win32ILP Win64LLP UNIXesLP Win32 Win64 Windows API Win32 and Win64 are referred to as the Windows API

10 Differences from UNIX HANDLEs are opaque (no short integers) No analogy to file descriptors 0,1,2 in Windows No distinctions between HANDLE and process ID Most functions treat file, process, event, pipe identically Windows API processes have no parent-child relationship Although the Windows kernel keeps this information Windows text files have CR-LF instead of LF (UNIX) Anachronisms: “long pointer“ (32 bit) LPSTR, LPVOID

11 Portability: The Standard C Library Included in the Windows API C library contains functions with limited capability to manage OS resources (e.g.; files) Often adequate for simple programs Possible to write portable programs Include files:,,,,

12 Example Application Sequential file copy: The simplest, most common, and most essential capability of any file system Common form of sequential processing Comparing programs: Quick way to introduce Windows API essentials Contrast different approaches Minimal error processing

13 Sequential File Copy UNIX: File descriptors are integers; error value: -1 read()/write() return number of bytes processed, 0 indicates EOF Positive return value indicates success close() works only for I/O objects I/O is synchronous Error processing depends on perror() & errno (global)

14 #include #define BUF_SIZE 256 int main (int argc, char *argv []) { int input_fd, output_fd; ssize_t bytes_in, bytes_out; char rec [BUF_SIZE]; if (argc != 3) { printf ("Usage: cp file1 file2\n"); return 1; } input_fd = open (argv [1], O_RDONLY); if (input_fd == -1) { perror (argv [1]); return 2; } output_fd = open(argv[2],O_WRONLY|O_CREAT,0666); if (output_fd == -1) { perror (argv [2]); return 3; } /* Process the input file a record at atime. */ while ((bytes_in = read (input_fd, &rec, BUF_SIZE)) > 0) { bytes_out = write (output_fd, &rec, bytes_in); if (bytes_out != bytes_in) { perror ("Fatal write error."); return 4; } close (input_fd); close (output_fd); return 0; } Basic cp file copy program. UNIX Implementation

15 File Copy with Standard C Library Open files identified by pointers to FILE structures NULL indicates invalid value Pointers are „handles“ to open file objects Call to fopen() specifies whether file is text or binary Errors are diagnosed with perror() of ferror() Portable between UNIX and Windows Competitive performance Still constrained to synchronous I/O No control of file security via C library

16 Basic cp file copy program. C library Implementation #include #define BUF_SIZE 256 int main (int argc, char *argv []) { FILE *in_file, *out_file; char rec [BUF_SIZE]; size_t bytes_in, bytes_out; if (argc != 3) { printf ("Usage: cp file1 file2\n"); return 1; } in_file = fopen (argv [1], "rb"); if (in_file == NULL) { perror (argv [1]); return 2; } out_file = fopen (argv [2], "wb"); if (out_file == NULL) { perror (argv [2]); return 3; } /* Process the input file a record at a time. */ while ((bytes_in = fread (rec,1,BUF_SIZE,in_file)) > 0) { bytes_out = fwrite (rec, 1, bytes_in, out_file); if (bytes_out != bytes_in) { perror ("Fatal write error."); return 4; } fclose (in_file); fclose (out_file); return 0; }

17 File Copying with Windows API imports all Windows API function definitions and data types imports all Windows API function definitions and data types Access Windows objects via variables of type HANDLE Generic CloseHandle() function works for most objects Symbolic constants and flags INVALID_HANDLE_VALUE, GENERIC_READ Functions return boolean values System error codes obtained via GetLastError() Windows security is complex and difficult to program

18 Basic cp file copy program. Windows API Implementation #include #define BUF_SIZE 256 int main (int argc, LPTSTR argv []) { HANDLE hIn, hOut; DWORD nIn, nOut; CHAR Buffer [BUF_SIZE]; if (argc != 3) { printf("Usage: cp file1 file2\n"); return 1; } hIn = CreateFile (argv [1], GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if (hIn == INVALID_HANDLE_VALUE) { printf ("Input file error:%x\n", GetLastError ()); return 2; } hOut = CreateFile (argv [2], GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); if (hOut == INVALID_HANDLE_VALUE) { printf("Output file error: %x\n", GetLastError ()); return 3; } while (ReadFile (hIn,Buffer, BUF_SIZE, &nIn, NULL) && nIn > 0) { WriteFile (hOut, Buffer,nIn,&nOut,NULL); if (nIn != nOut) { printf ("Fatal write error: %x\n", GetLastError ()); return 4; } CloseHandle (hIn); CloseHandle (hOut); return 0; }

19 File Copying with Windows API Convenience Functions Convenience functions may improve performance Programmer does not need to be concerned about arbitrary buffer sizes OS manages speed vs. space tradeoffs at runtime #include int main (int argc, LPTSTR argv []) { if (argc != 3) { printf ("Usage: cp file1 file2\n"); return 1; } if (!CopyFile (argv [1], argv [2], FALSE)) { printf ("CopyFile Error: %x\n", GetLastError ()); return 2; } return 0; }

20 Further Reading Johnson M. Hart, Win32 System Programming: A Windows® 2000 Application Developer's Guide, 2nd Edition, Addison-Wesley, (This book discusses select Windows programming problems and addresses the problem of portable programming by comparing Windows and Unix approaches). (This book discusses select Windows programming problems and addresses the problem of portable programming by comparing Windows and Unix approaches). Jeffrey Richter, Programming Applications for Microsoft Windows, 4th Edition, Microsoft Press, September (This book provides a comprehensive discussion of the Windows API – suggested reading).