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

Similar documents
Process Management! Goals of this Lecture!

Process Management 1

Using the Debugger. Michael Jantz Dr. Prasad Kulkarni

Process Creation in UNIX

Pipelines, Forks, and Shell

Lecture 5. Systems Programming: Unix Processes Creation: fork & exec Process Communication: Pipes

SOFTWARE ARCHITECTURE 3. SHELL

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

Process Management! Goals of this Lecture!

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

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

UNIX Kernel. UNIX History

CSC209H Lecture 6. Dan Zingaro. February 11, 2015

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

File Descriptors and Piping

Processes. Processes (cont d)

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

Creating a Shell or Command Interperter Program CSCI411 Lab

Assignment 1. Teaching Assistant: Michalis Pachilakis (

CS240: Programming in C

CSC 1600 Unix Processes. Goals of This Lecture

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

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

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

628 Lecture Notes Week 4

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

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

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

IPC and Unix Special Files

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

Linux shell scripting Getting started *

UNIVERSITY OF NEBRASKA AT OMAHA Computer Science 4500/8506 Operating Systems Summer 2016 Programming Assignment 1 Introduction The purpose of this

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

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

Interprocess Communication E. Im

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

Design Overview of the FreeBSD Kernel CIS 657

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.

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.

Unix Processes. What is a Process?

Fall 2015 COMP Operating Systems. Lab #3

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

UNIX System Calls. Sys Calls versus Library Func

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

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

Chapter 3 Processes we will completely ignore threads today

System Programming. Process Control III

The Classical OS Model in Unix

CSC209H Lecture 7. Dan Zingaro. February 25, 2015

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

Recitation 8: Tshlab + VM

Review of Fundamentals

SE350: Operating Systems

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

Operating systems and concurrency - B03

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

CSCI Computer Systems Fundamentals Shell Lab: Writing Your Own Unix Shell

Killing Zombies, Working, Sleeping, and Spawning Children

W4118: OS Overview. Junfeng Yang

OS Lab Tutorial 1. Spawning processes Shared memory

Introduction to Nachos Scheduling

Implementation of a simple shell, xssh

find starting-directory -name filename -user username

CSC209 Review. Yeah! We made it!

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

CS 213, Fall 2002 Lab Assignment L5: Writing Your Own Unix Shell Assigned: Oct. 24, Due: Thu., Oct. 31, 11:59PM

A2 Design Considerations. CS161, Spring

CS240: Programming in C

Process control. Game plan: Fork. Scribe notes for 11/11/14 by Diane Yang, Kara Shen, and Chris Lim. fork exec pipes

Lecture 8: Unix Pipes and Signals (Feb 10, 2005) Yap

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

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

Basic OS Progamming Abstrac7ons

CSE 410: Systems Programming

Basic OS Progamming Abstrac2ons

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

bash, part 3 Chris GauthierDickey

Introduction to UNIX. Introduction. Processes. ps command. The File System. Directory Structure. UNIX is an operating system (OS).

Introduction to UNIX. CSE 2031 Fall November 5, 2012

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

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

NETWORK AND SYSTEM PROGRAMMING

CSC209 Fall Karen Reid 1

Shell scripting and system variables. HORT Lecture 5 Instructor: Kranthi Varala

Processes. Process Concept

CS 3733 Operating Systems

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

fork System-Level Function

Command-line interpreters

Process Management 1

Subcontractors. bc math help for the shell. interactive or programatic can accept its commands from stdin can accept an entire bc program s worth

System Programming. Introduction to Unix

CSE 390a Lecture 2. Exploring Shell Commands, Streams, Redirection, and Processes

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

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

CS 213, Fall 2001 Lab Assignment L5: Writing Your Own Unix Shell Assigned: Oct. 25, Due: Fri., Nov. 2, 11:59PM

INSTITUTE OF AERONAUTICAL ENGINEERING (Autonomous) Dundigal, Hyderabad

Essentials for Scientific Computing: Bash Shell Scripting Day 3

PROGRAMMING PROJECT ONE DEVELOPING A SHELL

Transcription:

The Shell, System Calls, Processes, and Basic Inter-Process Communication Michael Jantz Dr. Prasad Kulkarni 1

Shell Programs A shell program provides an interface to the services of the operating system. It interprets user commands and uses the system calls provided by the operating system to execute commands. As programmers sought to increase efficiency and convenience of shell programs, shells with fairly sophisticated command languages developed. 2

Example Shell Commands Some common bash operators: foo < in.txt redirect standard input of program foo to in.txt. foo > out.txt redirect standard output of program foo to out.txt. foo >> out.txt redirect standard output of program foo to be appended to the file out.txt. foo bar redirect standard output of program foo to be the standard input to program bar. Bash is also a scripting programming language (complete with variables and if and while statements) that can be used to script the OS. 3

Utility Programs Any Unix distribution comes with several utility programs for interacting with the OS. grep search for strings in a file find find a particular file du Determine the disk usage of files and directories ls List files and their permissions Many, many more. Proficiency with these basic tools will make you a much more effective developer on your platform. Unix provides a manual (accessible from the shell) that documents the use and syntax of each core utility. e.g: man grep 4

finder.sh find $1 -name '*'.[ch] xargs grep -c $2 sort -t : +1.0-2.0 --numeric --reverse head --lines=$3 find $1 -name '*'.[ch] Find files with.c and.h extensions under the directory given by the first argument. xargs grep -c $2 Search the set of files on standard input for the string given by the second argument. -c says that instead of printing out each usage in each file, give me the number of times $2 is used in each file. sort Sort standard input and print the sorted order to standard output. -t : +1.0-2.0 says sort using the second column on each line (delimited by the ':' character) as a key. --numeric says to sort numerically (as opposed to alphabetically). --reverse says sort in reverse order. head print only the first n lines of standard input. --lines=$3 lets us set the number of lines with the third argument. 5

How the Shell Works When the user types this command at a shell, the shell parses the input, and issues system calls to create the processes and set up the pipes between these processes. In this lab, we will implement what the shell would typically perform when given a command like this. Although to save time, our implementation will only work for pipelines of length 4 as opposed to arbitrarily long pipelines (as a shell would handle). 6

Getting Started The first thing to notice after untarring the tar file is the Makefile: Notice the variables DIR, STR, and NUM_FILES and the command under the 'find' target. Test the command. In this lab's directory, do: bash> make find Should see the output as described two slides back. The goal of this lab is to write a program finder.c that produces the same output as this command. 7

finder.c As it is given, this program is a skeleton for a four stage pipeline. All it currently does is start a process, which forks off four children (which do nothing), waits for them to finish, and exits. 8

finder.c (cont.) We want a program that forks off four children, sets up pipes between these children, and executes the appropriate command with each child: main Fork 1 Fork 2 Fork 3 Fork 4 find P1 grep P2 sort P3 head 9

System Calls In order to accomplish this, you will need to make use of a few different system calls. The starter code uses fork() and waitpid() to create the general framework under which you will implement the desired functionality. You can look into the small program fork.c to understand the difference between the return values of fork() for parent and child If you do not understand how these work, please ask me. You will extend this program to create the desired functionality using: pipe(), dup2(), execv(), and close() 10

File Descriptors Before getting into how these system calls work, you must first understand file descriptors: A file descriptor is a per-process, unique, non-negative integer used to identify an open file for the purpose of file access. System calls (such as open, close, read, and write) use file descriptors to identify a particular file or pipe. IMPORTANT: Each process has its own file descriptor table that maps each open file descriptor to a file object maintained by the operating system. The file descriptor table is located in the process' PCB and is inherited by children of the process. 11

pipe() int pipe(int pipefd[2]) Creates a unidirectional data channel that can be used for interprocess communication. pipefd[0] is the read end of the pipe pipefd[1] is the write end of the pipe See man pipe for example usage. Things to think about: How many calls to pipe should you make to construct a three process pipeline? Which process(es) should create the pipe(s)? If you find yourself unsure, ask yourself which process(es) would create the pipe(s) if you were designing a shell that could handle pipelines of arbitrary length. 12

dup2() int dup2(int oldfd, int newfd) On return, the newfd provided will be a copy of oldfd (i.e. newfd will refer to the same file (or pipe) as oldfd). Things to think about: Each utility program we want to use in the pipeline reads from stdin and writes to stdout. Why have I showed you dup2() and not the closely related dup()? See man dup for an explanation of dup. 13

Constructing the Pipeline Go ahead and try to setup the pipeline using the pipe() and dup2() system calls. Remember to close() unused file descriptors for each process. You may want to experiment with pipes using the pipe.c program first. Try to connect the processes in pipe.c so that the file read in the first process is written down a pipe which is read from in the second process. 14

Adding exec When you are confident your pipeline is working correctly, all that is left is to tell each process to exec the appropriate binary. exec replaces the current process image with a new process image specified by a binary file name and arguments. Once the image is replaced, you have no control over what the process does (which is why it is recommended that you test the pipeline well before this step). 15

execl() There are many different flavors of exec. They give the programmer options as to how he or she would like to specify an executable and its arguments. int execl(const char *path, const char *arg1,... ) Allows you to specify the path to the executable and the arguments to the executable (as you would type at the command line) as a variable number of const char * arguments. Final argument must be (char *) 0 16

execl example if (pid_1 == 0) { /* First Child */ char cmdbuf[bsize]; bzero(cmdbuf, BSIZE); sprintf(cmdbuf, "%s %s -name \'*\'.[ch]", FIND_EXEC, argv[1]); /* set up pipes */ } if ( (execl(bash_exec, BASH_EXEC, "-c", cmdbuf, (char *) 0)) < 0) { fprintf(stderr, "\nerror execing find. ERROR#%d\n", errno); return EXIT_FAILURE; } 17

execv() execv() is very similar to execl(), but it only takes two arguments. int execv(const char *path, char *const argv[ ]) The argv[ ] array contains all of the arguments to the executable. Like execl(), the final argument of argv[ ] must be (char *) 0 18

execv() example if (pid_1 == 0) { /* First Child */ char cmdbuf[bsize]; bzero(cmdbuf, BSIZE); sprintf(cmdbuf, "%s %s -name \'*\'.[ch]", FIND_EXEC, argv[1]); /* set up pipes */ } char* myargs[] = {BASH_EXEC, "-c", cmdbuf, (char*) 0}; if( (execv(bash_exec, myargs)) < 0){ fprintf(stderr, "\nerror execing find. ERROR#%d\n", errno); return EXIT_FAILURE; } 19

Finishing Up After you have each completed your implementation, compile the finder program and run the test code: bash> make test If the diff line does not produce an error, your implementation is correct. 20