Operating Systems. Privileged Instructions



Similar documents
System Calls and Standard I/O

CSC 2405: Computer Systems II

Lecture 24 Systems Programming in C

Introduction. What is an Operating System?

Operating System Structure

The C Programming Language course syllabus associate level

Operating Systems. Lecture 03. February 11, 2013

COS 318: Operating Systems

Lab 4: Socket Programming: netcat part

Computer-System Architecture

Embedded Programming in C/C++: Lesson-1: Programming Elements and Programming in C

Operating Systems and Networks

Lecture 16: System-Level I/O

Tutorial on C Language Programming

Have both hardware and software. Want to hide the details from the programmer (user).

System Calls Related to File Manipulation

Giving credit where credit is due

Chapter 6, The Operating System Machine Level

LOW LEVEL FILE PROCESSING

Exceptions in MIPS. know the exception mechanism in MIPS be able to write a simple exception handler for a MIPS machine

C++ INTERVIEW QUESTIONS

Process Description and Control william stallings, maurizio pizzonia - sistemi operativi

Hacking Techniques & Intrusion Detection. Ali Al-Shemery arabnix [at] gmail

Jorix kernel: real-time scheduling

Last Class: OS and Computer Architecture. Last Class: OS and Computer Architecture

How To Write Portable Programs In C

MICROPROCESSOR. Exclusive for IACE Students iacehyd.blogspot.in Ph: /422 Page 1

Software Vulnerabilities

Operating System Components

Instruction Set Architecture (ISA)

Networks. Inter-process Communication. Pipes. Inter-process Communication

Advanced Computer Architecture-CS501. Computer Systems Design and Architecture 2.1, 2.2, 3.2

Instruction Set Architecture

KITES TECHNOLOGY COURSE MODULE (C, C++, DS)

Chapter 11: Input/Output Organisation. Lesson 06: Programmed IO

Keil C51 Cross Compiler

OS: IPC I. Cooperating Processes. CIT 595 Spring Message Passing vs. Shared Memory. Message Passing: Unix Pipes

The programming language C. sws1 1

/* File: blkcopy.c. size_t n

CS161: Operating Systems

File Handling. What is a file?

Operating System Overview. Otto J. Anshus

Microprocessor & Assembly Language

Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

CHAPTER 7: The CPU and Memory

Introduction to Operating Systems. Perspective of the Computer. System Software. Indiana University Chen Yu

Faculty of Engineering Student Number:

Chapter 3: Operating-System Structures. System Components Operating System Services System Calls System Programs System Structure Virtual Machines

Intel P6 Systemprogrammering 2007 Föreläsning 5 P6/Linux Memory System

ASCII Encoding. The char Type. Manipulating Characters. Manipulating Characters

6.828 Operating System Engineering: Fall Quiz II Solutions THIS IS AN OPEN BOOK, OPEN NOTES QUIZ.

MICROPROCESSOR AND MICROCOMPUTER BASICS

Chapter 3 Operating-System Structures

How To Understand How A Process Works In Unix (Shell) (Shell Shell) (Program) (Unix) (For A Non-Program) And (Shell).Orgode) (Powerpoint) (Permanent) (Processes

Using the CoreSight ITM for debug and testing in RTX applications

CSC230 Getting Starting in C. Tyler Bletsch

Tutorial on Socket Programming

How To Port A Program To Dynamic C (C) (C-Based) (Program) (For A Non Portable Program) (Un Portable) (Permanent) (Non Portable) C-Based (Programs) (Powerpoint)

Linux Driver Devices. Why, When, Which, How?

Minix Mini Unix (Minix) basically, a UNIX - compatible operating system. Minix is small in size, with microkernel-based design. Minix has been kept

Illustration 1: Diagram of program function and data flow

Lecture 22: C Programming 4 Embedded Systems

An Introduction to Assembly Programming with the ARM 32-bit Processor Family

Objectives. Chapter 2: Operating-System Structures. Operating System Services (Cont.) Operating System Services. Operating System Services (Cont.

MACHINE ARCHITECTURE & LANGUAGE

FRONT FLYLEAF PAGE. This page has been intentionally left blank

Lesson Objectives. To provide a grand tour of the major operating systems components To provide coverage of basic computer system organization

Chapter 11 I/O Management and Disk Scheduling

Member Functions of the istream Class

CSE 120 Principles of Operating Systems. Modules, Interfaces, Structure

W4118 Operating Systems. Junfeng Yang

Intro to GPU computing. Spring 2015 Mark Silberstein, , Technion 1

UNIX Sockets. COS 461 Precept 1

How to design and implement firmware for embedded systems

MPLAB TM C30 Managed PSV Pointers. Beta support included with MPLAB C30 V3.00

Review from last time. CS 537 Lecture 3 OS Structure. OS structure. What you should learn from this lecture

Programmation Systèmes Cours 7 IPC: FIFO

Outline. Review. Inter process communication Signals Fork Pipes FIFO. Spotlights

Linux/UNIX System Programming. POSIX Shared Memory. Michael Kerrisk, man7.org c February 2015

CPU Organization and Assembly Language

PART B QUESTIONS AND ANSWERS UNIT I

C / C++ and Unix Programming. Materials adapted from Dan Hood and Dianna Xu

As previously noted, a byte can contain a numeric value in the range Computers don't understand Latin, Cyrillic, Hindi, Arabic character sets!

Informatica e Sistemi in Tempo Reale

ARM Thumb Microcontrollers. Application Note. Software ISO 7816 I/O Line Implementation. Features. Introduction

Virtual vs Physical Addresses

ADVANCED PROCESSOR ARCHITECTURES AND MEMORY ORGANISATION Lesson-17: Memory organisation, and types of memory

The Operating System and the Kernel

Chapter 7D The Java Virtual Machine

Central Processing Unit (CPU)

Linux Kernel Architecture

Programing the Microprocessor in C Microprocessor System Design and Interfacing ECE 362

(Refer Slide Time: 00:01:16 min)

Stacks. Linear data structures

Operating Systems 4 th Class

Shared Memory Introduction

C++ Programming Language

PART-A Questions. 2. How does an enumerated statement differ from a typedef statement?

ASSEMBLY PROGRAMMING ON A VIRTUAL COMPUTER

Transcription:

Operating Systems Operating systems manage processes and resources Processes are executing instances of programs may be the same or different programs process 1 code data process 2 code data process 3 code data SV SV SV state vector: information necessary to start, stop, and restart the process State vector: registers, program counter, memory mgmt registers, etc. user-level processes assembly & high-level languages operating system, kernel bare machine machine language & system calls machine language Copyright 1997 Computer Science 217: Operating System Page 187 Privileged Instructions Machines have two kinds of instructions 1. normal instructions, e.g., add, sub, etc. 2. privileged instructions, e.g., initiate I/O switch state vectors or contexts load/save from protected memory etc. Operating systems hide privileged instructions and provide virtual instructions to access and manipulate virtual resources, e.g., I/O to and from disc files Virtual instructions are system calls Operating systems interpret virtual instructions Copyright 1997 Computer Science 217: Operating System Page 188

Processor Modes Machine level typically has 2 modes, e.g., user mode and kernel mode User mode processor executes normal instructions in the user s program upon encountering a privileged instruction, processor switches to kernel mode, and the operating system performs a service Kernel mode processor executes both normal and privileged instructions User-to-kernel switch saves the information necessary to continue the execution of the user process Another view Operating system is a process that runs in kernel mode. Copyright 1997 Computer Science 217: Operating System Page 189 Virtual Resources OS provides a high-level representation of low-level resources For example, low-level disks are presented as file systems simple to use file system tree-structured, hierarchical directories named files files are sequences of bytes UNIX Operating System network k disk drives different capacities, speeds access via cylinder, sector, track fixed I/O format and transfer rules authentication error correction and handling Copyright 1997 Computer Science 217: Operating System Page 190

System Calls Virtual instructions are often presented as a set of system calls Typical implementations (in order of prevalence) single privileged instruction with parameters interpret to other privileged instructions jump to fixed locations Parameters are passed in a machine-dependent manner in fixed registers in fixed memory locations in an argument block, with the block s address in a register in-line with the system call on the stack combination of the above System calls return results in registers, memory, etc., and an error indication Copyright 1997 Computer Science 217: Operating System Page 191 System Calls, cont d System call mechanism is tailored to the machine architecture system calls on the SPARC use a trap instruction ta 0 trap always; a trap value of 0 indicates a system call parameters are in registers %g1, %o0 %o5 and on the stack System call interface often designed to accommodate high-level languages system calls are accessed by a library of procedures e.g., on UNIX, system calls are packaged as a library of C functions Typical UNIX system call nread = read(fd, buffer, n); returns the number of bytes read from the file fd, or -1 if an error occurs what about EOF? Copyright 1997 Computer Science 217: Operating System Page 192

Implementing System Calls as Functions In the caller mov fd,%o0 mov buffer,%o1 mov n,%o2 call _read; nop mov %o0,nread Implementation of read _read: set 3,%g1 /* 3 indicates READ system call */ ta 0 bcc L1; nop set _errno,%g1 /* sets errno to the error code */ st %o0,[%g1] set -1,%o0 /* return -1 to indicate an error */ L1: retl; nop operating system sets the C bit if an error occurred stores an error code in %o0; see /usr/include/sys/errno.h note that read is a leaf function UNIX has ~150 system calls see man 2 intro and /usr/include/syscall.h Copyright 1997 Computer Science 217: Operating System Page 193 Exceptions and Interrupts Operating systems also field exceptions and interrupts Exceptions (a.k.a. traps): caused by execution of an instruction e.g., divide by 0, illegal address, memory protection violation, illegal opcode Exceptions are like implicit system calls operating systems can pass control to user processes to handle exceptions (e.g., signals ) operating systems have ways to process exceptions by defaults e.g., segmentation fault and core dump Interrupts: caused by external activity unrelated to the user process e.g., I/O completion, clock tick, etc. Interrupts are like transparent system calls normally user processes cannot detect interrupts, nor need to deal with them Copyright 1997 Computer Science 217: Operating System Page 194

A trap instruction enters kernel mode disables other traps decrements CWP saves PC, npc in %r17, %r18 sets PC to TBR, npc to TBR + 4 Hardware trap codes 1 reset 2 access exception 3 illegal instruction... Software trap codes sets TBR to trap number + 128 SPARC Traps There are conditional traps just like conditional branches There are separate floating point traps Copyright 1997 Computer Science 217: Operating System Page 195 System Calls for Input/Output Associating/disassociating files with file descriptors int open(char *filename, int flags, int mode) int close(int fd) Reading/writing from file descriptors int read(int fd, char *buf, int nbytes) int write(int fd, char *buf, int nbytes) Another version of cp source destination (see src/cp1.c) #include <sys/file.h> main(int argc, char *argv[]) { int count, src, dst; char buf[4096]; if (argc!= 3) error("usage: %s source destination\n", argv[0]); if ((src = open(argv[1], O_RDONLY, 0)) < 0) error("%s: can t read %s \n", argv[0], argv[1]); if ((dst = open(argv[2], O_WRONLY O_CREAT, 0666)) < 0) error("%s: can t write %s \n", argv[0], argv[2]); while ((count = read(src, buf, sizeof buf)) > 0) write(dst, buf, count); return EXIT_SUCCESS; } Copyright 1997 Computer Science 217: Operating System Page 196

Write with Confidence Most programs don t check for write errors or writes that are too large int ironclad_write(int fd, char *buf, int nbytes) { char *p, *q; int n; } p = buf; q = buf + nbytes; while (p < q) if ((n = write(fd, p, q - p)) > 0) p += n; else perror("iconclad_write:"); return nbytes; perror issues a diagnostic for the error code in errno iconclad_write: file system full Copyright 1997 Computer Science 217: Operating System Page 197 Buffered I/O Single-character I/O is usually too slow int getchar(void) { char c; } if (read(0, &c, 1) == 1) return c; return EOF; Solution: read chunks of input into a buffer, dole out chars one at a time int getchar(void) { static char buf[1024]; static char *p; static int n = 0; } if (n--) return *p++; if ((n = read(0, p = buf, sizeof buf)) > 0) return getchar(); n = 0; return EOF; Where s the bug? Copyright 1997 Computer Science 217: Operating System Page 198

Implementing the Standard I/O Library Single-character I/O functions are usually implemented as macros #define getc(p) (--(p)->_cnt >= 0? \ (int)(*(unsigned char *)(p)->_ptr++) : \ _filbuf(p)) #define getchar() (getc(stdin)) A FILE holds per-file buffer information typedef struct _iobuf { int _cnt; /* number of characters/slots left in the buffer */ char *_ptr; /* pointer to the next character in the buffer */ char *_base; /* the beginning of the buffer */ int _bufsiz; /* size of the buffer */ short _flag; /* open mode flags, etc. */ char _file; /* associated file descriptor */ } FILE; extern FILE *stdin, *stdout, *stderr; See /usr/princeton/include/ansi/stdio.h Copyright 1997 Computer Science 217: Operating System Page 199 Buffered Writes Single-character writes are usually implemented by macros #define putc(c,p) (--(p)->_cnt >= 0? \ (p)->_ptr++ = (c) : \ _flsbuf((c), (p))) #define putchar(c) (putc((c),stdout)) Buffering can interfere with interactive streams for (p = "Enter your name:\n"; *p; p++) putchar(*p); for (p = buf; ; p++) if ((*p = getchar()) == '\n') break; for (p = "Enter your age:\n"; *p; p++) putchar(*p); for (p = buf; ; p++) if ((*p = getchar()) == '\n') break; bug: program waits for input before prompt appears Copyright 1997 Computer Science 217: Operating System Page 200

Buffered Writes, cont d Output stream must be flushed before reading the input void fflush(file *stream) for (p = "Enter your name:\n"; *p; p++) putchar(*p); fflush(stdout); for (p = buf; ; p++) if ((*p = getchar()) == '\n') break; for (p = "Enter your age:\n"; *p; p++) putchar(*p); fflush(stdout); for (p = buf; ; p++) if ((*p = getchar()) == '\n') break; Standard I/O supports line-buffered files #define putc(x, p) (--(p)->_cnt >= 0?\ (int)(*(unsigned char *)(p)->_ptr++ = (x)) : \ (((p)->_flag&_iolbf) && -(p)->_cnt < (p)->_bufsiz? \ ((*(p)->_ptr = (x))!= '\n'? \ (int)(*(unsigned char *)(p)->_ptr++) : \ _flsbuf(*(unsigned char *)(p)->_ptr, p)) : \ _flsbuf((unsigned char)(x), p))) Why is line buffering necessary? f = fopen("/dev/tty", "w") Copyright 1997 Computer Science 217: Operating System Page 201