Recitation: Signaling 15213-S04, Recitation, Section A Debug Multiple Processes using GDB Debug Multiple Processes using GDB Dup2 Dup2 Signaling Signaling.

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.
1 CS345 Operating Systems Φροντιστήριο Άσκησης 1.
Last Lab: Writing a shell
Chapter Fourteen Process Management1 System Programming Process Management.
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.
UNIX Process Control Bach 7 Operating Systems Course Hebrew University Spring 2007.
System-Level I/O Topics Unix I/O Robust reading and writing Reading file metadata Sharing files I/O redirection Standard I/O.
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.
Exec function Exec function: - replaces the current process (its code, data, stack & heap segments) with a new program - the new program starts executing.
Process Control in Unix Operating Systems Hebrew University Spring 2004.
CS Lecture 16 Outline Inter-process Communication (IPC) – Pipes – Signals Lecture 161CS Operating Systems 1.
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, Signals, I/O, Shell lab
Recitation 11: I/O Problems Andrew Faulring Section A 18 November 2002.
Carnegie Mellon 1 Processes, Signals, I/O, Shell Lab : Introduction to Computer Systems Recitation 9: 10/21/2013 Tommy Klein Section B.
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.
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.
Fundamentals CIS 552. Fundamentals Low-level I/O (read/write using system calls)  Opening/Creating files  Reading & Writing files  Moving around in.
1 System-Level I/O Andrew Case Slides adapted from Jinyang Li, Randy Bryant and Dave O’Hallaron.
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).
Cli/Serv.: procs/51 Client/Server Distributed Systems v Objectives –look at how to program UNIX processes , Semester 1, Processes.
1 Week 2 The Crunchy Shell to the Soft and Chewy Kernel… Sarah Diesburg 8/3/2010 COP4610 / CGS5765.
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.
Recitation 11: 11/18/02 Outline Robust I/O Chapter 11 Practice Problems Annie Luo Office Hours: Thursday 6:00 – 7:00 Wean.
Creating and Executing Processes
CS252: Systems Programming Ninghui Li Based on Slides by Prof. Gustavo Rodriguez-Rivera Topic 8: Opening Files and Starting 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.
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.
Process Control Process identifiers Process creation fork and vfork wait and waitpid Race conditions exec functions system function.
System calls for Process management
1 IT 252 Computer Organization and Architecture Interaction Between Systems: File Sharing R. Helps.
Operating Systems Process Creation
Recitation 11 (Nov. 22) Outline Lab 6: interposition test Error handling I/O practice problem Reminders Lab 6: Due Tuesday Minglong Shao
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.
OS Labs 2/25/08 Frans Kaashoek MIT
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.
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.
1 Intro to the Shell with Fork, Exec, Wait Sarah Diesburg Operating Systems CS 3430.
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.
Week 3 Redirection, Pipes, and Background
Recitation 7 (Oct. 25) Outline Minglong Shao Office hours: Reminders
Robust I/O package Chapter 11 practice problems
CS 3305A Process – Part II Lecture 4 Sept 20, 2017.
LINUX System : Lecture 8 Programming with Processes
Virtual Memory : Introduction to Computer Systems Recitation 10: Nov. 2, 2015 Karthic Palaniappan.
Unix Process Management
UNIX PROCESSES.
LINUX System Programming with Processes (additional)
Programming Assignment # 2 – Supplementary Discussion
Virtual Memory CSCI 380: Operating Systems Lecture #7 -- Review and Lab Suggestions William Killian.
IPC Prof. Ikjun Yeom TA – Hoyoun
CSCI 380: Operating Systems William Killian
Section 3 Syscalls, I/O, Signals February 3rd, 2017 Taught by Josh Don.
Recitation 9: Processes, Signals, TSHLab
System Programming: Process Management
Presentation transcript:

Recitation: Signaling S04, Recitation, Section A Debug Multiple Processes using GDB Debug Multiple Processes using GDB Dup2 Dup2 Signaling Signaling L5 Due: This Wednesday L5 Due: This Wednesday

– 2 – , S’04 Debug Multiple Proc’s Using GDB attach pid attach pid pid: the process id of a running process set follow_fork_mode set follow_fork_mode only work in HP-UX and GNU/Linux (kernel >= ) Fish machines: Linux with kernel

– 3 – , S’04 Attach to a Running Process  Run the parent code and create the process tsh Directly in the shell In GDB  Get the pid $ ps [-e | axu] | grep tsh  Run gdb $ gdb tsh  Attach to the process (gdb) attach $ gdb tsh

– 4 – , S’04 Demo! Really that Easy? The process must be started outside GDB The process must be started outside GDB If you want to debug both the parent process and the child process, you need start another gdb (in another xterm) You have to type in the gdb & attach commands fast enough --- before the process actually finishes You have to type in the gdb & attach commands fast enough --- before the process actually finishes You have to modify the source code to let it wait Two methods sleep(10); int gdbf = 0; while (!gdbf); For Lab 5, it is more troublesome For Lab 5, it is more troublesome sdriver  runtrace  tsh/tshref  mycat

– 5 – , S’04 Attach to a Running Process  Run runtrace and create the process tsh Directly in the shell In GDB  Get the pid $ ps [-e | axu] | grep tsh  Run gdb $ gdb tsh  Attach to the process (gdb) attach $ gdb tsh

– 6 – , S’04 Dup2() Basic concepts on file handler Basic concepts on file handler File descriptor, file table, v-node table File sharing --- dup2() File sharing --- dup2() Practice problems Practice problems

– 7 – , S’04 How the Unix Kernel Represents Open Files Two descriptors referencing two distinct open disk files. Descriptor 1 (stdout) points to terminal, and descriptor 4 points to open disk file. fd 0 fd 1 fd 2 fd 3 fd 4 Descriptor table [one table per process] Open file table [shared by all processes] v-node table [shared by all processes] File pos refcnt=1... File pos refcnt=1... stderr stdout stdin File access... File size File type File access... File size File type File A (terminal) File B (disk) Info in stat struct

– 8 – , S’04 How Processes Share Files A child process inherits its parent’s open files. Here is the situation immediately after a fork fd 0 fd 1 fd 2 fd 3 fd 4 Descriptor tables Open file table (shared by all processes) v-node table (shared by all processes) File pos refcnt=2... File pos refcnt=2... Parent's table fd 0 fd 1 fd 2 fd 3 fd 4 Child's table File access... File size File type File access... File size File type File A File B

– 9 – , S’04 File Sharing Two distinct descriptors sharing the same disk file through two distinct open file table entries E.g., Calling open twice with the same filename argument fd 0 fd 1 fd 2 fd 3 fd 4 Descriptor table (one table per process) Open file table (shared by all processes) v-node table (shared by all processes) File pos refcnt=1... File pos refcnt=1... File access... File size File type File A File B

– 10 – , S’04 I/O Redirection dup2(oldfd, newfd) Copies (per-process) descriptor table entry oldfd to entry newfd a b fd 0 fd 1 fd 2 fd 3 fd 4 Descriptor table before dup2(4,1) b b fd 0 fd 1 fd 2 fd 3 fd 4 Descriptor table after dup2(4,1)

– 11 – , S’04 I/O Redirection Example Before calling dup2(4,1), stdout (descriptor 1) points to a terminal and descriptor 4 points to an open disk file. Before calling dup2(4,1), stdout (descriptor 1) points to a terminal and descriptor 4 points to an open disk file. fd 0 fd 1 fd 2 fd 3 fd 4 Descriptor table (one table per process) Open file table (shared by all processes) v-node table (shared by all processes) File pos refcnt=1... File pos refcnt=1... stderr stdout stdin File access... File size File type File access... File size File type File A File B

– 12 – , S’04 I/O Redirection Example (cont) After calling dup2(4,1), stdout is now redirected to the disk file pointed at by descriptor 4. fd 0 fd 1 fd 2 fd 3 fd 4 Descriptor table (one table per process) Open file table (shared by all processes) v-node table (shared by all processes) File pos refcnt=0... File pos refcnt=2... File access... File size File type File access... File size File type File A File B

– 13 – , S’04 File Sharing Descriptor table Each process has its own Child inherits from parents File Table set of all open files Shared by all processes Reference count of number of file descriptors pointing to each entry File position V-node table Contains information in the stat structure Shared by all processes

– 14 – , S’04 Problem 11.2 Suppose that consists of the 6 ASCII characters "". Then what is the output of the following program? Suppose that foobar.txt consists of the 6 ASCII characters "foobar". Then what is the output of the following program? #include "csapp.h" int main() { int fd1, fd2; char c; fd1 = Open("foobar.txt", O_RDONLY, 0); fd2 = Open("foobar.txt", O_RDONLY, 0); Read(fd1, &c, 1); Read(fd2, &c, 1); printf("c = %c\n", c); exit(0); }

– 15 – , S’04 Answer to 11.2 The descriptors and each have their own open file table entry, so each descriptor has its own file position for. Thus, the read from reads the first byte of, and the output is and not as you might have thought initially. The descriptors fd1 and fd2 each have their own open file table entry, so each descriptor has its own file position for foobar.txt. Thus, the read from fd2 reads the first byte of foobar.txt, and the output is c = f and not c = o as you might have thought initially.

– 16 – , S’04 Problem 11.3 As before, suppose consists of 6 ASCII characters "". Then what is the output of the following program? As before, suppose foobar.txt consists of 6 ASCII characters "foobar". Then what is the output of the following program? #include "csapp.h" int main() { int fd; char c; fd = Open("foobar.txt", O_RDONLY, 0); if(Fork() == 0) {Read(fd, &c, 1); exit(0);} Wait(NULL); Read(fd, &c, 1); printf("c = %c\n", c); exit(0); }

– 17 – , S’04 Answer to 11.3 Child inherit’s the parent’s descriptor table. So child and parent share an open file table entry (refcount = 2). Hence they share a file position. c = o

– 18 – , S’04 Problem 11.4 How would you use dup2 to redirect standard input to descriptor 5? int dup2(int oldfd, int newfd); copies descriptor table entry oldfd to descriptor table entry newfd

– 19 – , S’04 Answer to 11.4 dup2(5,0);or dup2(5,STDIN_FILENO);

– 20 – , S’04 Problem 11.5 Assuming that foobar.txt consists of 6 ASCII characters “foobar”. Then what is the output of the following program? #include "csapp.h" int main() { int fd1, fd2; char c; fd1 = Open("foobar.txt", O_RDONLY, 0); fd2 = Open("foobar.txt", O_RDONLY, 0); Read(fd2, &c, 1); Dup2(fd2, fd1); Read(fd1, &c, 1); printf("c = %c\n", c); exit(0); }

– 21 – , S’04 Answer to 11.5 We are redirecting to. (fd1 now points to the same open file table entry as fd2). So the second uses the file position offset of. We are redirecting fd1 to fd2. (fd1 now points to the same open file table entry as fd2). So the second Read uses the file position offset of fd2. c = o

– 22 – , S’04 Signaling Busy wait Busy wait waitpid() waitpid() Racing hazard Racing hazard

– 23 – , S’04 Busy Wait if(fork() != 0) { /* parent */ addjob(…); while(fg process still alive){ /* do nothing */ }

– 24 – , S’04 Pause if(fork() != 0) { /* parent */ addjob(…); while(fg process still alive){ pause(); } If signal handled before call to pause, then pause will not return when foreground process sends SIGCHLD

– 25 – , S’04 Sleep if(fork() != 0) { /* parent */ addjob(…); while(fg process still alive){ sleep(1); }

– 26 – , S’04 waitpid () pid_t waitpid(pid_t pid, int *status, int options) pid: wait until child process with pid has terminated -1: wait for any child process status: tells why child terminated options: WNOHANG: return immediately if no children zombied »returns -1 WUNTRACED: report status of stopped children too

– 27 – , S’04 Status in Waitpid int status; waitpid(pid, &status, NULL); Macros to evaluate status: WIFEXITED(status): child exited normally WEXITSTATUS(status): return code when child exits WIFSIGNALED(status): child exited because of a signal not caught WTERMSIG(status): gives the terminating signal number WIFSTOPPED(status): child is currently stopped WSTOPSIG(status): gives the stop signal number

– 28 – , S’04 Race Hazard A data structure is shared by two pieces of code that can run concurrently 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. Different behaviors of program depending upon how the schedule interleaves the execution of code.

– 29 – , S’04 eval & sigchld_handler Race Hazard sigchld_handler() { pid = waitpid(…); deletejob(pid); } eval() { pid = fork(); if(pid == 0) { /* child */ execve(…); } /* parent */ /* signal handler might run BEFORE addjob() */ addjob(…); }

– 30 – , S’04 Shell Signal HandlerChild fork() addjob() execve() exit() sigchld_handler() deletejobs() time An OK Schedule

– 31 – , S’04 Shell Signal HandlerChild fork() execve() exit() sigchld_handler() deletejobs() time addjob() Job added to job list after the signal handler tried to delete it! A Problematic Schedule

– 32 – , S’04 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, …) } More details (page 633)

– 33 – , S’04 Blocking Signals sigprocmask(SIG_BLOCK, (sigset_t *)SIGCHLD, NULL); sigprocmask(SIG_BLOCK, (sigset_t *)SIGINT, NULL); sigprocmask(SIG_BLOCK, (sigset_t *)SIGTSTP, NULL); sigemptyset(&mask); sigaddset(&mask, SIGCHLD); sigaddset(&mask, SIGINT); sigaddset(&mask, SIGTSTP); sigprocmask(SIG_BLOCK, &mask, NULL); x

– 34 – , S’04 Blocking Signals if (sigemptyset(&mask) < 0) unix_error("sigemptyset error"); if (sigaddset(&mask, SIGCHLD)) unix_error("sigaddset error"); if (sigaddset(&mask, SIGINT)) unix_error("sigaddset error"); if (sigaddset(&mask, SIGTSTP)) unix_error("sigaddset error"); if (sigprocmask(SIG_BLOCK, &mask, NULL) < 0) unix_error("sigprocmask error");

– 35 – , S’04 Summary Debug Multiple Processes using GDB Debug Multiple Processes using GDB Dup2 Dup2 Signaling Signaling