Recitation: Signaling 15213-S04, Recitation, Section A

Post on 02-Feb-2016

72 views 0 download

Tags:

description

Recitation: Signaling 15213-S04, Recitation, Section A. Debug Multiple Processes using GDB Dup2 Signaling L5 Due: This Wednesday. Debug Multiple Proc’s Using GDB. attach pid pid: the process id of a running process set follow_fork_mode - PowerPoint PPT Presentation

Transcript of Recitation: Signaling 15213-S04, Recitation, Section A

Recitation: SignalingRecitation: Signaling15213-S04, Recitation, Section A15213-S04, Recitation, Section A

Debug Multiple Processes using GDBDebug Multiple Processes using GDB

Dup2Dup2

SignalingSignaling

L5 Due: L5 Due: This WednesdayThis Wednesday

– 2 – 15-213, S’04

Debug Multiple Proc’s Using GDB

attach pidattach pid pid: the process id of a running process

set 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’04

Attach to a Running Process

1.1. Run the parent code and create the process Run the parent code and create the process tshtsh Directly in the shell In GDB

2.2. Get the pidGet the pid $ ps [-e | axu] | grep tsh

3.3. Run gdbRun gdb $ gdb tsh

4.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 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 You have to type in the gdb & attach commands fast enough --- before the process actually finishesenough --- 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 troublesomeFor Lab 5, it is more troublesome sdriver runtrace tsh/tshref mycat

– 5 – 15-213, S’04

Attach to a Running Process

1.1. Run Run runtraceruntrace and create the process and create the process tshtsh Directly in the shell In GDB

2.2. Get the pidGet the pid $ ps [-e | axu] | grep tsh

3.3. Run gdbRun gdb $ gdb tsh

4.4. Attach to the processAttach to the process (gdb) attach <pid>

$ gdb tsh <pid>

– 6 – 15-213, S’04

Dup2()

Basic concepts on file handlerBasic concepts on file handler File descriptor, file table, v-node table

File sharing --- dup2()File sharing --- dup2()

Practice problemsPractice problems

– 7 – 15-213, S’04

How the Unix Kernel Represents Open Files

Two 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 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

...

stderrstdoutstdin File access

...

File size

File type

File access

...

File size

File type

File A (terminal)

File B (disk)

Info in stat struct

– 8 – 15-213, S’04

How Processes Share Files

A 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 forkfork

fd 0fd 1fd 2fd 3fd 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 0fd 1fd 2fd 3fd 4

Child's table

File access

...

File size

File type

File access

...

File size

File type

File A

File B

– 9 – 15-213, S’04

File Sharing

Two 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 entries E.g., Calling open twice with the same filename argument

fd 0fd 1fd 2fd 3fd 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 – 15-213, S’04

I/O Redirectiondup2(oldfd, newfd)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 tablebefore dup2(4,1)

b

b

fd 0

fd 1

fd 2

fd 3

fd 4

Descriptor tableafter dup2(4,1)

– 11 – 15-213, S’04

I/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 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

...

stderrstdoutstdin File access

...

File size

File type

File access

...

File size

File type

File A

File B

– 12 – 15-213, S’04

I/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 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 – 15-213, S’04

File Sharing

Descriptor tableDescriptor table Each process has its own Child inherits from parents

File TableFile 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 tableV-node table Contains information in the stat structure Shared by all processes

– 14 – 15-213, S’04

Problem 11.2

Suppose 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 = %c\n", c); exit(0);}

– 15 – 15-213, S’04

Answer to 11.2

The descriptors The descriptors fd1 and and fd2 each have their own open each have their own open file table entry, so each descriptor has its own file file table entry, so each descriptor has its own file position for position for foobar.txt. Thus, the read from . Thus, the read from fd2 reads the first byte of reads the first byte of foobar.txt, and the output is, and the output is

c = f

and notand not

c = o

as you might have thought initially.as you might have thought initially.

– 16 – 15-213, S’04

Problem 11.3

As before, suppose As before, suppose foobar.txt consists of 6 ASCII consists of 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 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 – 15-213, S’04

Answer to 11.3

Child inherit’s the parent’s descriptor table. So child Child inherit’s the parent’s descriptor table. So child and parent share an open file table entry (refcount = 2). and parent share an open file table entry (refcount = 2). Hence they share a file position.Hence they share a file position.

c = o

– 18 – 15-213, S’04

Problem 11.4

How would you use dup2 to redirect standard input to How would you use dup2 to redirect standard input to descriptor 5?descriptor 5?

int dup2(int oldfd, int newfd);int dup2(int oldfd, int newfd); copies descriptor table entry oldfd to descriptor table entry newfd

– 19 – 15-213, S’04

Answer to 11.4

dup2(5,0);

oror

dup2(5,STDIN_FILENO);

– 20 – 15-213, 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 – 15-213, S’04

Answer to 11.5

We are redirecting We are redirecting fd1 to to fd2. (fd1 now points to the . (fd1 now points to the same open file table entry as fd2). So the second same open file table entry as fd2). So the second Read uses the file position offset of uses the file position offset of fd2..

c = o

– 22 – 15-213, S’04

Signaling

Busy waitBusy wait

waitpid()waitpid()

Racing hazardRacing hazard

– 23 – 15-213, S’04

Busy Wait

if(fork() != 0) { /* parent */

addjob(…);

while(fg process still alive){

/* do nothing */

}

}

– 24 – 15-213, 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 – 15-213, S’04

Sleep

if(fork() != 0) { /* parent */

addjob(…);

while(fg process still alive){

sleep(1);

}

}

– 26 – 15-213, S’04

waitpid ()

pid_t waitpid(pid_t pid, int *status, int options)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 -1WUNTRACED: report status of stopped children too

– 27 – 15-213, S’04

Status in Waitpid

int status;

waitpid(pid, &status, NULL);

Macros to evaluate status: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 – 15-213, S’04

Race Hazard

A data structure is shared by two pieces of code that A data structure is shared by two pieces of code that can run concurrentlycan run concurrently

Different behaviors of program depending upon how Different behaviors of program depending upon how the schedule interleaves the execution of code.the schedule interleaves the execution of code.

– 29 – 15-213, 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 – 15-213, S’04

Shell Signal Handler Child

fork()

addjob()

execve()

exit()

sigchld_handler()

deletejobs()

time

An OK Schedule

– 31 – 15-213, S’04

Shell Signal Handler Child

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 – 15-213, S’04

Blocking Signalssigchld_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 8.5.6 (page 633)

– 33 – 15-213, 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 – 15-213, 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 – 15-213, S’04

Summary

Debug Multiple Processes using GDBDebug Multiple Processes using GDB

Dup2Dup2

SignalingSignaling