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

Similar documents
SE350: Operating Systems

The Programming Interface

Operating System Structure

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

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

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

COMP 3100 Operating Systems

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

Processes & Threads. (Chapter 3) CS 4410 Operating Systems. [R. Agarwal, L. Alvisi, A. Bracy, M. George, E. Sirer, R. Van Renesse]

Lecture 4: Memory Management & The Programming Interface

Interrupts, Fork, I/O Basics

Getting to know you. Anatomy of a Process. Processes. Of Programs and Processes

Process Management 1

Process Management! Goals of this Lecture!

Processes COMPSCI 386

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

UNIX Processes. by Armin R. Mikler. 1: Introduction

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

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

PROCESSES. Jo, Heeseung

Processes. Jo, Heeseung

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

CSC 1600 Unix Processes. Goals of This Lecture

CSC209 Fall Karen Reid 1

CSE 153 Design of Operating Systems Fall 2018

fork System-Level Function

Process Management! Goals of this Lecture!

Processes. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Princeton University Computer Science 217: Introduction to Programming Systems. Process Management

PROCESS MANAGEMENT. Operating Systems 2015 Spring by Euiseong Seo

Computer Systems Assignment 2: Fork and Threads Package

IC221: Systems Programming 12-Week Written Exam [SOLUTIONS]

Processes. CS 416: Operating Systems Design, Spring 2011 Department of Computer Science Rutgers University

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 20

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

W4118 Operating Systems. Junfeng Yang

Process Creation in UNIX

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

Process Management 1

(In columns, of course.)

Creating a Shell or Command Interperter Program CSCI411 Lab

CS153: Process 2. Chengyu Song. Slides modified from Harsha Madhyvasta, Nael Abu-Ghazaleh, and Zhiyun Qian

The Kernel. wants to be your friend

System Programming. Process Control II

A process. the stack

Princeton University. Computer Science 217: Introduction to Programming Systems. Process Management

Altering the Control Flow

Chapter 3 Process Description and Control

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

Fall 2015 COMP Operating Systems. Lab #3

CS 201. Processes. Gerson Robboy Portland State University

OS Structure, Processes & Process Management. Don Porter Portions courtesy Emmett Witchel

Processes. CS3026 Operating Systems Lecture 05

Processes. Johan Montelius KTH

518 Lecture Notes Week 3

SOFTWARE ARCHITECTURE 3. SHELL

Princeton University Computer Science 217: Introduction to Programming Systems. Process Management

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

What is a Process? Processes and Process Management Details for running a program

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

Fall 2017 :: CSE 306. Process Abstraction. Nima Honarmand

Operating systems and concurrency - B03

CS 550 Operating Systems Spring Process III

Altering the Control Flow

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

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.

Chap 4, 5: Process. Dongkun Shin, SKKU

University of Washington What is a process?

CS240: Programming in C

Process a program in execution; process execution must progress in sequential fashion. Operating Systems

CSC209: Software tools. Unix files and directories permissions utilities/commands Shell programming quoting wild cards files

CSC209: Software tools. Unix files and directories permissions utilities/commands Shell programming quoting wild cards files. Compiler vs.

Processes. Dr. Yingwu Zhu

What is a Process. Processes. Programs and Processes. System Classification 3/5/2013. Process: An execution stream and its associated state

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

Lecture 2: Processes. CSE 120: Principles of Opera9ng Systems. UC San Diego: Summer Session I, 2009 Frank Uyeda

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

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

Exceptional Control Flow Part I Oct. 17, 2002

ENGR 3950U / CSCI 3020U Midterm Exam SOLUTIONS, Fall 2012 SOLUTIONS

PROCESS CONTROL BLOCK TWO-STATE MODEL (CONT D)

Chapter 3: Process Concept

Chapter 3: Process Concept

Control Flow. Systemprogrammering 2007 Föreläsning 2 Exceptional Control Flow Part I. Exceptional Control Flow. Altering the Control Flow

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

CS240: Programming in C

Chapter 3: Process Concept

Operating System Labs. Yuanbin Wu

Process management 1

Operating Systems. Lecture 05

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

CS510 Operating System Foundations. Jonathan Walpole

September 2 nd, 2015 Prof. John Kubiatowicz

LSN 13 Linux Concurrency Mechanisms

CSC209 Review. Yeah! We made it!

Operating Systems Lab

CS 537 Lecture 2 - Processes

Lecture Topics. Announcements. Today: Threads (Stallings, chapter , 4.6) Next: Concurrency (Stallings, chapter , 5.

Kernel and processes

Transcription:

Structure

Unix architecture users Functions of the System tools (shell, editors, compilers, ) standard library System call Standard library (printf, fork, ) OS kernel: processes, memory management, file system, I/O Kernel Mode hardware User Mode

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

Programming Interface

Main Points Creating and managing processes fork, exec, wait Performing I/O open, read, write, close Communicating between processes pipe, dup, select, connect Example: implementing a shell

Shell A shell is a job control system Allows programmer to create and manage a set of programs to do some task Windows, MacOS, Linux all have shells Example: to compile a C program cc c sourcefile1.c cc c sourcefile2.c ln o program sourcefile1.o sourcefile2.o

Question If the shell runs at user-level, what system calls does it make to run each of the programs? Ex: cc, ln

Windows CreateProcess System call to create a new process to run a program Create and initialize the process control block (PCB) in the kernel Create and initialize a new address space Load the program into the address space Copy arguments into memory in the address space Initialize the hardware context to start execution at ``start' Inform the scheduler that the new process is ready to run

Windows CreateProcess API (simplified) if (!CreateProcess( ) NULL, argv[1], NULL, NULL, FALSE, // No module name (use command line) // Command line // Process handle not inheritable // Thread handle not inheritable // Set handle inheritance to FALSE 0, // No creation flags NULL, NULL, &si, &pi ) // Use parent's environment block // Use parent's starting directory // Pointer to STARTUPINFO structure // Pointer to PROCESS_INFORMATION structure

UNIX Process Management UNIX fork system call to create a copy of the current process, and start it running No arguments! UNIX exec system call to change the program being run by the current process UNIX wait system call to wait for a process to finish UNIX signal system call to send a notification to another process

UNIX fork() fork() is used to generate a child process: The father and its child share the same code The child process inherits a copy of the kernel and user data of the father father fork() child

UNIX fork() fork() does not take input params Returns an integer: For the child it is 0 For the father is: A positive value that represents the PID of the child A negative value that represents an error code

UNIX Process Management

Question: What does this code print? int child_pid = fork(); if (child_pid == 0) { // I'm the child process printf("i am process #%d\n", getpid()); return 0; } else { // I'm the parent process } printf("i am parent of process #%d\n", child_pid); return 0;

Questions Can UNIX fork() return an error? Why? Can UNIX exec() return an error? Why? Can UNIX wait() ever return immediately? Why?

Implementing UNIX fork Steps to implement UNIX fork Create and initialize the process control block (PCB) in the kernel Namely, a process structure and a user structure Create a new address space Initialize the address space with a copy of the entire contents of the address space of the parent Inherit the execution context of the parent (e.g., any open files) Inform the scheduler that the new process is ready to run

Implementing UNIX fork Addressing spaces of the father and the child after a successful fork copy 232-1 Stack 232-1 Stack free free heap Data heap Data 0 Text (shared) Father 0 Text (shared) Child

Implementing UNIX fork Addressing spaces of the father and the child after a successful fork 232-1 &x 45 Stack 232-1 &x 0 Stack free heap Data PC = instruction after fork free heap Data 0 Text (shared) Father (PID=34) 0 Text (shared) Child (PID=45)

Implementing UNIX fork Steps to implement UNIX fork Load the program into the current address space Copy arguments into memory in the address space Initialize the hardware context to start execution at ``start''

UNIX exec Replaces the code executed by a process Does not create a new process, inherits PBC and changes address space Replaces the data Example: int execl(char *pathname, char *arg0,.. char *argn, (char*)0) Pathname is the name of an executable file argn[1],..argn[ ] are the arguments passed to the program list terminated with (char *)0

UNIX exec If it s successful it does not return The process executes another program If it fails it returns an error code After exec the process: Keeps the PID Keeps the PCB (process and user structures) But it changes references to code and data memory Resets the pending signals Keeps the kernel stack Keeps the assigned resources (open files)

Process termination in UNIX A process can terminate: Because of an exception due to illegal actions By invoking the system call exit The terminated process returns an exit value to its father The father receives the value by the system call wait If the father didn t already call the wait, the terminated process switches to zombie state If the father is already terminated, the init process adopts its children

exit() and wait() void exit(int status); Status is the termination code exit never returns Frees memory, releases resources If it switches to zombie, keeps the PBC until the father invokes wait int wait(int *status); Status if the PID of the terminated process or an error code

UNIX I/O Uniformity All operations on all files, devices use the same set of system calls: open, close, read, write Open before use Open returns a handle (file descriptor) for use in later calls on the file Byte-oriented Kernel-buffered read/write Explicit close To garbage collect the open file descriptor

UNIX File System Interface UNIX file open is a Swiss Army knife: Open the file, return file descriptor Options: if file doesn t exist, return an error If file doesn t exist, create file and open it If file does exist, return an error If file does exist, open file If file exists but isn t empty, nix it then open If file exists but isn t empty, return an error

Interface Design Question Why not separate syscalls for open/create/exists? if (!exists(name)) create(name); // can create fail? fd = open(name); // does the file exist?

Implementing a Shell char *prog, **args; int child_pid; // Read and parse the input a line at a time while (readandparsecmdline(&prog, &args)) { child_pid = fork(); // create a child process if (child_pid == 0) { exec(prog, args); // I'm the child process. Run program // NOT REACHED } else { wait(child_pid); // I'm the parent, wait for child return 0; } }