16.05.2015 Views

Working with the Unix OS

Working with the Unix OS

Working with the Unix OS

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Processes I<br />

dup (and o<strong>the</strong>r similar calls) copy a file descriptor to<br />

- <strong>the</strong> designated fd entry<br />

- <strong>the</strong> lowest free fd entry<br />

dup2 does <strong>the</strong> former;<br />

dup2 - used for redirecting I/O (stdin, stdout) of a process to/from:<br />

- a file (implements '')<br />

- a pipe (1mplements '|')<br />

NOTE: stdin defined as 0, so identifying fd1 of open file table.<br />

int pfd [2];<br />

pipe (pfd);<br />

if (fork()!= 0) { /* parent */<br />

close (pfd [0]); /* close <strong>the</strong> reading end */<br />

if (fork()!= 0) { /* parent still */<br />

close(pfd[l]); /* parent closes <strong>the</strong> writing end */<br />

} else { /* 2nd child */<br />

close(l); /* close stdout */<br />

dup2(pfd[l],l); /* copy <strong>the</strong> writing end over stdout */<br />

close(pfd[l]); /* close <strong>the</strong> original writing end */<br />

exec(foo); /* execute <strong>the</strong> utility writing to stdout */<br />

}<br />

} else { /* first child */<br />

close(pfd[l]); /* close <strong>the</strong> writing end */<br />

close(0); /* close stdin */<br />

dup2(pfd[0], 0); /* copy reading end over stdin */<br />

close(pfd[0]); /* close <strong>the</strong> original reading end */<br />

exec (utility); /* utility will read from stdin */<br />

}<br />

Bi-directional pipes ?<br />

two results:<br />

- short circuit (Pl will read back from pfd[0] its own data just written to pfd[l])<br />

- possibility of deadlock or looping (both processes):<br />

while not eof pfd[0] {<br />

read pfd[0];<br />

process data;<br />

}<br />

close pfd[l];<br />

Solution: Use 2 pipes, treat <strong>the</strong>m as simplex channels.<br />

File Subsystem<br />

- Manages files<br />

- Allocates file space<br />

- Administers free space<br />

- Controls access to files<br />

- Retrieves data for users<br />

Processes interact <strong>with</strong> F.S. by a set of system calls<br />

- open<br />

- close<br />

- read<br />

- write<br />

- stat (Query attributes of a file)<br />

- chrnod (change access permissions)<br />

Process control subsystem<br />

- Process synchronization<br />

- Inter-process communication<br />

- Memory management<br />

- Process scheduling<br />

P.C.S system calls<br />

- fork<br />

- exec<br />

- exit<br />

- wait<br />

- brk<br />

- signal<br />

Memory Management Module<br />

- Controls allocation of memory<br />

98

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!