Recitation 8 (Nov. 1) Outline Process & job control Lab 5 Reminder Lab 5: Due Thursday Minglong Shao Office hours: Thursdays 5-6PM.

Slides:



Advertisements
Similar presentations
Process Management.
Advertisements

3.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Process An operating system executes a variety of programs: Batch system.
Recitation 8: 10/28/02 Outline Processes Signals –Racing Hazard –Reaping Children Annie Luo Office Hours: Thursday 6:00.
June 1, 1999Foreground/Background Processing1 Introduction to UNIX H. Foreground/Background Processing.
1 CS345 Operating Systems Φροντιστήριο Άσκησης 1.
CS283 Systems Programming Process Management and Signals
Last Lab: Writing a shell
Carnegie Mellon 1 Exceptional Control Flow : Introduction to Computer Systems Recitation 9: Monday, October 21 st, 2013 Ian Hartwig Section E.
15-213, Fall 06 Outline Shell Lab Processes Signals.
System Programming Project 2 Due : 4/19...?. Foreground & Background Foreground job Foreground job Only one at moment Only one at moment Having user’s.
15-213/ Intro to Computer Systems by btan with reference to Spring 10’s slides.
Process Control Hua LiSystems ProgrammingCS2690Process Control Page 1 of 41.
UNIX Process Control Bach 7 Operating Systems Course Hebrew University Spring 2007.
CS 311 – Lecture 14 Outline Process management system calls Introduction System calls  fork()  getpid()  getppid()  wait()  exit() Orphan process.
Source: T.Y. Wong, Chinese University of Hong Kong Supplementary materials: Command shell using fork, exec, and wait.
Recitation 8 (Nov. 1) Outline Lab 5 hints Virtual Memory Reminder Shell Lab: Due THIS Thursday TA: Kun Gao Modified from Minglong Shao’s Recitation, Fall.
CS Lecture 15 Outline Process Management System calls – exec() – chdir() – system() – nice() – Accessing User and Group IDs – Redirection Lecture.
CSSE Operating Systems
Section A (March 14) Outline Exceptions Process Signals Non-local jumps Reminders Lab4: Due Next Thursday TA: Kun Gao Shamelessly Modified from Minglong.
Processes, Signals, I/O, Shell lab
Fork and Exec Unix Model Tutorial 3. Process Management Model The Unix process management model is split into two distinct operations : 1. The creation.
Process Control. Major Requirements of an Operating System Interleave the execution of several processes to maximize processor utilization while providing.
Carnegie Mellon 1 Processes, Signals, I/O, Shell Lab : Introduction to Computer Systems Recitation 9: 10/21/2013 Tommy Klein Section B.
LINUX System Programming with Processes. Overview 1. What is a Process? 2. fork() 3. exec() 4. wait() 5. Process Data 6. File Descriptors across Processes.
Today’s Topics Introducing process: the basic mechanism for concurrent programming –Process management related system calls Process creation Process termination.
Recitation 9: Section L (1:30pm - 2:20pm) Monday, October 22, 2012 Processes, Signals and Shell Lab Siddharth Dhulipalla.
The process concept (section 3.1, 3.3 and demos)  Process: An entity capable of requesting and using computer resources (memory, CPU cycles, files, etc).
1 Week 2 The Crunchy Shell to the Soft and Chewy Kernel… Sarah Diesburg 8/3/2010 COP4610 / CGS5765.
Creating and Executing Processes
CS 241 Section Week #2 9/9/10. 2 Topics This Section MP1 issues MP2 overview Process creation using fork()‏ Debugging tools: valgrind, gdb.
System calls for Process management
Concurrent Processes Processes can concurrently run same program. Processes can concurrently run same program. Processes can start other processes. Processes.
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.
Recitation: Signaling S04, Recitation, Section A Debug Multiple Processes using GDB Debug Multiple Processes using GDB Dup2 Dup2 Signaling Signaling.
Carnegie Mellon 1 Processes, Signals, I/O, Shell Lab : Introduction to Computer Systems Recitation 9: Monday, Oct. 21, 2013 Marjorie Carlson Section.
Process Management Azzam Mourad COEN 346.
CSCI 330 UNIX and Network Programming
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)
1 Exceptional Control Flow III. 2 Outline Loading and Running Programs Multitasking, shells Suggested reading 8.4.
Tutorial 3. In this tutorial we’ll see Fork() and Exec() system calls.
System calls for Process management Process creation, termination, waiting.
2.1 Processes  process = abstraction of a running program  multiprogramming = CPU switches from running program to running program  pseudoparallelism.
1 Exceptional Control Flow Ⅱ. 2 Outline Kernel Mode and User Mode Process context switches Three States of Processes Context Switch System Calls and Error.
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.
CSCI 4061 Recitation 2 1.
Week 3 Redirection, Pipes, and Background
Recitation 7 (Oct. 25) Outline Minglong Shao Office hours: Reminders
Section 8: Processes What is a process Creating processes Fork-Exec
LINUX System : Lecture 8 Programming with Processes
Unix Process Management
UNIX PROCESSES.
Exceptional Control Flow Part II
Exceptional Control Flow
Fork and Exec Unix Model
LINUX System Programming with Processes (additional)
Exceptional Control Flow
Process Creation Process Termination
Tutorial 3 Tutorial 3.
Exceptional Control Flow
Exceptional Control Flow
Exceptional Control Flow
System Programming: Process Management
Presentation transcript:

Recitation 8 (Nov. 1) Outline Process & job control Lab 5 Reminder Lab 5: Due Thursday Minglong Shao Office hours: Thursdays 5-6PM Wean Hall 1315

Process & concurrency int fork(void) Create an identical copy of the parent process Return values differentiate parent from child Any scheduling order of processes are possible Unless code explicitly imposes some order (synchronization) Context switching can happen at any point

Example 1 int main() { pid_t pid; if((pid = fork()) == 0) { printf("a "); } else { printf("b "); if(fork() == 0) { printf("c "); } if(waitpid(pid, NULL, 0) > 0) printf("d "); printf("e "); } Possible outputs 1. a b c d e e e ? 2. b a e c d e e ? 3. a b c e e d e ? 4. b e c e a d e ?

Example 1 (cont.) int main() { pid_t pid; if((pid = fork()) == 0) { printf("a "); } else { printf("b "); if(fork() == 0) { printf("c "); } if(waitpid(pid, NULL, 0) > 0) printf("d "); printf("e "); } L0 L1 “b “ L2 “a ““e “ (“d “) “c “ “e “ In L1, pid = 0  wait for any child process In L0 and L2, pid is the process id of L1  wait for process L1 1. a b c d e e e N 2. b a e c d e e Y 3. a b c e e d e Y 4. b e c e a d e N

Lab 5 A tiny shell (tsh) with job control & I/O redirection Key points: Reap all child processes Handle SIGCHLD, SIGTSTP, SIGINT Avoid race hazard

Process tree for shell Fore- ground job Back- ground job #1 Back- ground job #2 Shell Child pid=10 pgid=10 Foreground process group 20 Background process group 32 Backgroud process group 40 pid=20 pgid=20 pid=32 pgid=32 pid=40 pgid=40 pid=21 pgid=20 pid=22 pgid=20 Each job has a unique process group id int setpgid(pid_t pid, pid_t pgid); setpgid(0, 0);

Process tree for tsh Fore- ground job Back- ground job #1 Back- ground job #2 tsh Child pid=10 pgid=10 Foreground process group 20 Background process group 32 Backgroud process group 40 pid=20 pgid=20 pid=32 pgid=32 pid=40 pgid=40 pid=21 pgid=20 pid=22 pgid=20 UNIX shell pid=5 pgid=5 Foreground job receives SIGINT, SIGTSTP, when you type ctrl-c, ctrl-z Forward signals int kill(pid_t pid, int sig) pid > 0: send sig to specified process pid = 0: send sig to all processes in same group pid < -1: send sig to group -pid

Execute program int execve(const char *fname, char *const argv[], char *const envp[]); Examples: execve(“/bin/ls”, NULL, NULL); execve(“./mytest”, argv, envp); “front-end” functions for execve int execl(const char* path, const char *arg, …); int execlp(const char* file, const char *arg, …); int execle(const char*path, const char *arg, … char * const envp[]); int execv(const char *path, char *const argv[]); int execvp(const char *file, char *const argv[]);

Reaping child process waitpid(pid_t pid, int *status, int options) pid: wait for child process with pid (process id or group id) -1: wait for any child process status: tell why child terminated options: WNOHANG: return immediately if no children zombied WUNTRACED: report status of terminated or stopped children In Lab 5, use waitpid(-1, &status, WNOHANG|WUNTRACED) In sigchld_handler() : while ((c_pid = waitpid(…)) > 0) { … }

Check status int status; waitpid(pid, &status, WNOHANG|WUNTRACED) Macros to evaluate status: WIFEXITED(status): child exited normally WEXITSTATUS(status): returns code when child exits WIFSIGNALED(status): child exited because a signal is not caught WTERMSIG(status): gives the terminating signal number WIFSTOPPED(status): child is currently stopped WSTOPSIG(status): gives the stop signal number

Reaping child process in tsh Where to put waitpid(…) Two waits In sigchld_handler() : for bg processes In eval() : for fg processes One wait In sigchld_handler() : for both tsh should still wait for fg job to complete, how?

Busy wait for foreground job In eval() : if(fork() != 0) { /* parent */ addjob(…); while(fg process still alive){ /* do nothing */ }

Sleep In eval() : if(fork() != 0) { /* parent */ addjob(…); while(fg process still alive){ sleep(1); }

Race hazard A data structure is shared by two pieces of code that can run concurrently Different behaviors of program depending upon how the schedule interleaves the execution of code.

An example of race hazard sigchld_handler() { while ((pid = waitpid(…)) > 0){ deletejob(pid); } eval() { pid = fork(); if(pid == 0) { /* child */ execve(…); } /* parent */ /* signal handler may run BEFORE addjob()*/ addjob(…); }

An okay schedule Shell Signal HandlerChild fork() addjob() execve() exit() sigchld_handler() deletejobs() time

A problematic schedule Shell Signal HandlerChild fork() execve() exit() sigchld_handler() deletejobs() time addjob() Job added to job list after the signal handler tried to delete it!

Solution: blocking signals sigchld_handler() { pid = waitpid(…); deletejob(pid); } eval() { sigprocmask(SIG_BLOCK, …) pid = fork(); if(pid == 0) { /* child */ sigprocmask(SIG_UNBLOCK, …) execve(…); } /* parent */ /* signal handler might run BEFORE addjob() */ addjob(…); sigprocmask(SIG_UNBLOCK, …) }

I/O redirection Do it before call execve() in child process eval() { pid = fork(); if(pid == 0) { /* child */ /* Redirect I/O */ if (redirect_input) dup2(…); if (redirect_output) dup2(…); execve(…); } addjob(…); }

dup2(int oldfd, int newfd) Covered in Chapter 11 oldfd : old file descriptor newfd : new file descriptor Some examples: Get input from my_infd instead of standard input dup2(my_infd, STDIN_FILENO); Print output to my_outfd instead of standard output dup2(my_outfd, STDOUT_FILENO);

Reminders Some important system calls: fork(), execve(), waitpid(), sigprocmask(), setpgid(), kill() … Check man pages for details about system calls man 2 kill Check return values of all system calls STEP by STEP Test your shell by typing commands first I/O redirection: last step (15 pts out of 135 pts) Start now!

Virtual memory One of the most important concepts in CS Why use virtual memory (VM) Use RAM as a cache for disk Easier memory management Access protection Enable “partial swapping” Share memory efficiently