1 Week 2 The Crunchy Shell to the Soft and Chewy Kernel… Sarah Diesburg 8/3/2010 COP4610 / CGS5765.

Slides:



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

15-213/ Intro to Computer Systems by btan with reference to Spring 10’s slides.
UC Santa Barbara Project 1 Discussion Bryce Boe 2011/04/12.
Process Control Hua LiSystems ProgrammingCS2690Process Control Page 1 of 41.
CS 497C – Introduction to UNIX Lecture 26: - The Process Chin-Chih Chang
1 Processes Professor Jennifer Rexford
Source: T.Y. Wong, Chinese University of Hong Kong Supplementary materials: Command shell using fork, exec, and wait.
Exec function Exec function: - replaces the current process (its code, data, stack & heap segments) with a new program - the new program starts executing.
CS Lecture 15 Outline Process Management System calls – exec() – chdir() – system() – nice() – Accessing User and Group IDs – Redirection Lecture.
Process in Unix, Linux and Windows CS-3013 C-term Processes in Unix, Linux, and Windows CS-3013 Operating Systems (Slides include materials from.
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.
March 1, 2002Serguei A. Mokhov, 1 Brief Introduction to System Calls and Process Management COMP 229, 346, 444, 5201 Revision 1.3.
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.
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.
Simple Shell Part 1 Due date (75%): April, 2002 Part 2 Due date (25%): Apr 5, 2002.
Fundamentals CIS 552. Fundamentals Low-level I/O (read/write using system calls)  Opening/Creating files  Reading & Writing files  Moving around in.
Today’s Topics Introducing process: the basic mechanism for concurrent programming –Process management related system calls Process creation Process termination.
Creating and Executing Processes
Unix/Linux cs3353. The Shell The shell is a program that acts as the interface between the user and the kernel. –The shell is fully programmable and will.
CS 241 Section Week #2 9/9/10. 2 Topics This Section MP1 issues MP2 overview Process creation using fork()‏ Debugging tools: valgrind, gdb.
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.
Agenda  Working with Processes: Purpose Running Programs within same process (execl, execlp, execle, execv, execvp, execve) “Spawning” other process (fork,
Lecture 24CS311 – Operating Systems 1 1 CS311 – Lecture 24 Outline Final Exam Study Guide Note: These lecture notes are not intended replace your notes.
System calls for Process management
Process Management CS3320 Spring Process A process is an instance of a running program. –Not the same as “program” or “processor” Process provides.
Operating Systems Process Creation
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.
1 A Seven-State Process Model. 2 CPU Switch From Process to Process Silberschatz, Galvin, and Gagne  1999.
CSCI 330 UNIX and Network Programming
Introduction to Bash Shell. What is Shell? The shell is a command interpreter. It is the layer between the operating system kernel and the user.
S -1 Processes. S -2 wait and waitpid (11.2) Recall from a previous slide: pid_t wait( int *status ) wait() can: (a) block; (b) return with status; (c)
The Process CIS 370, Fall 2009 CIS UMassD. The notion of a process In UNIX a process is an instance of a program in execution A job or a task Each process.
Tutorial 3. In this tutorial we’ll see Fork() and Exec() system calls.
System calls for Process management Process creation, termination, waiting.
Dsh: A Devil Shell COMPSCI210 Recitation 14 Sep 2012 Vamsi Thummala.
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.
4.1 Operating Systems Lecture 9 Fork and Exec Read Ch
Process Related System Calls By Neha Hulkoti & Kavya Bhat.
1 Intro to the Shell with Fork, Exec, Wait Sarah Diesburg Operating Systems CS 3430.
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” –
Implementation of a simple shell, xssh
Week 3 Redirection, Pipes, and Background
Implementation of a simple shell, xssh (Section 1 version)
Section 8: Processes What is a process Creating processes Fork-Exec
LINUX System : Lecture 8 Programming with Processes
Implementation of a simple shell, xssh
Using Processes.
Unix Process Management
Processes in Unix, Linux, and Windows
Sarah Diesburg Operating Systems CS 3430
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
Tutorial 3 Tutorial 3.
Process Programming Interface
Processes Prof. Ikjun Yeom TA – Mugyo
Processes in Unix, Linux, and Windows
Chapter 3 The UNIX Shells
EECE.4810/EECE.5730 Operating Systems
EECE.4810/EECE.5730 Operating Systems
Intro to the Shell with Fork, Exec, Wait
Presentation transcript:

1 Week 2 The Crunchy Shell to the Soft and Chewy Kernel… Sarah Diesburg 8/3/2010 COP4610 / CGS5765

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?  /proc file system  System calls 3

What is /proc? Virtual file system created to pass information via files to and from the kernel  Inside /proc directory Often used in kernel debugging or when a developer does not want to create a new system call 4

Why /proc? You will be reading and displaying a bunch of goodies from the kernel through the /proc interface – Date – Up Time – Idle Time – CPU Info – Memory Info – Kernel Version – Terminal Process Details 5

How /proc? Looking to our good friend bash… $> cat /proc/cpuinfo (Hint – your viewproc command should do something similar…) 6

System Calls What are they again? The traditional way to ask the OS to do something on the user’s behalf Some important ones  Fork()  Execv() 7

Shell Basics (Project 1) 8

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

Inside main() while(1) { } 10

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

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

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

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

I/O Streams Examples  scanf() reads from standard input  fscanf() reads from any I/O stream  printf() prints to standard output  fprintf() prints to any I/O stream 15 Standard I/O StreamFile descriptor Standard input (stdin)0 Standard output (stdout)1 Standard error (stderr)2

Environmental Variables Gives programs specific information about your environemnt, such as your execution paths echo $PATH /usr/local/bin:/usr/bin:/bin:/usr/games May be set by you or other shell scripts (like.bashrc) export TEST=hello echo $TEST hello 16

Environmental Variables char *getenv(const char *name); Returns value of an environmental variable Returns NULL if not found 17

Environmental Variables Important examples  $PATH  $USER  $PWD (Hint: may want to use these in building the shell prompt) 18

Command Line Parsing Standard input (stdin) is the source of input data for command line programs Parsing can be done in multiple stages  Strip the whitespace  Interpret the command  Resolve the pathname  Variable expansion  I/O redirection  Final execution 19

Parsing Example ls -l a 20 Too much whitespace!

Parsing Example ls -l a ls –la 21 Parse out the whitespace

Parsing Example ls -l a ls –la /bin/ls -la 22 Resolve the pathname

Resolving Pathnames? You may not just pass ‘ls’ to the execute command  What is ‘ls’? You must search all of the users paths stored in the $PATH environmental variable 23

Finding full pathname for ‘ls’ $PATH=/usr/local/bin:/usr/bin:/bin Does /usr/local/bin/ls exist?  No Does /usr/bin/ls exist?  No Does /bin/ls exist?  Yes! 24

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

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

Exec Once we have created a child process, we need the child to execute a command for us Exec has many forms  Execl  Execlp  Execle  Execv  Execvp Must use execv() for project 1! 27

Execv() Takes two arguments  Absolute pathname  Array of string arguments, ending with NULL What is an absolute pathname?  Full execution path starting from root “/”  /bin/ls 28

Execv() char *command = “/bin/ls”; char *argv[] = {“/bin/ls”, “-l”, NULL}; execv(command,argv); Execv() replaces running image of child with a new process! 29

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==0 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, status, 0); sleep(10); } return 0; }

In Summary Pieces necessary for some of project 1  Part 1 – Command line parsing  Part 2 – Environmental variables and expansion  Part 3 – Command execution  Part 7 – Built-ins including /proc Hint: chdir() may be of some use  Part 8 – The prompt via parsing environmental variables and updating $PWD 34

Next Recitation Part 4 – Input/output redirection Part 5 – Pipes Part 6 – Background processing 35

Any Questions? Time for some of my demos?