CS 33. Architecture and the OS. CS33 Intro to Computer Systems XIX 1 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Similar documents
CS 33. Architecture and the OS. CS33 Intro to Computer Systems XIX 1 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Introduction to OS Processes in Unix, Linux, and Windows MOS 2.1 Mahmoud El-Gayyar

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 20

CS 355 Operating Systems. Keeping Track of Processes. When are processes created? Process States 1/26/18. Processes, Unix Processes and System Calls

518 Lecture Notes Week 3

Unix-Linux 2. Unix is supposed to leave room in the process table for a superuser process that could be used to kill errant processes.

CSC 1600 Unix Processes. Goals of This Lecture

Operating System Structure

CS240: Programming in C

Processes. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

CSci 4061 Introduction to Operating Systems. Processes in C/Unix

CS240: Programming in C

The Process Abstraction. CMPU 334 Operating Systems Jason Waterman

Reading Assignment 4. n Chapter 4 Threads, due 2/7. 1/31/13 CSE325 - Processes 1

CITS2002 Systems Programming. Creating a new process using fork() 1 next CITS2002 CITS2002 schedule

Processes. Johan Montelius KTH

A process. the stack

Fall 2015 COMP Operating Systems. Lab #3

Operating systems and concurrency - B03

Processes & Threads. Today. Next Time. ! Process concept! Process model! Implementing processes! Multiprocessing once again. ! More of the same J

CS 537 Lecture 2 - Processes

Interrupts, Fork, I/O Basics

TCSS 422: OPERATING SYSTEMS

Week 2 Intro to the Shell with Fork, Exec, Wait. Sarah Diesburg Operating Systems CS 3430

CS Operating Systems Lab 3: UNIX Processes

OS lpr. www. nfsd gcc emacs ls 1/27/09. Process Management. CS 537 Lecture 3: Processes. Example OS in operation. Why Processes? Simplicity + Speed

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2017 Lecture 19

Processes. Operating System CS 217. Supports virtual machines. Provides services: User Process. User Process. OS Kernel. Hardware

PESIT Bangalore South Campus Hosur road, 1km before Electronic City, Bengaluru -100 Department of Information Sciences and Engineering

Computer Systems Assignment 2: Fork and Threads Package

Process management. What s in a process? What is a process? The OS s process namespace. A process s address space (idealized)

OS lpr. www. nfsd gcc emacs ls 9/18/11. Process Management. CS 537 Lecture 4: Processes. The Process. Why Processes? Simplicity + Speed

CSE 451: Operating Systems Winter Module 4 Processes. Mark Zbikowski Allen Center 476

CS510 Operating System Foundations. Jonathan Walpole

Lesson 3. The func procedure allows a user to choose the action upon receipt of a signal.

CPSC 341 OS & Networks. Processes. Dr. Yingwu Zhu

CS 322 Operating Systems Practice Midterm Questions

Lecture 4: Process Management

Lesson 2. process id = 1000 text data i = 5 pid = 1200

Windows architecture. user. mode. Env. subsystems. Executive. Device drivers Kernel. kernel. mode HAL. Hardware. Process B. Process C.

Maria Hybinette, UGA. ! One easy way to communicate is to use files. ! File descriptors. 3 Maria Hybinette, UGA. ! Simple example: who sort

Processes. Process Management Chapter 3. When does a process gets created? When does a process gets terminated?

www nfsd emacs lpr Process Management CS 537 Lecture 4: Processes Example OS in operation Why Processes? Simplicity + Speed

CS 3305 Intro to Threads. Lecture 6

Section 2: Processes

Altering the Control Flow

Altering the Control Flow

CSE 153 Design of Operating Systems Fall 2018

Part II Processes and Threads Process Basics

Recall: Four fundamental OS concepts. Recall: give the illusion of multiple processors? Process Control Block

SE350: Operating Systems

Prepared by Prof. Hui Jiang Process. Prof. Hui Jiang Dept of Electrical Engineering and Computer Science, York University

System Programming. Process Control III

CS 201. Processes. Gerson Robboy Portland State University

CS 33. Shells and Files. CS33 Intro to Computer Systems XX 1 Copyright 2017 Thomas W. Doeppner. All rights reserved.

CSE 410: Computer Systems Spring Processes. John Zahorjan Allen Center 534

Recitation 8: Tshlab + VM

CSCE Operating Systems Interrupts, Exceptions, and Signals. Qiang Zeng, Ph.D. Fall 2018

Process. Prepared by Prof. Hui Jiang Dept. of EECS, York Univ. 1. Process in Memory (I) PROCESS. Process. How OS manages CPU usage? No.

CS 550 Operating Systems Spring Process II

Mon Sep 17, 2007 Lecture 3: Process Management

Preview. Process Control. What is process? Process identifier The fork() System Call File Sharing Race Condition. COSC350 System Software, Fall

PROCESS CONTROL BLOCK TWO-STATE MODEL (CONT D)

CS 261 Fall Mike Lam, Professor. Exceptional Control Flow and Processes

Operating Systems CS 217. Provides each process with a virtual machine. Promises each program the illusion of having whole machine to itself

A2 Design Considerations. CS161, Spring

Project 2--User Programs

CSE 380 Computer Operating Systems. Instructor: Insup Lee. University of Pennsylvania Fall 2003

Assignment 1. Teaching Assistant: Michalis Pachilakis (

The Kernel Abstraction. Chapter 2 OSPP Part I

System Programming. Process Control II

CS5460: Operating Systems

Processes. q Process concept q Process model and implementation q Multiprocessing once again q Next Time: Scheduling

Recitation Processes, Signals,UNIX error handling

CISC2200 Threads Spring 2015

Hardware OS & OS- Application interface

Sample Solutions to Project 1

CS 31: Intro to Systems Processes. Kevin Webb Swarthmore College March 31, 2016

Processes. CS439: Principles of Computer Systems January 30, 2019

What is a Process. Preview. What is a Process. What is a Process. Process Instruction Cycle. Process Instruction Cycle 3/14/2018.

Processes. CS3026 Operating Systems Lecture 05

3. Process Management in xv6

Recitation 8 Processes, Signals, Tshlab

September 2 nd, 2015 Prof. John Kubiatowicz

API Interlude: Process Creation (DRAFT)

There are 10 total numbered pages, 5 Questions. You have 60 minutes. Budget your time carefully!

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto

Exceptions and Processes

POSIX threads CS 241. February 17, Copyright University of Illinois CS 241 Staff

CSC209 Fall Karen Reid 1

UNIX System Calls. Sys Calls versus Library Func

CS 333 Introduction to Operating Systems. Class 3 Threads & Concurrency. Jonathan Walpole Computer Science Portland State University

Processes. Overview. Processes. Process Creation. Process Creation fork() Processes. CPU scheduling. Pål Halvorsen 21/9-2005

What Is A Process? Process States. Process Concept. Process Control Block (PCB) Process State Transition Diagram 9/6/2013. Process Fundamentals

CSCB09: Software Tools and Systems Programming. Bianca Schroeder IC 460

SYSTEM CALL IMPLEMENTATION. CS124 Operating Systems Fall , Lecture 14

Exceptional Control Flow Part I

Processes. Today. Next Time. ! Process concept! Process model! Implementing processes! Multiprocessing once again. ! Scheduling processes

an infinite loop Processes and Exceptions doing nothing on a busy system timing nothing

CS 333 Introduction to Operating Systems. Class 3 Threads & Concurrency. Jonathan Walpole Computer Science Portland State University

Transcription:

CS 33 Architecture and the OS CS33 Intro to Computer Systems XIX 1 Copyright 2018 Thomas W. Doeppner. All rights reserved.

The Operating System My Program Mary s Program Bob s Program OS CS33 Intro to Computer Systems XIX 2 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Processes Containers for programs virtual memory» address space scheduling» one or more threads of control file references» open files and lots more! CS33 Intro to Computer Systems XIX 3 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Idiot Proof Can I clobber Mary s program? int main( ) { int i; int A[1]; My Program for (i=0; ; i++) A[rand()] = i; Mary s Program CS33 Intro to Computer Systems XIX 4 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Fair Share void runforever( ){ while(1) ; int main( My Program ) { runforever(); Can I prevent Bob s program from running? Bob s Program CS33 Intro to Computer Systems XIX 5 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Architectural Support for the OS Not all instructions are created equal... non-privileged instructions» can affect only current program privileged instructions» may affect entire system Processor mode user mode» can execute only non-privileged instructions privileged mode» can execute all instructions CS33 Intro to Computer Systems XIX 6 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Which Instructions Should Be Privileged? I/O instructions Those that affect how memory is mapped Halt instruction Some others... CS33 Intro to Computer Systems XIX 7 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Who Is Privileged? No one user code always runs in user mode The operating-system kernel runs in privileged mode nothing else does not even super user on Unix or administrator on Windows CS33 Intro to Computer Systems XIX 8 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Entering Privileged Mode How is OS invoked? very carefully... strictly in response to interrupts and exceptions (booting is a special case) CS33 Intro to Computer Systems XIX 9 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Interrupts and Exceptions Things don t always go smoothly... I/O devices demand attention timers expire programs demand OS services programs demand storage be made accessible programs have problems Interrupts demand for attention by external sources Exceptions executing program requires attention CS33 Intro to Computer Systems XIX 10 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Exceptions Traps intentional exceptions» execution of special instruction to invoke OS after servicing, execution resumes with next instruction Faults a problem condition that is normally corrected after servicing, instruction is re-tried Aborts something went dreadfully wrong... not possible to re-try instruction, nor to go on to next instruction CS33 Intro to Computer Systems XIX 11 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Actions for Interrupts and Exceptions When interrupt or exception occurs processor saves state of current thread/process on stack processor switches to privileged mode (if not already there) invokes handler for interrupt/exception if thread/process is to be resumed (typical action after interrupt)» thread/process state is restored from stack if thread/process is to re-execute current instruction» thread/process state is restored, after backing up instruction pointer if thread/process is to terminate» it s terminated CS33 Intro to Computer Systems XIX 12 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Interrupt and Exception Handlers Interrupt or exception invokes handler (in OS) via interrupt and exception vector» one entry for each possible interrupt/exception contains address of handler code executed in privileged mode» but code is part of the OS intrpt/excp i handler 0 addr handler 1 addr handler 2 addr... handler i addr... handler n-1 addr handler i CS33 Intro to Computer Systems XIX 13 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Entering and Exiting Entering/exiting interrupt/exception handler more involved than entering/exiting a procedure must deal with processor mode» switch to privileged mode on entry» switch back to previous mode on exit interrupted process/thread's state is saved on separate kernel stack stack in kernel must be different from stack in user program» why? CS33 Intro to Computer Systems XIX 14 Copyright 2018 Thomas W. Doeppner. All rights reserved.

One Stack Per Mode Frame 1 Frame 2 Frame 3 Intrp/Excp Frame Frame 4 Frame 5 user stack kernel stack CS33 Intro to Computer Systems XIX 15 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Quiz 1 If an interrupt occurs, which general-purpose registers must be pushed onto the kernel stack? a) none b) callee-save registers c) caller-save registers d) all CS33 Intro to Computer Systems XIX 16 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Back to the x86... It s complicated more than it should be, but for historical reasons... Not just privileged and non-privileged modes, but four privilege levels level 0» most privileged, used by OS kernel level 1» not normally used level 2» not normally used level 3» least privileged, used by application code CS33 Intro to Computer Systems XIX 17 Copyright 2018 Thomas W. Doeppner. All rights reserved.

The Unix Address Space stack read-write dynamic bss data text read-write read-only CS33 Intro to Computer Systems XIX 18 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Creating Your Own Processes #include <unistd.h> int main( ) { pid_t pid; if ((pid = fork()) == 0) { /* new process starts running here */ /* old process continues here */ CS33 Intro to Computer Systems XIX 19 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Creating a Process: Before fork( ) parent process CS33 Intro to Computer Systems XIX 20 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Creating a Process: After fork( ) // returns p parent process fork( ) // returns 0 child process (pid = p) CS33 Intro to Computer Systems XIX 21 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Quiz 2 The following program a) runs forever b) terminates quickly int flag; int main() { while (flag == 0) { if (fork() == 0) { // in child process flag = 1; exit(0); // causes process to terminate CS33 Intro to Computer Systems XIX 22 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Process IDs int main( ) { pid_t pid; pid_t ParentPid = getpid(); parent prints: 27355, 27342, 27342 child prints: if ((pid = fork()) == 0) { 0, 27342, 27355 printf("%d, %d, %d\n", pid, ParentPid, getpid()); return 0; printf("%d, %d, %d\n", pid, ParentPid, getpid()); return 0; CS33 Intro to Computer Systems XIX 23 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Putting Programs into Processes execv... if (fork() == 0){ execv("prog", argv);... fork... /* prog */ int main() { if (fork() == 0){ execl("prog", 0);..... CS33 Intro to Computer Systems XIX 24 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Exec Family of related system functions we concentrate on one:» execv(program, argv) First real argument char *argv[] = {"MyProg", "12", (void *)0; if (fork() == 0) { execv("./myprog", argv); End of list Name of the file that contains the program argv[0] is the name of the program CS33 Intro to Computer Systems XIX 25 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Loading a New Image args prog s bss execv(prog, argv) Before prog s data prog s text After CS33 Intro to Computer Systems XIX 26 Copyright 2018 Thomas W. Doeppner. All rights reserved.

A Random Program int main(int argc, char *argv[]) { if (argc!= 2) { fprintf(stderr, "Usage: random count\n"); exit(1); int stop = atoi(argv[1]); for (int i = 0; i < stop; i++) printf("%d\n", rand()); return 0; CS33 Intro to Computer Systems XIX 27 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Passing It Arguments From the shell $ random 12 From a C program if (fork() == 0) { char *argv[] = {"random", "12", (void *)0; execv("./random", argv); CS33 Intro to Computer Systems XIX 28 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Quiz 3 if (fork() == 0) { char *argv[] = {"random", "12", (void *)0; execv("./random", argv); printf("random done\n"); The printf statement will be executed a) only if execv fails b) only if execv succeeds c) always CS33 Intro to Computer Systems XIX 29 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Receiving Arguments int main(int argc, char *argv[]) { if (argc!= 2) { fprintf(stderr, "Usage: random count\n"); exit(1); int stop = atoi(argv[1]); for (int i = 0; i < stop; i++) printf("%d\n", rand()); return 0; r a n d o m \0 argv 1 2 \0 CS33 Intro to Computer Systems XIX 30 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Not So Fast How does the shell invoke your program? if (fork() == 0) { char *argv = {"random", "12", (void *)0; execv("./random", argv); /* what does the shell do here??? */ CS33 Intro to Computer Systems XIX 31 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Wait #include <unistd.h> #include <sys/wait.h> pid_t pid; int status; if ((pid = fork()) == 0) { char *argv[] = {"random", "12", (void *)0; execv("./random", argv); waitpid(pid, &status, 0); CS33 Intro to Computer Systems XIX 32 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Exit #include <unistd.h> #include <stdlib.h> #include <sys/wait.h> int main( ) { pid_t pid; int status; if ((pid = fork()) == 0) { if (do_work() == 1) exit(0); /* success! */ else exit code exit(1); /* failure */ waitpid(pid, &status, 0); /* low-order byte of status contains exit code. WEXITSTATUS(status) extracts it */ CS33 Intro to Computer Systems XIX 33 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Shell: To Wait or Not To Wait... $ who if ((pid = fork()) == 0) { char *argv[] = {"who", 0; execv("who", argv); waitpid(pid, &status, 0); $ who & if ((pid = fork()) == 0) { char *argv[] = {"who", 0; execv("who", argv); CS33 Intro to Computer Systems XIX 34 Copyright 2018 Thomas W. Doeppner. All rights reserved.

System Calls Sole direct interface between user and kernel Implemented as library routines that execute trap instructions to enter kernel Errors indicated by returns of 1; error code is in global variable errno if (write(fd, buffer, bufsize) == 1) { // error! printf("error %d\n", errno); // see perror CS33 Intro to Computer Systems XIX 35 Copyright 2018 Thomas W. Doeppner. All rights reserved.

System Calls other stuff kernel stack Kernel portion of address space kernel text trap into kernel User portion of address space write(fd, buf, len) CS33 Intro to Computer Systems XIX 36 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Multiple Processes kernel data other stuff kernel stack other stuff kernel stack other stuff kernel stack kernel text CS33 Intro to Computer Systems XIX 37 Copyright 2018 Thomas W. Doeppner. All rights reserved.