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

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

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 20

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

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

CS240: Programming in C

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

CSC 1600 Unix Processes. Goals of This Lecture

Processes. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Operating System Structure

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.

518 Lecture Notes Week 3

CS240: Programming in C

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

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

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

CS Operating Systems Lab 3: UNIX Processes

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

The Process Abstraction. CMPU 334 Operating Systems Jason Waterman

Processes. Johan Montelius KTH

A process. the stack

CS 537 Lecture 2 - Processes

Operating systems and concurrency - B03

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

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

Interrupts, Fork, I/O Basics

Fall 2015 COMP Operating Systems. Lab #3

CS510 Operating System Foundations. Jonathan Walpole

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

TCSS 422: OPERATING SYSTEMS

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

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

Section 2: Processes

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

Altering the Control Flow

Altering the Control Flow

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

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

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

Computer Systems Assignment 2: Fork and Threads Package

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

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2017 Lecture 19

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

Part II Processes and Threads Process Basics

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

System Programming. Process Control III

Recitation 8: Tshlab + VM

Assignment 1. Teaching Assistant: Michalis Pachilakis (

System Programming. Process Control II

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

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

Project 2--User Programs

CS 201. Processes. Gerson Robboy Portland State University

Hardware OS & OS- Application interface

Sample Solutions to Project 1

Lecture 4: Process Management

SE350: Operating Systems

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

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

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

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

CS 322 Operating Systems Practice Midterm Questions

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

Exceptional Control Flow Part I

CSE 153 Design of Operating Systems Fall 2018

Exceptions and Processes

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 18

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

Each terminal window has a process group associated with it this defines the current foreground process group. Keyboard-generated signals are sent to

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

Light-Weight Processes: Dissecting Linux Threads

Process management 1

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

API Interlude: Process Creation (DRAFT)

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

CS 3305 Intro to Threads. Lecture 6

COE518 Lecture Notes Week 2 (Sept. 12, 2011)

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

CSC209 Fall Karen Reid 1

PROCESS CONTROL BLOCK TWO-STATE MODEL (CONT D)

COMP 3100 Operating Systems

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

UNIX System Calls. Sys Calls versus Library Func

Processes. CS3026 Operating Systems Lecture 05

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

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

Workshop on Inter Process Communication Solutions

Mon Sep 17, 2007 Lecture 3: Process Management

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

SYSTEM CALL IMPLEMENTATION. CS124 Operating Systems Fall , Lecture 14

by Marina Cholakyan, Hyduke Noshadi, Sepehr Sahba and Young Cha

Introduction to Processes

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

OS Interaction and Processes

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

System Programming. Signals II

CS 550 Operating Systems Spring Process II

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

Project 2: Shell with History1

September 2 nd, 2015 Prof. John Kubiatowicz

Transcription:

CS 33 Architecture and the OS CS33 Intro to Computer Systems XIX 1 Copyright 2017 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 2017 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 2017 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 2017 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 2017 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 2017 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 2017 Thomas W. Doeppner. All rights reserved.

Who Is Privileged? You re not and neither is anyone else 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 2017 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 2017 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 2017 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 2017 Thomas W. Doeppner. All rights reserved.

Interrupt and Exception Handling 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 12 Copyright 2017 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 stack in kernel must be different from stack in user program» why? CS33 Intro to Computer Systems XIX 13 Copyright 2017 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 14 Copyright 2017 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 15 Copyright 2017 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 16 Copyright 2017 Thomas W. Doeppner. All rights reserved.

The Unix Address Space stack dynamic bss data text CS33 Intro to Computer Systems XIX 17 Copyright 2017 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 18 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Creating a Process: Before fork( ) parent process CS33 Intro to Computer Systems XIX 19 Copyright 2017 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 20 Copyright 2017 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) { flag = 1; exit(0); // causes process to terminate CS33 Intro to Computer Systems XIX 21 Copyright 2017 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 22 Copyright 2017 Thomas W. Doeppner. All rights reserved.

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

Exec Family of related routines 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 24 Copyright 2017 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 25 Copyright 2017 Thomas W. Doeppner. All rights reserved.

A Random Program int main(int argc, char *argv[]) { int stop = atoi(argv[1]); for (int i = 0; i < stop; i++) printf("%d\n", rand()); return 0; CS33 Intro to Computer Systems XIX 26 Copyright 2017 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 27 Copyright 2017 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 only if something goes wrong with execv. a) yes b) no CS33 Intro to Computer Systems XIX 28 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Receiving Arguments int main(int argc, char *argv[]) { 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 29 Copyright 2017 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 30 Copyright 2017 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 31 Copyright 2017 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 32 Copyright 2017 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 33 Copyright 2017 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 34 Copyright 2017 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 35 Copyright 2017 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 36 Copyright 2017 Thomas W. Doeppner. All rights reserved.