Processes, Signals, I/O, Shell lab

Slides:



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

Recitation 8: 10/28/02 Outline Processes Signals –Racing Hazard –Reaping Children Annie Luo Office Hours: Thursday 6:00.
More on Processes Chapter 3. Process image _the physical representation of a process in the OS _an address space consisting of code, data and stack segments.
1 CS345 Operating Systems Φροντιστήριο Άσκησης 1.
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.
15-213/ Intro to Computer Systems by btan with reference to Spring 10’s slides.
Processes Topics Process context switches Creating and destroying processes CS 105 “Tour of the Black Holes of Computing!”
UNIX Process Control Bach 7 Operating Systems Course Hebrew University Spring 2007.
1 Processes Professor Jennifer Rexford
1 Processes and Pipes COS 217 Professor Jennifer Rexford.
CS 311 – Lecture 14 Outline Process management system calls Introduction System calls  fork()  getpid()  getppid()  wait()  exit() Orphan process.
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.
Process Process: the UNIX abstraction of a stand-along computer that manages resources (memory, CPU, I/O resources) comprising a running program. Processes.
Advanced OS Chapter 3p2 Sections 3.4 / 3.5. Interrupts These enable software to respond to signals from hardware. The set of instructions to be executed.
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.
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.
CSE 451 Section 4 Project 2 Design Considerations.
Processes in Unix, Linux, and Windows CS-502 Fall Processes in Unix, Linux, and Windows CS502 Operating Systems (Slides include materials from Operating.
Carnegie Mellon 1 Processes, Signals, I/O, Shell Lab : Introduction to Computer Systems Recitation 9: 10/21/2013 Tommy Klein Section B.
Process in Unix, Linux, and Windows CS-3013 A-term Processes in Unix, Linux, and Windows CS-3013 Operating Systems (Slides include materials from.
Introduction to Processes CS Intoduction to Operating Systems.
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.
Operating Systems Chapter 2
Lecture 3 Process Concepts. What is a Process? A process is the dynamic execution context of an executing program. Several processes may run concurrently,
Creating and Executing Processes
CS162B: Pipes Jacob T. Chan. Pipes  These allow output of one process to be the input of another process  One of the oldest and most basic forms of.
System calls for Process management
Unix Process Model Simple and powerful primitives for process creation and initialization. fork syscall creates a child process as (initially) a clone.
Scis.regis.edu ● CS 468: Advanced UNIX Class 5 Dr. Jesús Borrego Regis University 1.
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Processes and Threads.
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,
Outline for Today Objectives –Finish discussion of Birrell –UNIX Signals –Eraser Administrative –Spider talk after class.
1 Signals (continued) CS 241 April 9, 2012 University of Illinois.
UNIX Signals * POSIX-Defined Signals * Signaling Processes * Signal Mask * sigaction * kill and sigaction * alarm * Interval Timers * POSIX.1b Timers *
Operating Systems Recitation 4, April th, 2002 Signals.
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.
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.
System calls for Process management Process creation, termination, waiting.
1 Exceptional Control Flow Ⅱ. 2 Outline Kernel Mode and User Mode Process context switches Three States of Processes Context Switch System Calls and Error.
1 Lecture 19: Unix signals and Terminal management n what is a signal n signal handling u kernel u user n signal generation n signal example usage n terminal.
CS241 Systems Programming Discussion Section Week 2 Original slides by: Stephen Kloder.
CS241 Systems Programming Discussion Section Week 2 Original slides by: Stephen Kloder.
S ALVATORE DI G IROLAMO (TA) Networks and Operating Systems: Exercise Session 1.
Process Manipulation. Process Manipulation in UNIX Basic process manipulation: creation, program loading, exiting, … fork(), exec(), wait(), exit() Process.
Recitation 7 (Oct. 25) Outline Minglong Shao Office hours: Reminders
Section 8: Processes What is a process Creating processes Fork-Exec
Unix Process Management
UNIX PROCESSES.
Exceptional Control Flow Part II
Processes in Unix, Linux, and Windows
Lecture 5: Process Creation
Exceptional Control Flow
LINUX System Programming with Processes (additional)
Processes in Unix, Linux, and Windows
Exceptional Control Flow
CS 105 “Tour of the Black Holes of Computing!”
CS 105 “Tour of the Black Holes of Computing!”
Lecture 6: Multiprogramming and Context Switching
Exceptional Control Flow
CS510 Operating System Foundations
Presentation transcript:

Processes, Signals, I/O, Shell lab 15-213: Introduction to Computer Systems Recitation 9: Monday, October 20th, 2014 Sajjan

Agenda Processes Signals I/O Intro Shell Lab General

Processes An instance of an executing program Abstraction provided by the operating system Properties Private memory No two processes share memory, registers, etc. Some state is shared, such as open file table Have a process ID and process group ID pid,pgid Become zombies when finished running

Processes Four basic process control function families: fork() exec() And other variants such as execve() exit() wait() And variants like waitpid() Standard on all UNIX-based systems Don’t be confused: Fork(), Exit(), Wait() are all wrappers provided by CS:APP

Processes int fork(void) creates a new process (child process) that is identical to the calling process (parent process) OS creates an exact duplicate of parent’s state: Virtual address space (memory), including heap and stack Registers, except for the return value (%eax/%rax) File descriptors of files are copied into child process Result  Equal but separate state Fork is interesting (and often confusing) because it is called once but returns twice

Processes int fork(void) returns 0 to the child process returns child’s pid (process id) to the parent process Usually used like: pid_t pid = fork(); if (pid == 0) { // pid is 0 so we can detect child printf("hello from child\n"); } else { // pid = child’s assigned pid printf("hello from parent\n");

Processes int exec() Replaces the current process’s state and context But keeps PID, open files, and signal context Provides a way to load and run another program Replaces the current running memory image with that of new program Set up stack with arguments and environment variables Start execution at the entry point Never returns on successful execution The newly loaded program’s perspective: as if the previous program has not been run before More useful variant is int execve() More information? man 3 exec

Processes void exit(int status) Normally return with status 0 (other numbers indicate an error) Terminates the current process OS frees resources such as heap memory and open file descriptors and so on… Reduce to a zombie state Must wait to be reaped by the parent process (or the init process if the parent died) Signal is sent to the parent process notifying of death Reaper can inspect the exit status

Processes int wait(int *child_status) suspends current process until one of its children terminates return value is the pid of the child process that terminated When wait returns a pid > 0, child process has been reaped All child resources freed if child_status != NULL, then the object it points to will be set to a status indicating why the child process terminated More useful variant is int waitpid() For details: man 2 wait

Process Examples What are the possible output (assuming fork succeeds) ? Child! Parent! Parent! Child! How to get the child to always print first? pid_t child_pid = fork(); if (child_pid == 0){ /* only child comes here */ printf(“Child!\n”); exit(0); } else{ printf(“Parent!\n”); }

Process Examples Waits til the child has terminated. Parent can inspect exit status of child using ‘status’ WEXITSTATUS(status) Output always: Child! Parent! int status; pid_t child_pid = fork(); if (child_pid == 0){ /* only child comes here */ printf(“Child!\n”); exit(0); } else{ waitpid(child_pid, &status, 0); printf(“Parent!\n”); }

Signals A signal is a small message that notifies a process that an event of some type has occurred in the system akin to exceptions and interrupts (asynchronous) sent from the kernel (sometimes at the request of another process) to a process signal type is identified by small integer ID’s (1-30) only information in a signal is its ID and the fact that it arrived ID Name Default Action Corresponding Event 2 SIGINT Terminate Interrupt (e.g., ctl-c from keyboard) 9 SIGKILL Kill program (cannot override or ignore) 11 SIGSEGV Terminate & Dump Segmentation violation 14 SIGALRM Timer signal 17 SIGCHLD Ignore Child stopped or terminated

Signals Kernel sends (delivers) a signal to a destination process by updating some state in the context of the destination process Kernel sends a signal for one of the following reasons: Kernel has detected a system event such as Ctrl-C (SIGINT), divide-by-zero (SIGFPE), or the termination of a child process (SIGCHLD) Another program called the kill() function The user used a kill utility

Signals A destination process receives a signal when it is forced by the kernel to react in some way to the delivery of the signal Receiving a signal is non-queuing There is only one bit in the context per signal Receiving 1 or 300 SIGINTs looks the same to the process Signals are received at a context switch Three possible ways to react: Ignore the signal (do nothing) Terminate the process (with optional core dump) Catch the signal by executing a user-level function called signal handler Akin to a hardware exception handler being called in response to an asynchronous interrupt

Signals A destination process receives a signal when it is forced by the kernel to react in some way to the delivery of the signal Blocking signals Sometimes code needs to run through a section that can’t be interrupted Implemented with sigprocmask() Waiting for signals Sometimes, we want to pause execution until we get a specific signal Implemented with sigsuspend() Can’t modify behavior of SIGKILL and SIGSTOP

Signals Signal handlers Can be installed to run when a signal is received The form is void handler(int signum){ … } Separate flow of control in the same process Resumes normal flow of control upon returning Can be called anytime when the appropriate signal is fired

Signal Examples Every process belongs to exactly one process group Process groups can be used to distribute signals easily A forked process becomes a member of the parent’s process group Fore- ground job Back- job #1 job #2 Shell Child pid=10 pgid=10 Foreground process group 20 Background process group 32 process group 40 pid=20 pgid=20 pid=32 pgid=32 pid=40 pgid=40 pid=21 pid=22 getpgrp() Return process group of current process setpgid() Change process group of a process

Signal Examples Does add_job or remove_job() come first? // sigchld handler installed pid_t child_pid = fork(); if (child_pid == 0){ /* child comes here */ execve(……); } else{ add_job(child_pid); } void sigchld_handler(int signum) { int status; pid_t child_pid = waitpid(-1, &status, WNOHANG); if (WIFEXITED(status)) remove_job(child_pid); } Does add_job or remove_job() come first? Where can we block signals in this code to guarantee correct execution?

Signal Examples Does add_job or remove_job() come first? // sigchld handler installed pid_t child_pid = fork(); if (child_pid == 0){ /* child comes here */ execve(……); } else{ add_job(child_pid); } Block SIGCHLD void sigchld_handler(int signum) { int status; pid_t child_pid = waitpid(-1, &status, WNOHANG); if (WIFEXITED(status)) remove_job(child_pid); } Unblock SIGCHLD Unblock SIGCHLD Does add_job or remove_job() come first? Where can we block signals in this code to guarantee correct execution?

Unix I/O Unix processes use descriptors to reference i/o streams. File descriptors are unsigned integers obtained from open and socket system calls. dup, dup2 system calls are used to duplicate a file descriptor. int dup2(int oldfd, int newfd) newfd becomes a copy of oldfd Read/write on newfd will access the file corresponding to oldfd Every process starts with 3 file descriptors by default 0: STDIN 1: STDOUT 2: STDERR

Shell Lab Before starting the lab read chapter 8 and chapter 10 from the book. Make sure you understand every line from chapter 8. Read the code we’ve given you There’s a lot of stuff you don’t need to write yourself; we gave you quite a few helper functions It’s a good example of the code we expect from you! Don’t be afraid to write your own helper functions; this is not a simple assignment

Shell Lab Please do not use sleep() to solve synchronization issues. Read man pages. You may find the following functions helpful: sigemptyset() sigaddset() sigprocmask() sigsuspend() waitpid() open() dup2() setpgid() kill()

Shell lab Don’t forget to close any open file descriptors after call to dup2 Make sure you have error checking code for any system call or function you write Hazards Race conditions Hard to debug so start early (and think carefully) Reaping zombies Handling signals correctly Waiting for foreground job Think carefully about what the right way to do this is

Thank you

Extra Slides

Processes Four basic States Running Executing instructions on the CPU Number bounded by number of CPU cores Runnable Waiting to be running Blocked Waiting for an event, maybe input from STDIN Not runnable Zombie Terminated, not yet reaped

Process Examples Unix Process Hierarchy: [0] init [1] Daemon e.g. httpd Login shell Child Child Child Grandchild Grandchild

Process Examples An example of something useful. Why is the first arg “/bin/ls”? Will child reach here? int status; pid_t child_pid = fork(); char* argv[] = {“/bin/ls”, “-l”, NULL}; char* env[] = {…, NULL}; if (child_pid == 0){ /* only child comes here */ execve(“/bin/ls”, argv, env); /* will child reach here? */ } else{ waitpid(child_pid, &status, 0); … parent continue execution… }

Signal Examples Process A Process B Signal delivered Signal received Icurr user code (main) kernel code context switch user code (main) kernel code context switch Signal received user code (handler) kernel code Inext user code (main)