UNIX Structure. Operating Systems In Depth VII 1 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Similar documents
This lecture is covered in Section 4.1 of the textbook.

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

CS 33. Files Part 2. CS33 Intro to Computer Systems XXI 1 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Process Creation in UNIX

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

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

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

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

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

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

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

File Descriptors and Piping

Logical disks. Bach 2.2.1

Disk divided into one or more partitions

Systems Programming. COSC Software Tools. Systems Programming. High-Level vs. Low-Level. High-Level vs. Low-Level.

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

3/26/2014. Contents. Concepts (1) Disk: Device that stores information (files) Many files x many users: OS management

Processes COMPSCI 386

UNIX System Calls. Sys Calls versus Library Func

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

Process Management! Goals of this Lecture!

CSC209H Lecture 6. Dan Zingaro. February 11, 2015

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

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

Hyo-bong Son Computer Systems Laboratory Sungkyunkwan University

Basic OS Progamming Abstrac7ons

Basic OS Progamming Abstrac2ons

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

SE350: Operating Systems

Outline. Relationship between file descriptors and open files

Virtual File System. Don Porter CSE 306

Processes. Processes (cont d)

CS240: Programming in C

Lecture files in /home/hwang/cs375/lecture05 on csserver.

Computer Programming Lecture 1 이윤진서울대학교

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

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst

Process Management 1

The bigger picture. File systems. User space operations. What s a file. A file system is the user space implementation of persistent storage.

CS 25200: Systems Programming. Lecture 14: Files, Fork, and Pipes

Recitation 8: Tshlab + VM

Intel P The course that gives CMU its Zip! P6/Linux Memory System November 1, P6 memory system. Review of abbreviations

File I/0. Advanced Programming in the UNIX Environment

Section 2: Processes

UNIVERSITY OF NEBRASKA AT OMAHA Computer Science 4500/8506 Operating Systems Fall Programming Assignment 1 (updated 9/16/2017)

Input and Output System Calls

CS240: Programming in C

Process Management! Goals of this Lecture!

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

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

I/O OPERATIONS. UNIX Programming 2014 Fall by Euiseong Seo

Inter-Process Communication

COP4342 UNIX Tools Assignment #3: A Simple Unix Shell. Instructor: Dr. Robert Van Engelen Teaching Assistant: Imran Chowdhury Spring 2018

P6 memory system P6/Linux Memory System October 31, Overview of P6 address translation. Review of abbreviations. Topics. Symbols: ...

CSE 410: Systems Programming

SOFTWARE ARCHITECTURE 3. SHELL

Page Which had internal designation P5

The Classical OS Model in Unix

A2 Design Considerations. CS161, Spring

Housekeeping (Lecture 12-6/26/2018)

Intel P6 (Bob Colwell s Chip, CMU Alumni) The course that gives CMU its Zip! Memory System Case Studies November 7, 2007.

I/O OPERATIONS. UNIX Programming 2014 Fall by Euiseong Seo

CSI 402 Lecture 11 (Unix Discussion on Files continued) 11 1 / 19

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

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

CMSC 216 Introduction to Computer Systems Lecture 17 Process Control and System-Level I/O

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

UNIX. Session 2. UNIX processes and forking fork system call exec system call death of process kill fork and I/O using it 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.

PROCESSES. Jo, Heeseung

Processes. Jo, Heeseung

UNIX System Programming. Overview. 1. A UNIX System. 2. Processes (review) 2.1. Context. Pipes/FIFOs

CS2028 -UNIX INTERNALS

UNIX File Management (continued)

Last Week: ! Efficiency read/write. ! The File. ! File pointer. ! File control/access. This Week: ! How to program with directories

CS167 Programming Assignment 1: Shell

Applications of. Virtual Memory in. OS Design

Operating System Labs. Yuanbin Wu

CSE 333 SECTION 3. POSIX I/O Functions

Files and Directories

Chp1 Introduction. Introduction. Objective. Logging In. Shell. Briefly describe services provided by various versions of the UNIX operating system.

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

Unix File and I/O. Outline. Storing Information. File Systems. (USP Chapters 4 and 5) Instructor: Dr. Tongping Liu

Lab 4. Out: Friday, February 25th, 2005

Advanced Unix/Linux System Program. Instructor: William W.Y. Hsu

everything is a file main.c a.out /dev/sda1 /dev/tty2 /proc/cpuinfo file descriptor int

Memory System Case Studies Oct. 13, 2008

Midterm Exam CPS 210: Operating Systems Spring 2013

Processes. What s s a process? process? A dynamically executing instance of a program. David Morgan

RCU. ò Walk through two system calls in some detail. ò Open and read. ò Too much code to cover all FS system calls. ò 3 Cases for a dentry:

Processes in linux. What s s a process? process? A dynamically executing instance of a program. David Morgan. David Morgan

VFS, Continued. Don Porter CSE 506

Pentium/Linux Memory System March 17, 2005

PROCESS MANAGEMENT. Operating Systems 2015 Spring by Euiseong Seo

What Is Operating System? Operating Systems, System Calls, and Buffered I/O. Academic Computers in 1983 and Operating System

CSCE 313 Introduction to Computer Systems. Instructor: Dezhen Song

Virtual File System. Don Porter CSE 506

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

Solutions to the first midterm. COSC 4330/6310 Summer 2013

Operating Systems. Lecture 05

Transcription:

UNIX Structure Operating Systems In Depth VII 1 Copyright 2018 Thomas W. Doeppner. All rights reserved.

The Unix Address Space stack dynamic bss data text Operating Systems In Depth VII 2 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Creating a Process: Before fork( ) parent process Operating Systems In Depth VII 3 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Creating a Process: After fork( ) // returns p parent process fork( ) // returns 0 child process (pid = p) Operating Systems In Depth VII 4 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Fork and Wait short pid; if ((pid = fork()) == 0) { /* some code is here for the child to execute */ exit(n); } else { int ReturnCode; while(pid!= wait(&returncode)) ; /* the child has terminated with ReturnCode as its return code */ } Operating Systems In Depth VII 5 Copyright 2018 Thomas W. Doeppner. All rights reserved.

PID Process Control Blocks Terminated children Link Return code Other Stuff Process Control Block PID Terminated children Link Return code Other Stuff PID Terminated children Link Return code Other Stuff Operating Systems In Depth VII 6 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Exec int pid; if ((pid = fork()) == 0) { } /* we ll soon discuss what might take place before exec is called */ execl("/home/twd/bin/primes", "primes", "300", 0); exit(1); /* parent continues here */ while(pid!= wait(0)) /* ignore the return code */ ; Operating Systems In Depth VII 7 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Loading a New Image args exec(prog, args) Before prog s bss prog s data prog s text After Operating Systems In Depth VII 8 Copyright 2018 Thomas W. Doeppner. All rights reserved.

int A, B, C, D; A=1; if (fork() > 0) { B=1; A=111; } else { C=2; } if (fork() > 0) { D=222; } else { } D=A+B+C; // what value is now // in D for this process? exit(0); Quiz 1 Answer: a) 0 b) 3 c) 113 d) indeterminate Operating Systems In Depth VII 9 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Representing the Address Space Important component of a process is its address space how is it represented? Can page tables represent a process s address space? Operating Systems In Depth VII 10 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Simple User Address Space stack bss & dynamic data text Operating Systems In Depth VII 11 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Address-Space Representation Somewhat Simplified task_struct mm_struct vm_area_struct 0 7fff x, shared vm_area_struct 8000 1afff rw, private vm_area_struct 1b000 1bfff rw, private vm_area_struct 7fffd000 7fffffff rw, private struct file Operating Systems In Depth VII 12 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Adding a Mapped File stack mapped file bss & dynamic data text Operating Systems In Depth VII 13 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Address-Space Representation: More Areas task_struct mm_struct vm_area_struct 0 7fff x, shared vm_area_struct 8000 1afff rw, private vm_area_struct 1b000 1bfff rw, private vm_area_struct 200000 201fff rw, shared vm_area_struct 7fffd000 7fffffff rw, private struct file struct file Operating Systems In Depth VII 14 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Adding More Stuff stack 1 stack 2 stack 3 mapped file 1 mapped file 2 mapped file 3. mapped file 117 bss & dynamic data text Operating Systems In Depth VII 15 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Address-Space Representation: Reality task_struct mm_struct 200000 201fff 1b000 1bfff 202000 203fff 0 7fff 8000 1afff 204000 204fff 7fffd000 7fffffff 208000 210fff Operating Systems In Depth VII 16 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Layering User Applications System Calls User Kernel Virtual File System (VFS) disk file systems (e.g. S5FS) terminals networking other things (e.g. /proc) Operating Systems In Depth VII 17 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Quiz 2 int fd1 = open("file", O_CREAT O_RDWR, 0666); unlink("file"); write(fd1, "123", 3); int fd2 = open("file", O_CREAT O_RDWR, 0666); write(fd2, "4", 1); if (fork() == 0) { write(fd1, "5", 1); } exit(0); The final contents of file are: a) 12345 b) 453 c) 45 d) 4 Operating Systems In Depth VII 18 Copyright 2018 Thomas W. Doeppner. All rights reserved.

File-Descriptor Table File-descriptor table File descriptor 0 1 2 3.. ref count access mode file location inode pointer. User address space n 1 Kernel address space Operating Systems In Depth VII 19 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Allocation of File Descriptors Whenever a process requests a new file descriptor, the lowest numbered file descriptor not already associated with an open file is selected; thus #include <fcntl.h> #include <unistd.h> close(0); fd = open("file", O_RDONLY); will always associate file with file descriptor 0 (assuming that the open succeeds) Operating Systems In Depth VII 20 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Redirecting Output Twice if (fork() == 0) { /* set up file descriptors 1 and 2 in the child process */ } close(1); close(2); if (open("/home/twd/output", O_WRONLY) == -1) { } exit(1); if (open("/home/twd/output", O_WRONLY) == -1) { exit(1); } execl("/home/twd/bin/program", "program", 0); exit(1); /* parent continues here */ Operating Systems In Depth VII 21 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Redirected Output File descriptor 1 File-descriptor table 1 WRONLY 0 inode pointer File descriptor 2 1 WRONLY 0 inode pointer User address space Kernel address space Operating Systems In Depth VII 22 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Redirected Output After Write File descriptor 1 File-descriptor table 1 WRONLY 100 inode pointer File descriptor 2 1 WRONLY 0 inode pointer User address space Kernel address space Operating Systems In Depth VII 23 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Sharing Context Information if (fork() == 0) { /* set up file descriptors 1 and 2 in the child process */ close(1); close(2); if (open("/home/twd/output", O_WRONLY) == -1) { } exit(1); dup(1); /* set up file descriptor 2 as a duplicate of 1 */ execl("/home/twd/bin/program", "program", 0); exit(1); } /* parent continues here */ Operating Systems In Depth VII 24 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Redirected Output After Dup File descriptor 1 File descriptor 2 File-descriptor table 2 WRONLY 100 inode pointer User address space Kernel address space Operating Systems In Depth VII 25 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Fork and File Descriptors int logfile = open("log", O_WRONLY); if (fork() == 0) { } /* child process computes something, then does: */ write(logfile, LogEntry, strlen(logentry)); exit(0); /* parent process computes something, then does: */ write(logfile, LogEntry, strlen(logentry)); Operating Systems In Depth VII 26 Copyright 2018 Thomas W. Doeppner. All rights reserved.

File Descriptors After Fork logfile Parent s address space 2 WRONLY 0 inode pointer logfile Child s address space Kernel address space Operating Systems In Depth VII 27 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Directories unix etc home pro dev passwd motd twd unix... slide1 slide2 Operating Systems In Depth VII 28 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Directory Representation Component Name directory entry Inode Number. 1.. 1 unix 117 etc 4 home 18 pro 36 dev 93 Operating Systems In Depth VII 29 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Hard Links unix etc home pro dev twd image motd unix... slide1 slide2 % ln /unix /etc/image # link system call Operating Systems In Depth VII 30 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Directory Representation. 1.. 1 unix 117 etc 4 home 18 pro 36 dev 93. 4.. 1 image 117 motd 33 Operating Systems In Depth VII 31 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Soft Links unix etc home pro dev twd image twd unix... mylink /home/twd % ln s /unix /home/twd/mylink % ln s /home/twd /etc/twd # symlink system call slide1 slide2 /unix Operating Systems In Depth VII 32 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Working Directory Maintained in kernel for each process paths not starting from / start with the working directory changed by use of the chdir system call displayed (via shell) using pwd - how is this done? Operating Systems In Depth VII 33 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Mount Points (1) unix etc usr mnt dev tty01 tty02 dsk1 dsk2 tp1 src lib bin Operating Systems In Depth VII 34 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Mount Points (2) unix etc usr mnt dev tty01 tty02 dsk1 dsk2 tp1 src lib bin mount /dev/dsk2 /usr Operating Systems In Depth VII 35 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Representing File Systems class fs { char dev[str_max]; char mountpt[str_max]; vnode *vnodecovered; vnode *root; virtual void read_vnode(vnode *); virtual void delete_vnode(vnode *); }; // device containing the f.s. // where the f.s. is mounted // file on which f.s. is mounted // root of the f.s. Operating Systems In Depth VII 36 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Representing Files class vnode { unsigned short refcount; fs *vfsmounted; fs *vfs; unsigned long vno; int mode; int len; link_list_t link; kmutex_t mutex; virtual int create(const char *, int, vnode **); virtual int read(int, void *, int); virtual int write(int, const void *, int); }; Operating Systems In Depth VII 37 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Mounting a File System (1) /dev/disk0 / dev mountpt vnodecovered vfs_root_vn root [/] [/a/b] Operating Systems In Depth VII 38 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Mounting a File System (2) /dev/disk0 / /dev/disk1 /a/b vfs_root_vn [/] [/a/b] [/a/b] [/a/b/ ] Operating Systems In Depth VII 39 Copyright 2018 Thomas W. Doeppner. All rights reserved.

But Wait What s this about C++? real operating systems are written in C Operating Systems In Depth VII 40 Copyright 2018 Thomas W. Doeppner. All rights reserved.

fs class fs { char dev[str_max]; }; char mountpt[str_max]; vnode *vnodecovered; vnode *root; virtual void read_vnode(vnode *); virtual void delete_vnode(vnode *); typedef struct fs { char fs_dev[str_max]; char fs_mountpt[str_max]; struct vnode *fs_vnodecovered; struct vnode *fs_root; fs_ops_t *fs_op; /* function pointers */ void *fs_i; /* extra stuff in subclasses */ } fs_t; Operating Systems In Depth VII 41 Copyright 2018 Thomas W. Doeppner. All rights reserved.

vnode class vnode { unsigned short refcount; fs *vfsmounted; fs *vfs; unsigned long vno; int mode; int len; link_list_t link; kmutex_t mutex; virtual int create(const char *, int, vnode **); virtual int read(int, void *, int); virtual int write(int, const void *, int); }; typedef struct vnode { unsigned short vn_refcount; struct fs *vn_vfsmounted; struct fs *vn_vfs; unsigned long vn_vno; int vn_mode; int vn_len; link_list_t vn_link; kmutex_t vn_mutex; struct vnode_ops *vn_op; /* function pointers */ void *vn_i; /* extra stuff in subclasses */ } Operating Systems In Depth VII 42 Copyright 2018 Thomas W. Doeppner. All rights reserved.