Basic OS Programming Abstractions (and Lab 1 Overview)

Similar documents
Basic OS Progamming Abstrac2ons

Basic OS Progamming Abstrac7ons

Signals and Inter-Process Communica.on

CSE 506: Opera.ng Systems Signals and Inter-Process Communica.on

Process Creation in UNIX

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

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

File Descriptors and Piping

Virtual File System. Don Porter CSE 306

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

Processes often need to communicate. CSCB09: Software Tools and Systems Programming. Solution: Pipes. Recall: I/O mechanisms in C

CSci 4061 Introduction to Operating Systems. IPC: Basics, Pipes

Unix Processes. What is a Process?

Operating Systems. Lecture 06. System Calls (Exec, Open, Read, Write) Inter-process Communication in Unix/Linux (PIPE), Use of PIPE on command line

Fall 2017 :: CSE 306. File Systems Basics. Nima Honarmand

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

W4118 Operating Systems. Junfeng Yang

Virtual File System. Don Porter CSE 506

Processes COMPSCI 386

Process Management! Goals of this Lecture!

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

RCU. ò Dozens of supported file systems. ò Independent layer from backing storage. ò And, of course, networked file system support

Outline. OS Interface to Devices. System Input/Output. CSCI 4061 Introduction to Operating Systems. System I/O and Files. Instructor: Abhishek Chandra

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

Recitation 8: Tshlab + VM

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

UNIX Kernel. UNIX History

Process Management 1

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

CSE 333 SECTION 3. POSIX I/O Functions

The Process Abstraction. CMPU 334 Operating Systems Jason Waterman

CSC209H Lecture 6. Dan Zingaro. February 11, 2015

Applications of. Virtual Memory in. OS Design

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

CS 471 Operating Systems. Yue Cheng. George Mason University Fall 2017

Assignment 1. Teaching Assistant: Michalis Pachilakis (

OS COMPONENTS OVERVIEW OF UNIX FILE I/O. CS124 Operating Systems Fall , Lecture 2

IPC and Unix Special Files

Operating System Structure

Pipes and FIFOs. Woo-Yeong Jeong Computer Systems Laboratory Sungkyunkwan University

CSE 410: Systems Programming

UNIX System Calls. Sys Calls versus Library Func

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

Process Management! Goals of this Lecture!

SOFTWARE ARCHITECTURE 3. SHELL

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 471 Operating Systems. Yue Cheng. George Mason University Fall 2017

Hyo-bong Son Computer Systems Laboratory Sungkyunkwan University

Contents. IPC (Inter-Process Communication) Representation of open files in kernel I/O redirection Anonymous Pipe Named Pipe (FIFO)

File Systems. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

Computer Science 330 Operating Systems Siena College Spring Lab 5: Unix Systems Programming Due: 4:00 PM, Wednesday, February 29, 2012

CS2028 -UNIX INTERNALS

Inter-Process Communication

PROCESS CONTROL BLOCK TWO-STATE MODEL (CONT D)

CSC374, Spring 2010 Lab Assignment 1: Writing Your Own Unix Shell

System Calls and Signals: Communication with the OS. System Call. strace./hello. Kernel. Context Switch

ESE 333 Real-Time Operating Systems 2 What is an operating system? Two views: 1. Top-down view: Extended machine ffl Covers the details of the hardwar

A process. the stack

Introduction to Operating Systems Prof. Chester Rebeiro Department of Computer Science and Engineering Indian Institute of Technology, Madras

Layers in a UNIX System. Create a new process. Processes in UNIX. fildescriptors streams pipe(2) labinstructions

Interrupts, Fork, I/O Basics

CS 201. Files and I/O. Gerson Robboy Portland State University

Operating System Labs. Yuanbin Wu

SE350: Operating Systems

CSE506: Operating Systems CSE 506: Operating Systems

Linux Operating System

CS370 Operating Systems

Section 3: File I/O, JSON, Generics. Meghan Cowan

CSE 333 SECTION 3. POSIX I/O Functions

Processes. Processes (cont d)

Processes. Johan Montelius KTH

Lecture 23: System-Level I/O

Preview. Interprocess Communication with Pipe. Pipe from the Parent to the child Pipe from the child to the parent FIFO popen() with r Popen() with w

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

System Calls & Signals. CS449 Spring 2016

Contents. PA1 review and introduction to PA2. IPC (Inter-Process Communication) Exercise. I/O redirection Pipes FIFOs

CSE 153 Design of Operating Systems Fall 2018

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

CS 326: Operating Systems. Process Execution. Lecture 5

UNIX File Hierarchy: Structure and Commands

Inter-Process Communication. Disclaimer: some slides are adopted from the book authors slides with permission 1

CS 5460/6460 Operating Systems

Design Overview of the FreeBSD Kernel CIS 657

Data and File Structures Chapter 2. Basic File Processing Operations

NETWORK AND SYSTEM PROGRAMMING

Design Overview of the FreeBSD Kernel. Organization of the Kernel. What Code is Machine Independent?

CS240: Programming in C

Fall 2015 COMP Operating Systems. Lab #3

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.

Inter-process communication (IPC)

(MCQZ-CS604 Operating Systems)

EECS 482 Introduction to Operating Systems

Problem Set: Processes

CS 4410, Fall 2017 Project 1: My First Shell Assigned: August 27, 2017 Due: Monday, September 11:59PM

ECE 650 Systems Programming & Engineering. Spring 2018

PROCESS MANAGEMENT. Operating Systems 2015 Spring by Euiseong Seo

CS Operating Systems Lab 3: UNIX Processes

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

CS 550 Operating Systems Spring Inter Process Communication

Transcription:

Basic OS Programming Abstractions (and Lab 1 Overview) Don Porter Portions courtesy Kevin Jeffay 1

Recap We ve introduced the idea of a process as a container for a running program This lecture: Introduce key OS APIs for a process Some may be familiar from lab 0 Some will help with lab 1

Lab 1: A (Not So) Simple Shell Last year: Most of the lab focused on just processing input and output Kind of covered in lab 0 I m giving you some boilerplate code that does basics Reminder: demo My goal: Get some experience using process APIs Most of what you will need discussed in this lecture You will incrementally improve the shell 3

Tasks Turn input into commands; execute those commands Support PATH variables Be able to change directories Print the working directory at the command line Add debugging support Add variables and scripting support Pipe indirection: <, >, and Job control (background & foreground execution) goheels draw an ASCII art Tar Heel Significantly more work than Lab 0 start early! 4

Outline Fork recap Files and File Handles Inheritance Pipes Sockets Signals Synthesis Example: The Shell

Process Creation: fork/join in Linux The execution context for the child process is a copy of the parent s context at the time of the call main { int childpid; S 1 ; fork() childpid = fork(); if(childpid == 0) <code for child process> else { <code for parent process> wait(); } Code Data Stack childpid = xxx childpid = 0 Code Data Stack S 2 ; } Parent Child

Process Creation: exec in Linux exec allows a process to replace itself with another program (The contents of another binary file) foo: main { S } exec() a.out: main { S 0 exec(foo) S 1 S 2 } Code Data Stack Memory Context

Process Creation: Abstract fork in Linux The original fork semantics can be realized in Linux via a (UNIX) fork followed by an exec main { int childpid; S 1 ; fork() exec() childpid = fork(); if(childpid == 0) exec(filename) else { <code for parent process> wait(); } S 2 ; } Code Data Stack Parent Code Data Stack Child main { S }. /foo

2 Ways to Refer to a File Path, or hierarchical name, of the file Absolute: /home/porter/foo.txt Starts at system root Relative: foo.txt Assumes file is in the program s current working directory Handle to an open file Handle includes a cursor (offset into the file)

Path-based calls Functions that operate on the directory tree Rename, unlink (delete), chmod (change permissions), etc. Open creates a handle to a file intopen (char *path, intflags, mode_t mode); Flags include O_RDONLY, O_RDWR, O_WRONLY Permissions are generally checked only at open Opendir variant for a directory

Handle-based calls ssize_t read (int fd, void *buf, size_t count) Fd is the handle Buf is a user-provided buffer to receive count bytes of the file Returns how many bytes read ssize_twrite(int fd, void *buf, size_tcount) Same idea, other direction int close (int fd) Close an open file

PC Example char buf[9]; int fd = open ( foo.txt, O_RDWR); ssize_t bytes = read(fd, buf, 8); if (bytes!= 8) // handle the error memcpy(buf, Awesome, 7); buf[7] = \0 ; bytes = write(fd, buf, 8); if (bytes!= 8) // error close(fd); Handle 3 Awesome\0 fd: 3 bytes: 8 buf User-level stack Kernel Contents\0 Awesome Contents foo.txt

But what is a handle? A reference to an open file or other OS object For files, this includes a cursor into the file In the application, a handle is just an integer This is an offset into an OS-managed table

Handles can Logical be View shared Virtual Address Space Hello! Foo.txt inode 50 Handle Table Process A Handle indices are processspecific 20 Process B Disk Handle Table Process C

Handle Recap Every process has a table of pointers to kernel handle objects E.g., a file handle includes the offset into the file and a pointer to the kernel-internal file representation (inode) Application s can t directly read these pointers Kernel memory is protected Instead, make system calls with the indices into this table Index is commonly called a handle

Rearranging the table The OS picks which index to use for a new handle An application explicitly copy an entry to a specific index with dup2(old, new) Be careful if new is already in use

Other useful handle APIs mmap() can map part or all of a file into memory seek() adjust the cursor position of a file Like rewinding a cassette tape

Outline Files and File Handles Inheritance Pipes Sockets Signals Synthesis Example: The Shell

Inheritance By default, a child process gets a reference to every handle the parent has open Very convenient Also a security issue: may accidentally pass something the program shouldn t Between fork() and exec(), the parent has a chance to clean up handles it doesn t want to pass on See also CLOSE_ON_EXEC flag

Standard in, out, error Handles 0, 1, and 2 are special by convention 0: standard input 1: standard output 2: standard error (output) Command-line programs use this convention Parent program (shell) is responsible to use open/close/dup2 to set these handles appropriately between fork() and exec()

int pid = fork(); if (pid == 0) { } // Example int input = open ( in.txt, O_RDONLY); dup2(input, 0); exec( grep, quack );

Outline Files and File Handles Inheritance Pipes Sockets Signals Synthesis Example: The Shell

Pipes FIFO stream of bytes between two processes Read and write like a file handle But not anywhere in the hierarchical file system And not persistent And no cursor or seek()-ing Actually, 2 handles: a read handle and a write handle Primarily used for parent/child communication Parent creates a pipe, child inherits it

Example PC PC int pipe_fd[2]; int rv = pipe(pipe_fd); int pid = fork(); if (pid == 0) { W Virtual Address Space Handle Table Parent } else { close(pipe_fd[1]); dup2(pipe_fd[0], 0); close(pipe_fd[0]); exec( grep, quack ); R Child close (pipe_fd[0]);...

Sockets Similar to pipes, except for network connections Setup and connection management is a bit trickier A topic for another day (or class)

Select What if I want to block until one of several handles has data ready to read? Read will block on one handle, but perhaps miss data on a second Select will block a process until a handle has data available Useful for applications that use pipes, sockets, etc.

Outline Files and File Handles Inheritance Pipes Sockets Signals Synthesis Example: The Shell

Signals Similar concept to an application-level interrupt Unix-specific (more on Windows later) Each signal has a number assigned by convention Just like interrupts Application specifies a handler for each signal OS provides default If a signal is received, control jumps to the handler If process survives, control returns back to application

Can occur for: Signals, cont. Exceptions: divide by zero, null pointer, etc. IPC: Application-defined signals (USR1, USR2) Control process execution (KILL, STOP, CONT) Send a signal using kill(pid, signo) Killing an errant program is common, but you can also send a non-lethal signal using kill() Use signal() or sigaction() to set the handler for a signal

How signals work Although signals appear to be delivered immediately They are actually delivered lazily Whenever the OS happens to be returning to the process from an interrupt, system call, etc. So if I signal another process, the other process may not receive it until it is scheduled again Does this matter?

More details When a process receives a signal, it is added to a pending mask of pending signals Stored in PCB Just before scheduling a process, the kernel checks if there are any pending signals If so, return to the appropriate handler Save the original register state for later When handler is done, call sigreturn() system call Then resume execution

Laziness rules! Not on homework But in system design Meta-lesson Procrastinating on work in the system often reduces overall effort Signals: Why context switch immediately when it will happen soon enough?

Language Exceptions Signals are the underlying mechanism for Exceptions and catch blocks JVM or other runtime system sets signal handlers Signal handler causes execution to jump to the catch block

Windows comparison Exceptions have specific upcalls from the kernel to ntdll IPC is done using Events Shared between processes Handle in table No data, only 2 states: set and clear Several variants: e.g., auto-clear after checking the state

Outline Files and File Handles Inheritance Pipes Sockets Signals Synthesis Example: The Shell

Shell Recap Almost all commands are really binaries /bin/ls Key abstraction: Redirection over pipes >, <, and implemented by the shell itself

Shell Example Ex: ls grep foo Shell pseudocde: thsh fork() while(eof!= read_input) { parse_input(); // Sets up chain of pipes // Forks and exec s ls and grep separately // Wait on output from grep, print to console csh thsh ls exec(ls) } // print console prompt

A note on Lab 1 thsh wait() fork() csh thsh ls exec(ls) You re going to be creating lots of processes in this assignment If you fork a process and it never terminates You ve just created a Z O M B I E P R O C E S S!! Zombie s will fill up the process table in the Linux kernel Nobody can create a new process This means no one can launch a shell to kill the zombies!

A note on Lab 1 thsh wait() fork() csh thsh ls exec(ls) Be safe! Limit the number of processes you can create add the command limit maxproc 10 to the file ~/.cshrc (remember to delete this line at the end of the course!) Periodically check for and KILL! zombie processes ps -ef egrep -e PID -e YOUR-LOGIN-NAME kill pid-number Read the HW handout carefully for zombie-hunting details!

What about Ctrl-Z? Shell really uses select() to listen for new keystrokes (while also listening for output from subprocess) Special keystrokes are intercepted, generate signals Shell needs to keep its own scheduler for background processes Assigned simple numbers like 1, 2, 3 fg 3 causes shell to send a SIGCONT to suspended child

Other hints Splice(), tee(), and similar calls are useful for connecting pipes together Avoids copying data into and out-of application

Collaboration Policy Reminder You can work alone or in a pair Can be different from lab 0 Every line of code handed in must be written by one of the pair (or the boilerplate) No sharing code with other groups No code from Internet Any other collaboration must be acknowledged in writing High-level discussion is ok (no code) See written assignment and syllabus for more details Not following these rules is an Honor Code violation 42

Summary Understand how handle tables work Survey basic APIs Understand signaling abstraction Intuition of how signals are delivered Be prepared to start writing your shell in lab 1!