SOFTWARE ARCHITECTURE 3. SHELL

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

System Programming. Introduction to Unix

Process Management 1

Operating System Labs. Yuanbin Wu

The Shell, System Calls, Processes, and Basic Inter-Process Communication

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

Unix Processes. What is a Process?

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

Process Management! Goals of this Lecture!

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

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

Fall 2015 COMP Operating Systems. Lab #3

UNIX. Session 2. UNIX processes and forking fork system call exec system call death of process kill fork and I/O using it UNIX.

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

Processes. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Table of contents. Our goal. Notes. Notes. Notes. Summer June 29, Our goal is to see how we can use Unix as a tool for developing programs

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

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

CS240: Programming in C

Implementation of a simple shell, xssh

Assignment 1. Teaching Assistant: Michalis Pachilakis (

UNIX Kernel. UNIX History

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

PROCESSES. Jo, Heeseung

Processes. Jo, Heeseung

Operating System Structure

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

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

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

PROCESS MANAGEMENT. Operating Systems 2015 Spring by Euiseong Seo

CSC 1600 Unix Processes. Goals of This Lecture

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

Pipes. Pipes Implement a FIFO. Pipes (cont d) SWE 545. Pipes. A FIFO (First In, First Out) buffer is like a. Pipes are uni-directional

CSC209F Midterm (L5101) Fall 1998 University of Toronto Department of Computer Science

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.

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 20

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

The Classical OS Model in Unix

csci3411: Operating Systems

The Process Abstraction. CMPU 334 Operating Systems Jason Waterman

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

Process Management! Goals of this Lecture!

Processes. Processes (cont d)

Processes COMPSCI 386

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

Process Creation in UNIX

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

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

Chapter 9. Shell and Kernel

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

SOFTWARE ARCHITECTURE 2. FILE SYSTEM. Tatsuya Hagino lecture URL.

518 Lecture Notes Week 3

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

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.

Chapter 1. Introduction

Introduction to UNIX. Logging in. Basic System Architecture 10/7/10. most systems have graphical login on Linux machines

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

Creating a Shell or Command Interperter Program CSCI411 Lab

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

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

5/20/2007. Touring Essential Programs

CS61 Scribe Notes Date: Topic: Fork, Advanced Virtual Memory. Scribes: Mitchel Cole Emily Lawton Jefferson Lee Wentao Xu

Introduction to the Shell

UNIX COMMANDS AND SHELLS. UNIX Programming 2015 Fall by Euiseong Seo

CST Algonquin College 2

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

Operating Systems. Lecture 07. System Calls, Input/Output and Error Redirection, Inter-process Communication in Unix/Linux

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

bash, part 3 Chris GauthierDickey

CS Operating Systems Lab 3: UNIX Processes

CSE 153 Design of Operating Systems Fall 2018

Programming Assignment #1: A Simple Shell

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

628 Lecture Notes Week 4

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

Linux shell scripting Getting started *

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

CS510 Operating System Foundations. Jonathan Walpole

CSC209 Fall Karen Reid 1

CSC209 Review. Yeah! We made it!

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

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

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

Assignment clarifications

Killing Zombies, Working, Sleeping, and Spawning Children

The UNIX Shells. Computer Center, CS, NCTU. How shell works. Unix shells. Fetch command Analyze Execute

SE350: Operating Systems

Pipelines, Forks, and Shell

Chap 4, 5: Process. Dongkun Shin, SKKU

CS 550 Operating Systems Spring Process III

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

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

COMP 4/6262: Programming UNIX

3/8/2017. Unix/Linux Introduction. In this part, we introduce. What does an OS do? Examples

CSI Module 2: Processes

Operating Systemss and Multicore Programming (1DT089)

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

Lab 2: Implementing a Shell COMPSCI 310: Introduction to Operating Systems

Transcription:

1 SOFTWARE ARCHITECTURE 3. SHELL Tatsuya Hagino hagino@sfc.keio.ac.jp slides URL https://vu5.sfc.keio.ac.jp/sa/login.php

2 Software Layer Application Shell Library MIddleware Shell Operating System Hardware

3 Functions of Shell Start programs Control running programs background foreground Input/output redirection for programs pipe Set environment variables and shell variables Expanding wild cards for command line History of commands Alias for commands File name completion Repeat and conditional execution Shell script Starting and Controlling Programs Setting up execution environment Useful functions Running scripts

4 Shells Windows Command Prompt PowerShell Explorer Mac Finder Launcher UNIX sh Bourne Shell Korn Shell Z Shell csh C Shell TENEX C Shell App App User Shell OS Kernel App Shell between OS and user

5 UNIX Shell Commands Shell communicates your instruction to OS Shell OS Very few built-in commands set, alias, cd, setenv, etc. Most of commands are ordinary programs. ls is a program to list files in a directory. cat is a program to output contents of files.

6 Shell Command Processing 1 2 3 4 shell() { char buf[512], char *argv[512]; for (;;) { printf("% "); if (!fgets(buf, sizeof(buf), stdin)) break; parse(buf, argv); execute(argv[0], argv); 1. Write a prompt. 2. Read one line from a terminal. 3. Separate the line into a command and its arguments separated by spaces. 4. Execute the command with the arguments. 5. Back to the next input.

7 Execute a Program Create a child and execute a program. execute(char *cmd, char *argv[]) { int pid, status; pid = fork(); if (pid == 0) { execve(cmd, argv, NULL); fprintf(stderr, "command %s not found n", cmd); exit(1); while (wait(&status)!= pid); Shell is the parent and waits for the child to finish.

8 Process Running state of a program Multiple es may run the same program. Each consists of: program CPU state (registers, PC, SP) data memory space file descriptors other related states CPU state file descriptors etc. program code data Heap stack memory space

9 Process related System calls fork Create a child. Exact copy of the parent exec Specify a program to execute. Current program is destroyed and replaced with the new one. wait Wait until a child terminates. exit Terminate the current program. parent running abc child running abc fork exec wait child running xyz destroyed exit other system calls signal Specify a handler for each interrupt. kill Send an interrupt. parent-child relation in es

10 fork and exec fork Inherit all from the parent. memory space program data stack file descriptors environment variables Memory spaces are not shared, but copied. copy-on-write If either one changes, it is actually copied. exec Independence from the parent Destroy the current content of the memory. Replace it with the new program image. Start the new program from its entry point. main What inherits. file descriptors arguments to exec including environment variables The program image is not loaded into the memory immediately. demand paging loaded when necessary

11 How fork works 1. Create a new and make it a child. 2. Copy CPU state. registers, PC, SP, etc. 3. Copy memory contents. use copy-on-write sharing 4. Copy file descriptors. 5. Make the new ready-to-run (or runnable). 6. Return to where fork is called. CPU parent CPU CPU CPU CPU file descriptors parent parent child parent child CPU child file descriptors file descriptors file descriptors file descriptors file descriptors

12 How exec works 1. Allocate a temporary memory space. 2. Copy the command arguments and the environment variables to the temporary memory space. 3. Release all the memory of the current and create an empty memory space. 4. Set up demand paging of the given program on the new memory space. 5. Copy the command arguments and the environment variables from the temporary memory space to the stack in the new memory space. 6. Release the temporary memory space. 7. Set CPU PC to the entry point. HDD SSD CPU program entry point CPU PC program program command arguments environment variables command arguments environment variables command arguments environment variables file descriptors file descriptors file descriptors file descriptors

13 Command PATH The first argument of execve is the path name of the command. execve("ls",...) does not work. execve("/bin/ls",...) does work. Writing full path name is tedious. Use an environment variable PATH which contains a list of directories where commands are. /bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin Try each directory to find the command. execute(char *cmd, char *argv[]) { int pid, status; pid = fork(); if (pid == 0) { execve("/bin/" + cmd, args, NULL); execve("/usr/bin/" + cmd, args, NULL); execve("/sbin/" + cmd, args, NULL); execve("/usr/sbin/" + cmd, args, NULL); execve("/usr/local/bin/" + cmd, args, NULL); fprintf(stderr, "command %s not found n", cmd); exit(1); while (wait(&status)!= pid); After a successful execve the rest will not be executed.

14 Background and Foreground Foreground Usual execution of a program Execute one by one. Background Put & at the end of command. Execute the next command without waiting termination of the current one. Multiple commands can be executed as background. Shell Foreground Background Background Background Editor Compiler Server

15 Implementation of Background Shell waits termination of child with wait system call. For background es, shell does not wait for the termination. execute(char *cmd, char *argv[], int foreground) { int pid, status; pid = fork(); if (pid == 0) { execve(cmd, argv, NULL); fprintf(stderr, "command %s not found n", cmd); exit(1); if (foreground) { while (wait(&status)!= pid);

16 Job Control A job may consists of more than one. Connect commands with pipe. A command may fork to create children. A job is a group of es. Foreground and background are controlled for jobs (not for each ). UNIX uses group. Each belongs to a group. Child es belong to the same group. Shell creates a new group for each command. group child group child group

17 Job Control for a Terminal Each terminal holds one group. can be set by ioctl with TIOCSPGRP parameter. Foreground terminal group = group Terminal switches group. Input from terminal Sent to foreground es. Background es stop when they try input from terminal. Output to terminal Foreground and background es can output. Possible to prohibit background es to output. shell group 123 group 234 group 345 foreground group 123 input/output background terminal output background output

18 Redirection Redirect standard input/output/error to files. % cat /etc/passwd > /tmp/aaa % wc < /etc/passwd Pipe can combine two commands. Output of one command is connected to input of the other. % grep abc /etc/passwd wc terminal file terminal terminal cat grep output pipe input wc redirect standard output connect commands using pipe

19 Implementation of Redirection File descriptors are inherited by execve. Set file descriptors before execve. execute(char *cmd, char *argv[]) { int pid, status; pid = fork(); if (pid == 0) { fd = open("/etc/passwd", O_RDONLY); dup2(fd, 0); close(fd); execve(cmd, argv, NULL);... while (wait(&status)!= pid); dup2 copies file descriptors. If changed before fork, shell's file descriptors are also changed. fork and exec need to be separated.

20 Shell variables and Environment varaibles Shell variables Used by shell Change shell behavior Often used in shell scripts. Environment variables Inherited to commands User name Home directory PATH shell variables command () shell environment variables fork copies environment variables

21 Wild Card Wide card *.c can be used to specify multiple files. *.c is expanded by shell DIR *dp; % ls *.c % cat a???.b? struct dirent *de; dp = opendir(dir); while (de = readdir(dp)) { if (match(name, de->d_name)) { strcpy(argv[argc++], de->d_name); closedir(dp); int match(char *pattern, char *p) { char ch; while (ch = *pattern++) { if (ch == * ) { while (*p) { if (match(pattern, p)) return 1; p++; return (*pattern == 0); else if (ch ==? ) { if (*p++ == 0) return 0; else if (*p++!= ch) return 0; return (*p == 0); matching of * and?

22 Shell Script test echo Hello date ls chmod a+x test Mark it as executable a new command test Create a new command combining some commands: Create a text file with commands. Mark it as executable using chmod. OS execute a shell if the given file is not binary. Conditional braches and repetitions are allowed. Shell can be specified in the first line. test #!/bin/csh -f echo Hello date ls

23 Summary Functions of shell Execute commands Job control Redirection Environment variables Wide card Shell script Other functions Command alias File name completion Command history