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

Similar documents
CS240: Programming in C

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.

CSC 1600 Unix Processes. Goals of This Lecture

CS 350 : COMPUTER SYSTEM CONCEPTS SAMPLE TEST 2 (OPERATING SYSTEMS PART) Student s Name: MAXIMUM MARK: 100 Time allowed: 70 minutes

518 Lecture Notes Week 3

Process Management! Goals of this Lecture!

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 20

Operating systems and concurrency - B03

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

System Programming. Process Control II

Discussion of Assignments 2. Line buffered vs. full buffered I/O. Some often encountered issues in the submissions.

CSC209H Lecture 6. Dan Zingaro. February 11, 2015

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

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

Process Management 1

Process management 1

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

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

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

Process Management 1

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

CS240: Programming in C

Project 2: Shell with History1

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

Process Management! Goals of this Lecture!

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

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

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

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

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

Announcement (1) sys.skku.edu is now available

Introduction to Processes

Fall 2015 COMP Operating Systems. Lab #3

System Programming. Process Control III

PROCESS PROGRAMMING INTERFACE

Unix Processes 1 / 31

fork System-Level Function

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

CSC209 Fall Karen Reid 1

COE518 Lecture Notes Week 4 (Sept. 26, 2011)

LAB 1: C PRIMER CS444/544 WENJIN HU JAN 16TH, 2009

Part II Processes and Threads Process Basics

API Interlude: Process Creation (DRAFT)

Chapter 3 Processes we will completely ignore threads today

Operating System Structure

CSC 252: Computer Organization Spring 2018: Lecture 19

Operating Systemss and Multicore Programming (1DT089)

The Process Abstraction. CMPU 334 Operating Systems Jason Waterman

Programming Assignment #1: A Simple Shell

Processes: Introduction. CS 241 February 13, 2012

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

COE518 Lecture Notes Week 2 (Sept. 12, 2011)

Parents and Children

Environment Variables

Processes. CSE 351 Autumn Instructor: Justin Hsia

Operating Systems Lab

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

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

CS 201. Processes. Gerson Robboy Portland State University

Operating Systems. Lecture 05

CSE 410: Computer Systems Spring Processes. John Zahorjan Allen Center 534

Processes. CSE 351 Autumn Instructor: Justin Hsia

Killing Zombies, Working, Sleeping, and Spawning Children

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

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

PESIT Bangalore South Campus Hosur road, 1km before Electronic City, Bengaluru -100 Department of Information Sciences and Engineering

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

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

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

Operating Systems. Engr. Abdul-Rahman Mahmood MS, PMP, MCP, QMR(ISO9001:2000) alphapeeler.sf.net/pubkeys/pkey.htm

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

SE350: Operating Systems

Interrupts, Fork, I/O Basics

Computer Systems Assignment 2: Fork and Threads Package

CSE 153 Design of Operating Systems Fall 2018

PROCESSES. Jo, Heeseung

Processes. Jo, Heeseung

PROCESS CONTROL BLOCK TWO-STATE MODEL (CONT D)

Exceptional Control Flow Part I

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

Processes. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

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

CITS2002 Systems Programming. Creating a new process using fork() 1 next CITS2002 CITS2002 schedule

What is a Process. Preview. What is a Process. What is a Process. Process Instruction Cycle. Process Instruction Cycle 3/14/2018.

Assignment 1. Teaching Assistant: Michalis Pachilakis (

Operating Systems. Processes

Processes. CS3026 Operating Systems Lecture 05

Q & A (1) Where were string literals stored? Virtual Address. SSE2033: System Software Experiment 2 Spring 2016 Jin-Soo Kim

Creating a Shell or Command Interperter Program CSCI411 Lab

File IO and command line input CSE 2451

Piotr Mielecki Ph. D.

Lesson 2. process id = 1000 text data i = 5 pid = 1200

Computer Science & Engineering Department I. I. T. Kharagpur

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

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

TCSS 422: OPERATING SYSTEMS

Sample Solutions to Project 1

Lecture 3 Process API in UNIX systems

Final Precept: Ish. Slides Originally Prepared by: Wonho Kim

University of Washington What is a process?

Transcription:

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

Why is the Shell Important? Shells provide us with a way to interact with the core system Executes programs on our behalf Shows us our stuff No OS should be without one! Can think of a shell as built around a component So what are some examples of shells? 2

How do we Crack the Shell? In other words, how will our shell interact with the soft and chewy kernel? The mechanism we will explore today is through system calls Other ways /proc in Linux 3

System Calls What are system calls, again? Some important ones fork() execvp() waitpid() We will need to use all three above to make a fully-functioning shell 4

Shell Basics (Project 1) Basic shell components Prompt and accept user input Execute the command OR perform some built-in functionality Reset the shell (Loop back to first step) 5

Inside main() Continuous loop Parse user input Make something happen 6

Inside main() while(1) { 7

Inside main() while(1) { */ Get user input */ 8

Inside main() while(1) { */ Get user input */ */ Exit? */ 9

Inside main() while(1) { */ Get user input */ */ Exit? */ */ Do something with input */ 10

Inside main() while(1) { */ Get user input */ */ Exit? */ */ Do something with input */ */ Reset the shell */ 11

I/O Streams Standard I/O Stream File descriptor Standard input (stdin) 0 Standard output (stdout) 1 Standard error (stderr) 2 User input goes to virtual file descripter 0 Regular program output goes to virtual file descriptor 1 Sometimes error messages are printed to virtual file descriptor 2 12

Getting Input / Printing Output Use the fgets() library call to get input from stdin char *fgets(char *s, int size, FILE *stream); Use printf() to print to stdout Use fprintf() to print to any file To see how to use printf, take a look at the end of section 2 of the short C tutorial Also man printf for a list of all specifiers 13

fgets and printf #include <stdio.h> #include <stdlib.h> #define MAX_LINE 80 int main(int argc, char *argv[]) { char cmd[max_line]; /* Get user input */ fgets(cmd, MAX_LINE, stdin); printf("the input was [%s]\n",cmd); return 0; 14

strtok #include <string.h> #include <stdio.h> int main() { const char str[80] = "This is a sample string 1 2 34"; const char s[2] = " "; char *token; /* get the first token */ token = strtok(str, s); /* walk through other tokens */ while( token!= NULL ) { printf( " %s\n", token ); token = strtok(null, s); return(0); We can use strtok to walk through a string and get each token See man strtok for more info 15

Processes Our shell process must continually run but we need to execute other stuff on the user s behalf How can we create children processes to do our work? Fork! 16

Fork Child pid==0 Parent pid==something else #include <stdio.h> #include <unistd.h> #include <sys/types.h> int main() { pid_t pid; if ((pid = fork()) == 0) { printf( I am a child with pid %d\n, pid); else { printf( I am the parent with pid %d\n, pid); return 0; 17

A fork Example, Nag.c #include <stdio.h> #include <unistd.h> #include <sys/types.h> int main() { pid_t pid; if ((pid = fork()) == 0) { printf( child s return value %d: I want to play \n, pid); else { printf( parent s return value %d: After the project \n, pid); return 0; Parent process

A fork Example, Nag.c #include <stdio.h> #include <unistd.h> #include <sys/types.h> int main() { pid_t pid; if ((pid = fork()) == 0) { printf( child s return value %d: I want to play \n, pid); else { printf( parent s return value %d: After the project \n, pid); return 0; Parent process

A fork Example, Nag.c #include <stdio.h> #include <unistd.h> #include <sys/types.h> int main() { pid_t pid; if ((pid = fork()) == 0) { else { return 0; Parent process #include <stdio.h> #include <unistd.h> #include <sys/types.h> printf( child s return value %d: I want int to main() play \n, { pid); pid_t pid; if ((pid = fork()) == 0) { printf( parent s return value %d: After the printf( child s project \n, pid); return value %d: I want to p else { printf( parent s return value %d: After the return 0; Child process

A fork Example, Nag.c #include <stdio.h> #include <unistd.h> #include <sys/types.h> int main() { pid_t pid; if ((pid = 3128) == 0) { else { return 0; Parent process #include <stdio.h> #include <unistd.h> #include <sys/types.h> printf( child s return value %d: I want int to main() play \n, { pid); pid_t pid; if ((pid = 0) == 0) { printf( parent s return value %d: After the printf( child s project \n, pid); return value %d: I want to p else { printf( parent s return value %d: After the return 0; Child process

A fork Example, Nag.c #include <stdio.h> #include <unistd.h> #include <sys/types.h> int main() { pid_t pid; if ((pid = 3128) == 0) { else { return 0; Parent process #include <stdio.h> #include <unistd.h> #include <sys/types.h> printf( child s return value %d: I want int to main() play \n, { pid); pid_t pid; if ((pid = 0) == 0) { printf( parent s return value %d: After the printf( child s project \n, pid); return value %d: I want to p else { printf( parent s return value %d: After the return 0; Child process

Nag.c Outputs >a.out child s return value 0: I want to play child s return value 0: I want to play child s return value 0: I want to play // context switch parent s return value 3218: After the project parent s return value 3218: After the project parent s return value 3218: After the project // context switch child s return value 0: I want to play child s return value 0: I want to play child s return value 0: I want to play ^C >

The exec System Call Family A fork by itself is not interesting To make a process run a program that is different from the parent process, you need exec system call exec starts a program by overwriting the current process

A exec Example, train.c #include <stdio.h> #include <unistd.h> #include <sys/types.h> #include <string.h> #include <malloc.h> #define ARGV_SIZE 2 A process At a shell prompt: >whereis sl int main(int argc, char *argv[]) { char fullpathname[] ="/usr/games/sl"; char *myargv[argv_size]; // an array of pointers to strings myargv[0] = fullpathname; myargv[1] = NULL; // last element should be a NULL pointer execvp(fullpathname, myargv); return 0; // should not be reached

Wait up? How does our parent process know to wait until the child is done? waitpid() Performing a wait allows the system to release the resources associated with the child If child is not waited on, it will become a zombie! 26

Zombie? Process that shows up with a Z status or the word <defunct> Child process has terminated, but parent has not waited on it Child process stays allocated on the system until Wait() or waitpid() is called by the parent The parent exits, and init adopts the zombie processes and performs a wait() 27

waitpid() int waitpid(pid_t pid, int *status, int options); pid type of children to wait on For this project, pid==-1 to mean wait for any child process created by our parent *status returns the status of the child process options return if additional things have happened to the child 28

waitpid() Comment waitpid() line to see a defunct process for 10 seconds through ps #include <stdio.h> #include <unistd.h> #include <sys/types.h> #include <stdlib.h> #include <sys/wait.h> int main() { pid_t pid; if ((pid = fork()) == 0) { printf( I am a child with pid %d\n, pid); else { printf( I am the parent\n ); waitpid(-1, NULL, 0); sleep(10); return 0; 29

In Summary Pieces necessary for project 1 Basic structure of shell Command line parsing Command execution 30

Any Questions?