System Programming Course introduction Getting Started …

Slides:



Advertisements
Similar presentations
Threads, SMP, and Microkernels
Advertisements

Chorus and other Microkernels Presented by: Jonathan Tanner and Brian Doyle Articles By: Jon Udell Peter D. Varhol Dick Pountain.
Chap 2 System Structures.
Threads, SMP, and Microkernels Chapter 4. Process Resource ownership - process is allocated a virtual address space to hold the process image Scheduling/execution-
OS Fall ’ 02 Introduction Operating Systems Fall 2002.
OS Spring’03 Introduction Operating Systems Spring 2003.
Operating Systems 1 K. Salah Module 1.2: Fundamental Concepts Interrupts System Calls.
OPERATING SYSTEMS Introduction
Chapter 6 Implementing Processes, Threads, and Resources.
1 JMH Associates © 2004, All rights reserved Chapter 1 Getting Started with Win32/64.
OS Spring’04 Introduction Operating Systems Spring 2004.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 2: Operating-System Structures Modified from the text book.
1 Threads Chapter 4 Reading: 4.1,4.4, Process Characteristics l Unit of resource ownership - process is allocated: n a virtual address space to.
I/O Tanenbaum, ch. 5 p. 329 – 427 Silberschatz, ch. 13 p
Chapter 51 Threads Chapter 5. 2 Process Characteristics  Concept of Process has two facets.  A Process is: A Unit of resource ownership:  a virtual.
CSE 451: Operating Systems Autumn 2013 Module 6 Review of Processes, Kernel Threads, User-Level Threads Ed Lazowska 570 Allen.
Chapter 8 Windows Outline Programming Windows 2000 System structure Processes and threads in Windows 2000 Memory management The Windows 2000 file.
MODERN OPERATING SYSTEMS Third Edition ANDREW S. TANENBAUM Chapter 11 Case Study 2: Windows Vista Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall,
Chapter 4 Threads, SMP, and Microkernels Dave Bremer Otago Polytechnic, N.Z. ©2008, Prentice Hall Operating Systems: Internals and Design Principles, 6/E.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 2: System Structures.
Protection and the Kernel: Mode, Space, and Context.
Chapter 4: Threads. 4.2CSCI 380 Operating Systems Chapter 4: Threads Overview Multithreading Models Threading Issues Pthreads Windows XP Threads Linux.
Chapter 1. Introduction What is an Operating System? Mainframe Systems
Threads, SMP, and Microkernels Chapter 4. 2 Outline n Threads n Symmetric Multiprocessing (SMP) n Microkernel n Linux Threads.
Chapter 6 Operating System Support. This chapter describes how middleware is supported by the operating system facilities at the nodes of a distributed.
System Calls ULK Chapter 10 COMS W4118 Spring 2008.
OS provide a user-friendly environment and manage resources of the computer system. Operating systems manage: –Processes –Memory –Storage –I/O subsystem.
Windows 2000 Course Summary Computing Department, Lancaster University, UK.
Processes and Threads Processes have two characteristics: – Resource ownership - process includes a virtual address space to hold the process image – Scheduling/execution.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 2: Operating-System Structures.
CE Operating Systems Lecture 11 Windows – Object manager and process management.
Operating Systems Lecture 7 OS Potpourri Adapted from Operating Systems Lecture Notes, Copyright 1997 Martin C. Rinard. Zhiqing Liu School of Software.
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.
Threads, SMP, and Microkernels Chapter 4. Process Resource ownership - process is allocated a virtual address space to hold the process image Scheduling/execution-
Advanced Design and System Patterns The Microkernel Pattern.
UNIX Files File organization and a few primitives.
NT Kernel CS Spring Overview Interrupts and Exceptions: Trap Handler Interrupt Request Levels and IRT DPC’s, and APC’s System Service Dispatching.
Operating Systems Lecture November 2015© Copyright Virtual University of Pakistan 2 Agenda for Today Review of previous lecture Hardware (I/O, memory,
Processes Introduction to Operating Systems: Module 3.
1 Threads, SMP, and Microkernels Chapter Multithreading Operating system supports multiple threads of execution within a single process MS-DOS.
Interrupt driven I/O. MIPS RISC Exception Mechanism The processor operates in The processor operates in user mode user mode kernel mode kernel mode Access.
Operating Systems 1 K. Salah Module 1.2: Fundamental Concepts Interrupts System Calls.
Windows Operating System Internals - by David A. Solomon and Mark E. Russinovich with Andreas Polze Unit OS2: Operating System Principles 2.4. The Windows.
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.
Interrupt driven I/O Computer Organization and Assembly Language: Module 12.
What is a Process ? A program in execution.
Interrupts and Exception Handling. Execution We are quite aware of the Fetch, Execute process of the control unit of the CPU –Fetch and instruction as.
Embedded Real-Time Systems Processing interrupts Lecturer Department University.
CISC2200 Threads Fall 09. Process  We learn the concept of process  A program in execution  A process owns some resources  A process executes a program.
CSCI/CMPE 4334 Operating Systems Review: Exam 1 1.
1 Chapter 2: Operating-System Structures Services Interface provided to users & programmers –System calls (programmer access) –User level access to system.
S ALVATORE DI G IROLAMO (TA) Networks and Operating Systems: Exercise Session 1.
CSCE451/851 Introduction to Operating Systems
Introduction to Operating Systems Concepts
CS 3214 Computer Systems Lecture 9 Godmar Back.
Chapter 2: Operating-System Structures
OPERATING SYSTEM CONCEPT AND PRACTISE
Windows API.
Protection of System Resources
Unit OS2: Operating System Principles
Chapter 3: Windows7 Part 2.
Threads, SMP, and Microkernels
Chapter 3: Windows7 Part 2.
Lecture 4- Threads, SMP, and Microkernels
B.Ramamurthy Chapter 2 : Appendix
Threads Chapter 4.
Operating Systems Lecture 3.
CSE 451: Operating Systems Autumn 2003 Lecture 2 Architectural Support for Operating Systems Hank Levy 596 Allen Center 1.
CSE 451: Operating Systems Winter 2003 Lecture 2 Architectural Support for Operating Systems Hank Levy 412 Sieg Hall 1.
Operating Systems Structure
Presentation transcript:

System Programming Course introduction Getting Started …

– 2 – OBJECTIVES Windows API, UNIX system calls, C library Architecture style and programming conventions API and functions

– 3 – KERNEL HAL: Hardware Abstraction HARDWARE Process Manager Systems Services Virtual Memory Manager I/O Manager NT Executive Protected Subsystems Applications OS/2 Program Windows Program POSIX Program OS/2 Subsystem Windows Subsystem POSIX Subsystem THE WINDOWS NT ARCHITECTURE

– 4 – WINDOWS NT 5 (1/2) All platforms use the Windows API, BUT there are differences: Windows NT4 (and above) has full NSA “Orange Book” C2 security features. “NT” means NT 4.0 and above (including all NT5) Windows 9X only runs on Intel x86 architecture Only NT supports SMP Windows 2003 also runs on Itanium,... Windows 2003 for Win64 migration Note: Windows CE also supports Windows on several processor architectures

– 5 – WINDOWS NT5 (2/2) Windows NT uses UNICODE international character set throughout Windows 9X limits asynchronous I/O to serial devices Windows NT has a fully protected kernel Windows NT supports the NTFS, a robust file system Windows 9X and CE will not support as many resources Open files, processes, etc. Many Windows 9X Windows functions have restricted implementations In general, Windows programs are portable between platforms at both the source and, mostly, binary level

– 6 – THE WINDOWS NT ARCHITECTURE Windows is the dominant environment running on the NT (all versions) executive OS/2 and POSIX compatility modes are rarely used Historical interest only

– 7 – UNIX Kernel Architecture

– 8 – C Library, UNIX system calls, Win32 API C Library Defined in the ANSI-C standard Functions defined in standard POSIX (Portable Operating System Interface) POSIX functions NOT in C library are listed in: Windows Application Programming Interface (Win32 API). Windows supports the ANSI-C library Windows sockets Remote Procedure Calls

– 9 – What is a System Call? User-level processes (clients) request services from the kernel (server) via special “protected procedure calls” System calls provide: An abstraction layer between processes and hardware, allowing the kernel to provide access control, arbitration A virtualization of the underlying system A well-defined “API” (ASI?) for system services

– 10 – System Calls: how do they work ? ProcessKernel (user mode)(kernel mode) syscall (params) syscall routine bodies.… dispatch vector switcher other process

– 11 – Implementing System Calls Initiating a system call is known as “trapping into the kernel” and is usually effected by a software initiated interrupt to the CPU Example: Intel (“int 80h”), ARM (“swi”) CPU saves current context, changes mode and transfers to a well- defined location in the kernel System calls are designated by small integers; once in the kernel, a dispatch table is used to invoke the corresponding kernel function A special assembly instruction (Intel “iret”) is used to return to user mode from kernel mode

– 12 – System Calls vs. Library Calls System calls can only be initiated by assembly code (special software interrupt instructions) Processes normally call library “wrapper routines” that hide the details of system call entry/exit Library calls are much faster than system calls Library calls (man 3), system calls (man 2) Some library functions: never call syscalls (strlen), some always call syscalls (mmap), some occasionally call syscalls (printf)

– 13 – Designing the Syscall Interface Important to keep interface small, stable Early UNIXes had about 60 system calls, Linux 2.6 has about 300; Solaris more, Window more still Aside: Windows does not publicly document syscalls and only documents library wrapper routines (unlike UNIX/Linux) Syscall numbers cannot be reused (!); deprecated syscalls are implemented by a special “not implemented” syscall (sys_ni)

– 14 – Dual-Mode Architecture Modern architectures all support at least two execution modes: regular (user) and privileged (kernel) Intel supports 4 modes or rings but only rings 0 and 3 are used Some instructions are not allowed in user mode and will cause an exception if executed Examples include: Setting up page tables Almost any kind of device I/O Changing the mode bit

– 15 – Trapping into the Kernel Trapping into the kernel involves executing a special assembly instruction that activates the interrupt hardware just like a device interrupt would but it is done by software instead so it is known as a “software interrupt” Intel uses the “int” (interrupt) instruction with the operand “80h” (80 hex = 128), indicating the interrupt handler that should be executed “int 80h” ultimately causes control to transfer to the assembly label: system_call in the kernel (found in arch/kernel/i386/entry.S) Every process in Linux has the usual user-mode stack as well as a small, special kernel stack that is part of the kernel address space; trapping involves switching the stack pointer to the kernel stack (and back)

– 16 – Getting Started with Windows Naming conventions Programming conventions Style Sample program

– 17 – THE Windows API Windows is the 32-bit API used by: Windows 9X (95, 98, Me) Windows NT Windows CE (palmtops, embedded systems, etc.) Win64 is very similar at the source level Supported on Windows 2003 and Itanium processor family Windows statements nearly always apply to Win64 There are several major subdivisions, including: Windows Management Graphics Device Interface (GDI) System Services Multimedia Remote Procedure Calls

– 18 – GETTING STARTED: Windows PRINCIPLES (1/2) Nearly every resource is an “object” identified and referenced by a “handle” of type HANDLE Handles’ role is similar to UNIX file descriptors, BUT Handles are blind objects, whereas UNIX file descriptors are integers (in a sequential order: 0, 1, 2, …) Kernel objects must be manipulated by WindowsAPIs Not object oriented HANDLE datatype objects include: filespipes processesmemory mapping threadsevents, mutexes, semaphores

– 19 – GETTING STARTED: Windows PRINCIPLES (2/2) Windows API is rich and flexible Many functions perform the same or similar operations Each function has numerous parameters and flags Many synchronization and communication components available Windows thread is the basic unit of execution, rather than a process A process can contain one or more threads Each process has its own code and data address space Threads share the process address space Threads are “lightweight” and more efficient than processes Used for servers, asynchronous I/O, …

– 20 – Windows NAMING CONVENTIONS Long and descriptive WaitForSingleObject WaitForMultipleObjects Predefined descriptive data types in upper case BOOL, DWORD, LPDWORD,... Predefined types avoid the * operator and make distinctions: LPTSTR (defined as TCHAR * ) and LPCTSTR (defined as const TCHAR * ) Variable names in API descriptions use “Hungarian” notation - we’ll avoid this convention lpFileName — long pointer [to a zero terminated string]

– 21 – Windows PROGRAMMING CONVENTIONS is always included is always included All objects identified by variables of type HANDLE CloseHandle function applies to (nearly) all objects Symbolic constants and flags which explain their meaning INVALID_HANDLE_VALUE and GENERIC_READ ReadFile, WriteFile, and many other Windows functions return Boolean values System error codes obtained through GetLastError () C library always available But you cannot fully exploit Windows with it

– 22 – EXAMPLE: Windows FILE COPY (1/3) /* Basic cp file copy program */ /* cp file1 file2: Copy file1 to file2 */ #include /* Always required for Windows */ #include #include #define BUF_SIZE 256 /* Increase for faster copy */ int main (int argc, LPTSTR argv []) { HANDLE hIn, hOut; /* Input and output handles */ DWORD nIn, nOut; /* Number bytes transferred */ CHAR Buffer [BUF_SIZE]; if (argc != 3) { printf ("Usage: cp file1 file2\n"); return 1; }

– 23 – EXAMPLE: Windows FILE COPY (2/3) /* Create handles for reading and writing. Many*/ /*default values are used */ hIn = CreateFile (argv [1], GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if (hIn == INVALID_HANDLE_VALUE) { printf ("Cannot open input file\n"); return 2; } hOut = CreateFile (argv [2], GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); if (hOut == INVALID_HANDLE_VALUE) { printf ("Cannot open output file\n"); return 3; }

– 24 – EXAMPLE: Windows FILE COPY (3/3) /*Input and output file handles are open.*/ /*Copy file. Note end-of-file detection */ while (ReadFile (hIn, Buffer, BUF_SIZE, &nIn, NULL) && nIn > 0) WriteFile (hOut, Buffer, nIn, &nOut, NULL); /*Deallocate resources, such as open handles */ CloseHandle (hIn); CloseHandle (hOut); return 0; }

– 25 – How to copy a file Four implementations: standard C library (chaptr01/cpC.c); standard C library (chaptr01/cpC.c); Unix style (POSIX) (chaptr01/cpU.c); Unix style (POSIX) (chaptr01/cpU.c); Win32 “base” (chaptr01/cpW.c); Win32 “base” (chaptr01/cpW.c); Win32 “convenience” function: CopyFile (chaptr01/cpCF.c); Win32 “convenience” function: CopyFile (chaptr01/cpCF.c);