Recitation: Signaling 15213-S04, Recitation, Section ADebug Multiple Proc’s Using GDBAttach to a Running ProcessReally that Easy?Slide 5Dup2()How the Unix Kernel Represents Open FilesHow Processes Share FilesFile SharingI/O RedirectionI/O Redirection ExampleI/O Redirection Example (cont)Slide 13Problem 11.2Answer to 11.2Problem 11.3Answer to 11.3Problem 11.4Answer to 11.4Problem 11.5Answer to 11.5SignalingBusy WaitPauseSleepwaitpid ()Status in WaitpidRace Hazardeval & sigchld_handler Race HazardAn OK ScheduleA Problematic ScheduleBlocking SignalsBlocking SignalsSlide 34SummaryRecitation: SignalingRecitation: Signaling15213-S04, Recitation, Section A15213-S04, Recitation, Section ADebug Multiple Processes using GDBDebug Multiple Processes using GDBDup2Dup2SignalingSignalingL5 Due: L5 Due: This WednesdayThis Wednesday– 2 –15-213, S’04Debug Multiple Proc’s Using GDBattach pidattach pidpid: the process id of a running processset follow_fork_mode <parent | child>set follow_fork_mode <parent | child>only work in HP-UX and GNU/Linux (kernel >= 2.5.60)Fish machines: Linux with kernel 2.2.20– 3 –15-213, S’04Attach to a Running Process1.1.Run the parent code and create the process Run the parent code and create the process tshtshDirectly in the shellIn GDB2.2.Get the pidGet the pid$ ps [-e | axu] | grep tsh3.3.Run gdbRun gdb$ gdb tsh4.4.Attach to the processAttach to the process(gdb) attach <pid>$ gdb tsh <pid>– 4 –15-213, S’04Demo!Really that Easy?The process must be started The process must be started outsideoutside GDB GDBIf 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 You have to type in the gdb & attach commands fast enough --- before the process actually finishesenough --- before the process actually finishesYou have to modify the source code to let it waitTwo methodssleep(10);int gdbf = 0; while (!gdbf);For Lab 5, it is more troublesomeFor Lab 5, it is more troublesomesdriver runtrace tsh/tshref mycat– 5 –15-213, S’04Attach to a Running Process1.1.Run Run runtraceruntrace and create the process and create the process tshtshDirectly in the shellIn GDB2.2.Get the pidGet the pid$ ps [-e | axu] | grep tsh3.3.Run gdbRun gdb$ gdb tsh4.4.Attach to the processAttach to the process(gdb) attach <pid>$ gdb tsh <pid>– 6 –15-213, S’04Dup2()Basic concepts on file handlerBasic concepts on file handlerFile descriptor, file table, v-node tableFile sharing --- dup2()File sharing --- dup2()Practice problemsPractice problems– 7 –15-213, S’04How the Unix Kernel Represents Open FilesTwo descriptors referencing two distinct open disk Two descriptors referencing two distinct open disk files. Descriptor 1 (stdout) points to terminal, and files. Descriptor 1 (stdout) points to terminal, and descriptor 4 points to open disk file.descriptor 4 points to open disk file.fd 0fd 1fd 2fd 3fd 4Descriptor table[one table per process]Open file table [shared by all processes]v-node table[shared by all processes]File posrefcnt=1...File posrefcnt=1...stderrstdoutstdinFile access...File sizeFile typeFile access...File sizeFile typeFile A (terminal)File B (disk)Info in stat struct– 8 –15-213, S’04How Processes Share FilesA child process inherits its parent’s open files. Here is A child process inherits its parent’s open files. Here is the situation immediately after a the situation immediately after a forkforkfd 0fd 1fd 2fd 3fd 4Descriptor tablesOpen file table (shared by all processes)v-node table(shared by all processes)File posrefcnt=2...File posrefcnt=2...Parent's tablefd 0fd 1fd 2fd 3fd 4Child's tableFile access...File sizeFile typeFile access...File sizeFile typeFile AFile B– 9 –15-213, S’04File SharingTwo distinct descriptors sharing the same disk file Two distinct descriptors sharing the same disk file through two distinct open file table entriesthrough two distinct open file table entriesE.g., Calling open twice with the same filename argumentfd 0fd 1fd 2fd 3fd 4Descriptor table(one table per process)Open file table (shared by all processes)v-node table(shared by all processes)File posrefcnt=1...File posrefcnt=1...File access...File sizeFile typeFile AFile B– 10 –15-213, S’04I/O Redirectiondup2(oldfd, newfd)dup2(oldfd, newfd)Copies (per-process) descriptor table entry oldfd to entry newfdabfd 0fd 1fd 2fd 3fd 4Descriptor tablebefore dup2(4,1)bbfd 0fd 1fd 2fd 3fd 4Descriptor tableafter dup2(4,1)– 11 –15-213, S’04I/O Redirection Example Before calling Before calling dup2(4,1)dup2(4,1), stdout (descriptor 1) points , stdout (descriptor 1) points to a terminal and descriptor 4 points to an open disk to a terminal and descriptor 4 points to an open disk file.file.fd 0fd 1fd 2fd 3fd 4Descriptor table(one table per process)Open file table (shared by all processes)v-node table(shared by all processes)File posrefcnt=1...File posrefcnt=1...stderrstdoutstdinFile access...File sizeFile typeFile access...File sizeFile typeFile AFile B– 12 –15-213, S’04I/O Redirection Example (cont)After calling After calling dup2(4,1)dup2(4,1), stdout is now redirected to the , stdout is now redirected to the disk file pointed at by descriptor 4.disk file pointed at by descriptor 4.fd 0fd 1fd 2fd 3fd 4Descriptor table(one table per process)Open file table (shared by all processes)v-node table(shared by all processes)File posrefcnt=0...File posrefcnt=2...File access...File sizeFile typeFile access...File sizeFile typeFile AFile B– 13 –15-213, S’04File SharingDescriptor tableDescriptor tableEach process has its ownChild inherits from parentsFile TableFile Tableset of all open filesShared by all processesReference count of number of file descriptors pointing to each entryFile positionV-node tableV-node tableContains information in the stat structureShared by all processes– 14 –15-213, S’04Problem 11.2Suppose that Suppose that foobar.txt consists of the 6 ASCII consists of the 6 ASCII characters characters ""foobar"". Then what is the output of the . Then what is the output of the following program?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 =
View Full Document