35
CS143A Principles on Operating Systems Discussion 02: OS Interfaces Instructor: Prof. Anton Burtsev TA: Saehanseul Yi (Hans) Oct 16, 2020 Noon Revision: 3

CS143A Principles on Operating Systems Discussion 02: OS …aburtsev/143A/discussions/CS143... · 2020. 10. 17. · $ ls| grep asdf asdfasdf $ parent process fork1 child (left) fork1

  • Upload
    others

  • View
    3

  • Download
    0

Embed Size (px)

Citation preview

Page 1: CS143A Principles on Operating Systems Discussion 02: OS …aburtsev/143A/discussions/CS143... · 2020. 10. 17. · $ ls| grep asdf asdfasdf $ parent process fork1 child (left) fork1

CS143APrinciples on Operating Systems

Discussion 02:OS Interfaces

Instructor: Prof. Anton BurtsevTA: Saehanseul Yi (Hans)

Oct 16, 2020 Noon

Revision: 3

Page 2: CS143A Principles on Operating Systems Discussion 02: OS …aburtsev/143A/discussions/CS143... · 2020. 10. 17. · $ ls| grep asdf asdfasdf $ parent process fork1 child (left) fork1

About me

• Link for all office hours/discussion:https://uci.zoom.us/j/93369206818

• Teaching staff office hours:

Hari: Mon 12:00 PSTZhaofeng Li: Tue 12:00 PSTDeep: Wed 9:00 AM PSTHans: Thu 12:00 PSTSe-Min Lim: Fri 9:00 PST

Page 3: CS143A Principles on Operating Systems Discussion 02: OS …aburtsev/143A/discussions/CS143... · 2020. 10. 17. · $ ls| grep asdf asdfasdf $ parent process fork1 child (left) fork1

Motivating example: redirection

• Best example for explaining pipe(),fork(), and exec()• Program output -> stdout (default: screen)• | (pipe operator): send outputs to somewhere else

[email protected] 2

$$ lsa.out b.out asdfasdf$

$$ ls | grep asdfasdfasdf$

Page 4: CS143A Principles on Operating Systems Discussion 02: OS …aburtsev/143A/discussions/CS143... · 2020. 10. 17. · $ ls| grep asdf asdfasdf $ parent process fork1 child (left) fork1

[email protected] 3

Typical UNIX OS

(OS)

System calls are the interface of the OS

Lecture2 slides p3

Page 5: CS143A Principles on Operating Systems Discussion 02: OS …aburtsev/143A/discussions/CS143... · 2020. 10. 17. · $ ls| grep asdf asdfasdf $ parent process fork1 child (left) fork1

[email protected] 4

But what is shell?

● Normal process

● Kernel starts it for each user that logs in into the system

● In xv6 shell is created after the kernel boots

● Shell interacts with the kernel through system calls

● E.g., starts other processes

Lecture2 slides p14

Page 6: CS143A Principles on Operating Systems Discussion 02: OS …aburtsev/143A/discussions/CS143... · 2020. 10. 17. · $ ls| grep asdf asdfasdf $ parent process fork1 child (left) fork1

[email protected] 5

System calls, interface for...

● Processes

● Creating, exiting, waiting, terminating

● Memory

● Allocation, deallocation

● Files and folders

● Opening, reading, writing, closing

● Inter-process communication

● Pipes

Lecture2 slides p5

$$ ls | grep asdfasdfasdf$

parent process (shell)

Child process(left)

Child process(right)

parent process (shell)

Page 7: CS143A Principles on Operating Systems Discussion 02: OS …aburtsev/143A/discussions/CS143... · 2020. 10. 17. · $ ls| grep asdf asdfasdf $ parent process fork1 child (left) fork1

Wait… stdin? stdout?

[email protected] 6

$$ ls | grep asdfasdfasdf$(standard input, standard output)

IN OUTProcess

stdi

n

stdout

Page 8: CS143A Principles on Operating Systems Discussion 02: OS …aburtsev/143A/discussions/CS143... · 2020. 10. 17. · $ ls| grep asdf asdfasdf $ parent process fork1 child (left) fork1

Wait… stdin? stdout?

[email protected] 7

$$ ls | grep asdfasdfasdf$(standard input, standard output)

ProcessIN OUT

stdi

n

stdout

lsIN OUT

stdi

n

stdout

grepIN OUT

stdi

n

stdout

Page 9: CS143A Principles on Operating Systems Discussion 02: OS …aburtsev/143A/discussions/CS143... · 2020. 10. 17. · $ ls| grep asdf asdfasdf $ parent process fork1 child (left) fork1

Wait… stdin? stdout?

[email protected] 8

$$ ls | grep asdfasdfasdf$(standard input, standard output)

ProcessIN OUT

stdi

n

stdout

lsIN OUT

stdi

n

stdout

grepIN OUT

stdi

n

stdout

Page 10: CS143A Principles on Operating Systems Discussion 02: OS …aburtsev/143A/discussions/CS143... · 2020. 10. 17. · $ ls| grep asdf asdfasdf $ parent process fork1 child (left) fork1

Wait… stdin? stdout?

[email protected] 9

$$ ls | grep asdfasdfasdf$(standard input, standard output)

ProcessIN OUT

stdi

n

stdout

lsIN OUT

stdi

n

stdout

grepIN OUT

stdi

n

stdout

• stdin(0), stdout(1), and stderr(2) are file descriptors(just an integer in user-program)

Page 11: CS143A Principles on Operating Systems Discussion 02: OS …aburtsev/143A/discussions/CS143... · 2020. 10. 17. · $ ls| grep asdf asdfasdf $ parent process fork1 child (left) fork1

Wait… stdin? stdout?

[email protected] 10

$$ ls | grep asdfasdfasdf$(standard input, standard output)

ProcessIN OUT

stdi

n

stdout

lsIN OUT

stdi

n

stdout

grepIN OUT

stdi

n

stdout

• stdin(0), stdout(1), and stderr(2) are file descriptors(i.e. just an integer in user-program)

• Each program has its own descriptor table

Page 12: CS143A Principles on Operating Systems Discussion 02: OS …aburtsev/143A/discussions/CS143... · 2020. 10. 17. · $ ls| grep asdf asdfasdf $ parent process fork1 child (left) fork1

Wait… stdin? stdout?

[email protected] 11

$$ ls | grep asdfasdfasdf$(standard input, standard output)

ProcessIN OUT

stdi

n

stdout

lsIN OUT

stdi

n

stdout

grepIN OUT

stdi

n

stdout

IN OUT

• stdin(0), stdout(1), and stderr(2) are file descriptors(i.e. just an integer in user-program)

• Each program has its own descriptor table• How to modify process’ file descriptors?

Page 13: CS143A Principles on Operating Systems Discussion 02: OS …aburtsev/143A/discussions/CS143... · 2020. 10. 17. · $ ls| grep asdf asdfasdf $ parent process fork1 child (left) fork1

Wait… stdin? stdout?

[email protected] 12

$$ ls | grep asdfasdfasdf$(standard input, standard output)

ProcessIN OUT

stdi

n

stdout

lsIN OUT

stdi

n

stdout

grepIN OUT

stdi

n

stdout

• stdin(0), stdout(1), and stderr(2) are file descriptors(just an integer in user-program)

• Each program has its own descriptor table• How to modify process’ file descriptors?

• close, dup(or open)

Page 14: CS143A Principles on Operating Systems Discussion 02: OS …aburtsev/143A/discussions/CS143... · 2020. 10. 17. · $ ls| grep asdf asdfasdf $ parent process fork1 child (left) fork1

Wait… stdin? stdout?

[email protected] 13

$$ ls | grep asdfasdfasdf$(standard input, standard output)

ProcessIN OUT

stdi

n

stdout

lsIN OUT

stdi

n

stdout

grepIN OUT

stdi

n

stdout

• stdin(0), stdout(1), and stderr(2) are file descriptors(just an integer in user-program)

• Each program has its own descriptor table• How to modify process’ file descriptors?

• close, dup(or open)• What we need to do:

close appropriate descriptors for each process and set the appropriate descriptor by copying

Page 15: CS143A Principles on Operating Systems Discussion 02: OS …aburtsev/143A/discussions/CS143... · 2020. 10. 17. · $ ls| grep asdf asdfasdf $ parent process fork1 child (left) fork1

Wait… stdin? stdout?

[email protected] 14

$$ ls | grep asdfasdfasdf$(standard input, standard output)

ProcessIN OUT

stdi

n

stdout

lsIN OUT

stdi

n

stdout

grepIN OUT

stdi

n

stdout

IN OUT

• stdin(0), stdout(1), and stderr(2) are file descriptors(just an integer in user-program)

• Each program has its own descriptor table• How to modify process’ file descriptors?

• close, dup(or open)• What we need to do:

close appropriate descriptors for each process and set the appropriate descriptor by copying

pipe() creates a pair of file descriptors, pointing to a pipe inode, and places them in the array pointed to by filedes. filedes[0] is for reading, filedes[1] is for writing. pipe is uni-directional

POSIX.1-2001

Page 16: CS143A Principles on Operating Systems Discussion 02: OS …aburtsev/143A/discussions/CS143... · 2020. 10. 17. · $ ls| grep asdf asdfasdf $ parent process fork1 child (left) fork1

pipe() and fork()

[email protected] 15

---------------------Point 0--------------------case PIPE:pcmd = (struct pipecmd*)cmd;if(pipe(p) < 0)

panic("pipe");---------------------Point A--------------------if(fork1() == 0){

close(1);dup(p[1]);close(p[0]);close(p[1]);

---------------------Point B--------------------runcmd(pcmd->left);

}if(fork1() == 0){

close(0);dup(p[0]);close(p[0]);close(p[1]);runcmd(pcmd->right);

}

close(p[0]);close(p[1]);---------------------Point C--------------------wait();wait();break;

$$ ls | grep asdfasdfasdf$

parent process

fork1 child(left)

fork1 child(right)

parent process

int pipe(int pipefd[2]);Create a pipe & assign each end to pipefd

pid_t fork(void);Copy the current process (parent)Returns the PID of the child (parent)or 0 (child)

Page 17: CS143A Principles on Operating Systems Discussion 02: OS …aburtsev/143A/discussions/CS143... · 2020. 10. 17. · $ ls| grep asdf asdfasdf $ parent process fork1 child (left) fork1

pipe() and fork()

[email protected] 16

---------------------Point 0--------------------case PIPE:pcmd = (struct pipecmd*)cmd;if(pipe(p) < 0)

panic("pipe");---------------------Point A--------------------if(fork1() == 0){

close(1);dup(p[1]);close(p[0]);close(p[1]);

---------------------Point B--------------------runcmd(pcmd->left);

}PA

REN

T PR

OCE

SS

0

1

2

stdin

stdout

stderr

p[0]

p[1]

※ Throughout the example, stderr is always connected to the screen. Omitted for simplicityas well as p[0] and p[1] to the parent process

Page 18: CS143A Principles on Operating Systems Discussion 02: OS …aburtsev/143A/discussions/CS143... · 2020. 10. 17. · $ ls| grep asdf asdfasdf $ parent process fork1 child (left) fork1

pipe() and fork()

[email protected] 17

---------------------Point 0--------------------case PIPE:pcmd = (struct pipecmd*)cmd;if(pipe(p) < 0)

panic("pipe");---------------------Point A-------------------if(fork1() == 0){

close(1);dup(p[1]);close(p[0]);close(p[1]);

---------------------Point B--------------------runcmd(pcmd->left);

}PA

REN

T PR

OCE

SS

0

1

2

3

4

stdin

stdout

stderr

p[0]

p[1]IN OUT

int p[2]

Page 19: CS143A Principles on Operating Systems Discussion 02: OS …aburtsev/143A/discussions/CS143... · 2020. 10. 17. · $ ls| grep asdf asdfasdf $ parent process fork1 child (left) fork1

pipe() and fork()

[email protected] 18

---------------------Point 0--------------------case PIPE:pcmd = (struct pipecmd*)cmd;if(pipe(p) < 0)

panic("pipe");---------------------Point A-------------------if(fork1() == 0){

close(1);dup(p[1]);close(p[0]);close(p[1]);

---------------------Point B--------------------runcmd(pcmd->left);

}

PARE

NT

PRO

CESS

0

1

2

3

4

stdin

stdout

stderr

p[0]

p[1]IN OUT

CHIL

D PR

OCE

SS

0

1

2

3

4

stdin

stdout

stderr

p[0]

p[1]

fork() copies the descriptors too!

Executed by child process

Page 20: CS143A Principles on Operating Systems Discussion 02: OS …aburtsev/143A/discussions/CS143... · 2020. 10. 17. · $ ls| grep asdf asdfasdf $ parent process fork1 child (left) fork1

pipe() and fork()

[email protected] 19

---------------------Point 0--------------------case PIPE:pcmd = (struct pipecmd*)cmd;if(pipe(p) < 0)

panic("pipe");---------------------Point A-------------------if(fork1() == 0){

close(1);dup(p[1]);close(p[0]);close(p[1]);

---------------------Point B--------------------runcmd(pcmd->left);

}

PARE

NT

PRO

CESS

0

1

2

3

4

stdin

stdout

stderr

p[0]

p[1]IN OUT

CHIL

D PR

OCE

SS

0

1

2

3

4

stdin

stdout

stderr

p[0]

p[1]

lsIN OUT

IN OUT

fork() copies the descriptors too!

Executed by child process

Page 21: CS143A Principles on Operating Systems Discussion 02: OS …aburtsev/143A/discussions/CS143... · 2020. 10. 17. · $ ls| grep asdf asdfasdf $ parent process fork1 child (left) fork1

pipe() and fork()

[email protected] 20

---------------------Point 0--------------------case PIPE:pcmd = (struct pipecmd*)cmd;if(pipe(p) < 0)

panic("pipe");---------------------Point A-------------------if(fork1() == 0){

close(1);dup(p[1]);close(p[0]);close(p[1]);

---------------------Point B--------------------runcmd(pcmd->left);

}

PARE

NT

PRO

CESS

0

1

2

3

4

stdin

stdout

stderr

p[0]

p[1]IN OUT

CHIL

D PR

OCE

SS

0

1

2

3

4

stdin

stdout

stderr

p[0]

p[1]

lsIN OUT

fork() copies the descriptors too!

Executed by child process

Page 22: CS143A Principles on Operating Systems Discussion 02: OS …aburtsev/143A/discussions/CS143... · 2020. 10. 17. · $ ls| grep asdf asdfasdf $ parent process fork1 child (left) fork1

pipe() and fork()

[email protected] 21

---------------------Point 0--------------------case PIPE:pcmd = (struct pipecmd*)cmd;if(pipe(p) < 0)

panic("pipe");---------------------Point A-------------------if(fork1() == 0){

close(1);dup(p[1]);close(p[0]);close(p[1]);

---------------------Point B--------------------runcmd(pcmd->left);

}

PARE

NT

PRO

CESS

0

1

2

3

4

stdin

stdout

stderr

p[0]

p[1]IN OUT

CHIL

D PR

OCE

SS

0

1

2

3

4

stdin

stdout

stderr

p[0]

p[1]

lsIN OUT

IN OUT

fork() copies the descriptors too!dup()’s destination is the lowest & unused file descriptor!

Executed by child process

Page 23: CS143A Principles on Operating Systems Discussion 02: OS …aburtsev/143A/discussions/CS143... · 2020. 10. 17. · $ ls| grep asdf asdfasdf $ parent process fork1 child (left) fork1

pipe() and fork()

[email protected] 22

---------------------Point 0--------------------case PIPE:pcmd = (struct pipecmd*)cmd;if(pipe(p) < 0)

panic("pipe");---------------------Point A-------------------if(fork1() == 0){

close(1);dup(p[1]);close(p[0]);close(p[1]);

---------------------Point B--------------------runcmd(pcmd->left);

}

PARE

NT

PRO

CESS

0

1

2

3

4

stdin

stdout

stderr

p[0]

p[1]IN OUT

CHIL

D PR

OCE

SS

0

1

2

3

4

stdin

stdout

stderr

p[0]

p[1]

lsIN OUT

IN OUT

fork() copies the descriptors too!dup()’s destination is the lowest & unused file descriptor!

Executed by child process

Page 24: CS143A Principles on Operating Systems Discussion 02: OS …aburtsev/143A/discussions/CS143... · 2020. 10. 17. · $ ls| grep asdf asdfasdf $ parent process fork1 child (left) fork1

pipe() and fork()

[email protected] 23

---------------------Point 0--------------------case PIPE:pcmd = (struct pipecmd*)cmd;if(pipe(p) < 0)

panic("pipe");---------------------Point A-------------------if(fork1() == 0){

close(1);dup(p[1]);close(p[0]);close(p[1]);

---------------------Point B--------------------runcmd(pcmd->left);

}

PARE

NT

PRO

CESS

0

1

2

3

4

stdin

stdout

stderr

p[0]

p[1]IN OUT

CHIL

D PR

OCE

SS

0

1

2

3

4

stdin

stdout

stderr

p[0]

p[1]

lsIN OUT

IN OUT

fork() copies the descriptors too!dup()’s destination is the lowest & unused file descriptor!

Executed by child process

Page 25: CS143A Principles on Operating Systems Discussion 02: OS …aburtsev/143A/discussions/CS143... · 2020. 10. 17. · $ ls| grep asdf asdfasdf $ parent process fork1 child (left) fork1

pipe() and fork()

[email protected] 24

---------------------Point B--------------------runcmd(pcmd>left);

}if(fork1() == 0){

close(0);dup(p[0]);close(p[0]);close(p[1]);runcmd(pcmd->right);

}close(p[0]);close(p[1]);---------------------Point C--------------------wait();wait();break;

PARE

NT

PRO

CESS

0

1

2

3

4

stdin

stdout

stderr

p[0]

p[1]IN OUT

CHIL

D PR

OCE

SS

0

1

2

3

4

stdin

stdout

stderr

p[0]

p[1]

fork() copies the descriptors too!dup()’s destination is the lowest & unused file descriptor!

Executed by child process

Page 26: CS143A Principles on Operating Systems Discussion 02: OS …aburtsev/143A/discussions/CS143... · 2020. 10. 17. · $ ls| grep asdf asdfasdf $ parent process fork1 child (left) fork1

CHIL

D PR

OCE

SS

0

1

2

3

4

stdin

stdout

stderr

p[0]

p[1]

pipe() and fork()

[email protected] 25

---------------------Point B--------------------runcmd(pcmd>left);

}if(fork1() == 0){

close(0);dup(p[0]);close(p[0]);close(p[1]);runcmd(pcmd->right);

}close(p[0]);close(p[1]);---------------------Point C--------------------wait();wait();break;

PARE

NT

PRO

CESS

0

1

2

3

4

stdin

stdout

stderr

p[0]

p[1]IN OUT

CHIL

D PR

OCE

SS

0

1

2

3

4

stdin

stdout

stderr

p[0]

p[1]

fork() copies the descriptors too!dup()’s destination is the lowest & unused file descriptor!

grepIN OUT

stdi

nstdout

IN OUT

Executed by child process

Page 27: CS143A Principles on Operating Systems Discussion 02: OS …aburtsev/143A/discussions/CS143... · 2020. 10. 17. · $ ls| grep asdf asdfasdf $ parent process fork1 child (left) fork1

CHIL

D PR

OCE

SS

0

1

2

3

4

stdin

stdout

stderr

p[0]

p[1]

pipe() and fork()

[email protected] 26

---------------------Point B--------------------runcmd(pcmd>left);

}if(fork1() == 0){

close(0);dup(p[0]);close(p[0]);close(p[1]);runcmd(pcmd->right);

}close(p[0]);close(p[1]);---------------------Point C--------------------wait();wait();break;

PARE

NT

PRO

CESS

0

1

2

3

4

stdin

stdout

stderr

p[0]

p[1]IN OUT

CHIL

D PR

OCE

SS

0

1

2

3

4

stdin

stdout

stderr

p[0]

p[1]

fork() copies the descriptors too!dup()’s destination is the lowest & unused file descriptor!

grepIN OUT

stdi

nstdout

IN OUT

Executed by child process

Page 28: CS143A Principles on Operating Systems Discussion 02: OS …aburtsev/143A/discussions/CS143... · 2020. 10. 17. · $ ls| grep asdf asdfasdf $ parent process fork1 child (left) fork1

CHIL

D PR

OCE

SS

0

1

2

3

4

stdin

stdout

stderr

p[0]

p[1]

pipe() and fork()

[email protected] 27

---------------------Point B--------------------runcmd(pcmd>left);

}if(fork1() == 0){

close(0);dup(p[0]);close(p[0]);close(p[1]);runcmd(pcmd->right);

}close(p[0]);close(p[1]);---------------------Point C--------------------wait();wait();break;

PARE

NT

PRO

CESS

0

1

2

3

4

stdin

stdout

stderr

p[0]

p[1]IN OUT

CHIL

D PR

OCE

SS

0

1

2

3

4

stdin

stdout

stderr

p[0]

p[1]

fork() copies the descriptors too!dup()’s destination is the lowest & unused file descriptor!

grepIN OUT

stdi

nstdout

IN OUT

Executed by child process

Page 29: CS143A Principles on Operating Systems Discussion 02: OS …aburtsev/143A/discussions/CS143... · 2020. 10. 17. · $ ls| grep asdf asdfasdf $ parent process fork1 child (left) fork1

CHIL

D PR

OCE

SS

0

1

2

3

4

stdin

stdout

stderr

p[0]

p[1]

pipe() and fork()

[email protected] 28

---------------------Point B--------------------runcmd(pcmd>left);

}if(fork1() == 0){

close(0);dup(p[0]);close(p[0]);close(p[1]);runcmd(pcmd->right);

}close(p[0]);close(p[1]);---------------------Point C--------------------wait();wait();break;

PARE

NT

PRO

CESS

0

1

2

3

4

stdin

stdout

stderr

p[0]

p[1]IN OUT

CHIL

D PR

OCE

SS

0

1

2

3

4

stdin

stdout

stderr

p[0]

p[1]

fork() copies the descriptors too!dup()’s destination is the lowest & unused file descriptor!

grepIN OUT

stdi

nstdout

IN OUT

Executed by child process

Page 30: CS143A Principles on Operating Systems Discussion 02: OS …aburtsev/143A/discussions/CS143... · 2020. 10. 17. · $ ls| grep asdf asdfasdf $ parent process fork1 child (left) fork1

CHIL

D PR

OCE

SS

0

1

2

3

4

stdin

stdout

stderr

p[0]

p[1]

pipe() and fork()

[email protected] 29

---------------------Point B--------------------runcmd(pcmd>left);

}if(fork1() == 0){

close(0);dup(p[0]);close(p[0]);close(p[1]);runcmd(pcmd->right);

}close(p[0]);close(p[1]);---------------------Point C--------------------wait();wait();break;

PARE

NT

PRO

CESS

0

1

2

3

4

stdin

stdout

stderr

p[0]

p[1]IN OUT

CHIL

D PR

OCE

SS

0

1

2

3

4

stdin

stdout

stderr

p[0]

p[1]

fork() copies the descriptors too!dup()’s destination is the lowest & unused file descriptor!

grepIN OUT

stdi

nstdout

IN OUT

Executed by child process

Page 31: CS143A Principles on Operating Systems Discussion 02: OS …aburtsev/143A/discussions/CS143... · 2020. 10. 17. · $ ls| grep asdf asdfasdf $ parent process fork1 child (left) fork1

CHIL

D PR

OCE

SS

0

1

2

3

4

stdin

stdout

stderr

p[0]

p[1]

pipe() and fork()

[email protected] 30

---------------------Point B--------------------runcmd(pcmd>left);

}if(fork1() == 0){

close(0);dup(p[0]);close(p[0]);close(p[1]);runcmd(pcmd->right);

}close(p[0]);close(p[1]);---------------------Point C--------------------wait();wait();break;

PARE

NT

PRO

CESS

0

1

2

3

4

stdin

stdout

stderr

p[0]

p[1]IN OUT

CHIL

D PR

OCE

SS

0

1

2

3

4

stdin

stdout

stderr

p[0]

p[1]

fork() copies the descriptors too!dup()’s destination is the lowest & unused file descriptor!

Parent waits child processes

Page 32: CS143A Principles on Operating Systems Discussion 02: OS …aburtsev/143A/discussions/CS143... · 2020. 10. 17. · $ ls| grep asdf asdfasdf $ parent process fork1 child (left) fork1

pipe() and fork()and exec()

[email protected] 31

PARE

NT

PRO

CESS

0

1

2

3

4

stdin

stdout

stderr

p[0]

p[1]IN OUT

CHIL

D PR

OCE

SS

0

1

2

3

4

stdin

stdout

stderr

p[0]

p[1]

$$ ls | grep asdfasdfasdf$

int execvp(const char *file, char *const argv[]);replaces the current process image with a new process image.

runcmd() contains exec functions

if(fork1() == 0){…runcmd(pcmd->right);

}

Page 33: CS143A Principles on Operating Systems Discussion 02: OS …aburtsev/143A/discussions/CS143... · 2020. 10. 17. · $ ls| grep asdf asdfasdf $ parent process fork1 child (left) fork1

CHIL

D PR

OCE

SS

0

1

2

3

4

stdin

stdout

stderr

p[0]

p[1]

pipe() and fork()and exec()

[email protected] 32

PARE

NT

PRO

CESS

0

1

2

3

4

stdin

stdout

stderr

p[0]

p[1]IN OUT

LS

0

1

2

3

4

stdin

stdout

stderr

p[0]

p[1]

$$ ls | grep asdfasdfasdf$

int execvp(const char *file, char *const argv[]);replaces the current process image with a new process image.

runcmd() contains exec functions

if(fork1() == 0){…runcmd(pcmd->right);

}

Page 34: CS143A Principles on Operating Systems Discussion 02: OS …aburtsev/143A/discussions/CS143... · 2020. 10. 17. · $ ls| grep asdf asdfasdf $ parent process fork1 child (left) fork1

0

1

2

3

4

stdin

stdout

stderr

p[0]

p[1]

pipe() and fork()and exec()

[email protected] 33

PARE

NT

PRO

CESS

0

1

2

3

4

stdin

stdout

stderr

p[0]

p[1]IN OUT

LS

0

1

2

3

4

stdin

stdout

stderr

p[0]

p[1]

$$ ls | grep asdfasdfasdf$

GREPint execvp(const char *file, char *const argv[]);

replaces the current process image with a new process image.

runcmd() contains exec functions

if(fork1() == 0){…runcmd(pcmd->right);

}

Page 35: CS143A Principles on Operating Systems Discussion 02: OS …aburtsev/143A/discussions/CS143... · 2020. 10. 17. · $ ls| grep asdf asdfasdf $ parent process fork1 child (left) fork1

GREP

0

1

2

3

4

stdin

stdout

stderr

p[0]

p[1]

pipe() and fork()and exec()

[email protected] 34

PARE

NT

PRO

CESS

0

1

2

3

4

stdin

stdout

stderr

p[0]

p[1]IN OUT

LS

0

1

2

3

4

stdin

stdout

stderr

p[0]

p[1]

$$ ls | grep asdfasdfasdf$

int execvp(const char *file, char *const argv[]);replaces the current process image with a new process image.

runcmd() contains exec functions

if(fork1() == 0){…runcmd(pcmd->left);

}