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

Slides:



Advertisements
Similar presentations
1 CS345 Operating Systems Φροντιστήριο Άσκησης 1.
Advertisements

Processes Topics Process context switches Creating and destroying processes CS 105 “Tour of the Black Holes of Computing!”
1 Processes Professor Jennifer Rexford
1 Processes and Pipes COS 217 Professor Jennifer Rexford.
Process in Unix, Linux and Windows CS-3013 C-term Processes in Unix, Linux, and Windows CS-3013 Operating Systems (Slides include materials from.
CS-502 Fall 2006Processes in Unix, Linux, & Windows 1 Processes in Unix, Linux, and Windows CS502 Operating Systems.
CSE 451 Section 4 Project 2 Design Considerations.
Fork and Exec Unix Model Tutorial 3. Process Management Model The Unix process management model is split into two distinct operations : 1. The creation.
Processes in Unix, Linux, and Windows CS-502 Fall Processes in Unix, Linux, and Windows CS502 Operating Systems (Slides include materials from Operating.
Process Control. Major Requirements of an Operating System Interleave the execution of several processes to maximize processor utilization while providing.
Some Example C Programs. These programs show how to use the exec function.
Unix Processes Slides are based upon IBM technical library, Speaking Unix, Part 8: Unix processes Extended System Programming Laboratory (ESPL) CS Department.
Shell (Part 1). Process r A process is an instance of an application running r If there are two instances of an application running then there are two.
Process in Unix, Linux, and Windows CS-3013 A-term Processes in Unix, Linux, and Windows CS-3013 Operating Systems (Slides include materials from.
Fundamentals CIS 552. Fundamentals Low-level I/O (read/write using system calls)  Opening/Creating files  Reading & Writing files  Moving around in.
Cli/Serv.: procs/51 Client/Server Distributed Systems v Objectives –look at how to program UNIX processes , Semester 1, Processes.
Process. Processes A process is an abstraction for sequence of operations that implement a computation/program. A process may be manipulated, suspended,
1 Week 2 The Crunchy Shell to the Soft and Chewy Kernel… Sarah Diesburg 8/3/2010 COP4610 / CGS5765.
System V IPC Provides three mechanisms for InterProcess Communication (IPC) : Messages : exchange messages with any process or server. Semaphores : allow.
Shell (Part 2). Example r What if we want to support something like this: m ps –le | sort r One process should execute ps –le and another should execute.
Minishell InKwan Yu Topics Unix System calls waitpid() pipe() dup2() C function calls strtok() strcmp() Minishell Software Enginnering.
Creating and Executing Processes
CS252: Systems Programming Ninghui Li Based on Slides by Prof. Gustavo Rodriguez-Rivera Topic 8: Opening Files and Starting Processes.
Shell (Addendum). Example r What if we want to support something like this: m ps –le | sort r One process should execute ps –le and another should execute.
System calls for Process management
CS 153 Design of Operating Systems Spring 2015 Lecture 5: Processes and Threads.
Operating Systems Process Creation
CS4315A. Berrached:CMS:UHD1 Process Management Chapter 6.
What is a Process? u A process is an executable “cradle” in which a program may run u This “cradle” provides an environment in which the program can run,
CSCI 330 UNIX and Network Programming Unit XII: Process & Pipe Part 1.
Process Management Azzam Mourad COEN 346.
Genesis: From Raw Hardware to Processes Andy Wang Operating Systems COP 4610 / CGS 5765.
1 A Seven-State Process Model. 2 CPU Switch From Process to Process Silberschatz, Galvin, and Gagne  1999.
CSCI 330 UNIX and Network Programming
Tutorial 3. In this tutorial we’ll see Fork() and Exec() system calls.
System calls for Process management Process creation, termination, waiting.
CS241 Systems Programming Discussion Section Week 2 Original slides by: Stephen Kloder.
1 Unix system calls fork( ) wait( ) exit( ). 2 How To Create New Processes? n Underlying mechanism -A process runs fork to create a child process -Parent.
CS241 Systems Programming Discussion Section Week 2 Original slides by: Stephen Kloder.
Process Related System Calls By Neha Hulkoti & Kavya Bhat.
Using System Calls (Unix) Have to tell compiler (if C/C++) where to find the headers, etc. – i.e., the “include” files May have to tell compiler where.
The Shell What does a shell do? - execute commands, programs - but how? For built in commands run some code to do the command For other commands find program.
A process is a program in execution A running system consists of multiple processes – OS processes Processes started by the OS to do “system things” –
CSCI 4061 Recitation 2 1.
Error handling I/O Man pages
Week 3 Redirection, Pipes, and Background
CS 3305A Process – Part II Lecture 4 Sept 20, 2017.
Section 8: Processes What is a process Creating processes Fork-Exec
Unix Process Management
Processes in Unix, Linux, and Windows
Processes in Unix, Linux, and Windows
Lecture 5: Process Creation
Fork and Exec Unix Model
LINUX System Programming with Processes (additional)
Processes in Unix, Linux, and Windows
Genesis: From Raw Hardware to Processes
Process Creation Process Termination
Tutorial 3 Tutorial 3.
Process Programming Interface
CS 105 “Tour of the Black Holes of Computing!”
Processes Prof. Ikjun Yeom TA – Mugyo
CS 105 “Tour of the Black Holes of Computing!”
CS 105 “Tour of the Black Holes of Computing!”
Processes in Unix, Linux, and Windows
Processes in Unix and Windows
EECE.4810/EECE.5730 Operating Systems
EECE.4810/EECE.5730 Operating Systems
Intro to the Shell with Fork, Exec, Wait
Chapter 3 Process Description and Control
Presentation transcript:

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

2 The shell! The kernel!

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? 3

Is this a Shell? 4

5

6

7

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 8

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 9

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

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

Inside main() while(1) { } 12

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

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

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

I/O Streams 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 16 Standard I/O StreamFile descriptor Standard input (stdin)0 Standard output (stdout)1 Standard error (stderr)2

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 17

fgets and printf 18 #include #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; }

strtok We can use strtok to walk through a string and get each token  See ‘man strtok’ for more info 19 #include int main() { const char str[80] = "This is a sample string "; 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); }

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! 20

Fork Child pid==0 Parent pid==something else 21 #include 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; }

A fork Example, Nag.c #include int main() { pid_t pid; if ((pid = fork()) == 0) { while (1) { printf(“child’s return value %d: I want to play…\n”, pid); } } else { while (1) { printf(“parent’s return value %d: After the project…\n”, pid); } return 0; } Parent process

A fork Example, Nag.c #include int main() { pid_t pid; if ((pid = fork()) == 0) { while (1) { printf(“child’s return value %d: I want to play…\n”, pid); } } else { while (1) { printf(“parent’s return value %d: After the project…\n”, pid); } return 0; } Parent process

A fork Example, Nag.c #include int main() { pid_t pid; if ((pid = fork()) == 0) { while (1) { printf(“child’s return value %d: I want to play…\n”, pid); } } else { while (1) { printf(“parent’s return value %d: After the project…\n”, pid); } return 0; } Parent process #include int main() { pid_t pid; if ((pid = fork()) == 0) { while (1) { printf(“child’s return value %d: I want to play…\n”, pid); } } else { while (1) { printf(“parent’s return value %d: After the project…\n”, pid); } return 0; } Child process

A fork Example, Nag.c #include int main() { pid_t pid; if ((pid = 3128) == 0) { while (1) { printf(“child’s return value %d: I want to play…\n”, pid); } } else { while (1) { printf(“parent’s return value %d: After the project…\n”, pid); } return 0; } Parent process #include int main() { pid_t pid; if ((pid = 0) == 0) { while (1) { printf(“child’s return value %d: I want to play…\n”, pid); } } else { while (1) { printf(“parent’s return value %d: After the project…\n”, pid); } return 0; } Child process

A fork Example, Nag.c #include int main() { pid_t pid; if ((pid = 3128) == 0) { while (1) { printf(“child’s return value %d: I want to play…\n”, pid); } } else { while (1) { printf(“parent’s return value %d: After the project…\n”, pid); } return 0; } Parent process #include int main() { pid_t pid; if ((pid = 0) == 0) { while (1) { printf(“child’s return value %d: I want to play…\n”, pid); } } else { while (1) { printf(“parent’s return value %d: After the project…\n”, pid); } return 0; } Child process

Nag.c Outputs >a.out child’s return value 0: I want to play… …// context switch parent’s return value 3218: After the project… …// context switch 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 #include #define ARGV_SIZE 10 int main(int argc, char *argv[]) { char *myArgv[ARGV_SIZE]; // an array of pointers to strings myArgv[0] = "ps"; myArgv[1] = "-aux"; myArgv[2] = NULL; // last element should be a NULL pointer execvp(myArgv[0], myArgv); return 0; // should not be reached A process

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! 30

Zombie? Process that shows up with a “Z” status or the word 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() 31

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 32

waitpid() Comment waitpid() line to see a defunct process for 10 seconds through ‘ps’ 33 #include 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; }

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

Any Questions?