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

Although <strong>the</strong> processes appear to copy <strong>the</strong> source file twice as fast because <strong>the</strong>y share <strong>the</strong> work load, <strong>the</strong> contents of<br />

<strong>the</strong> target file depends on <strong>the</strong> order that <strong>the</strong> kernel scheduled <strong>the</strong> processes.<br />

#include <br />

char string[0] = "hello world";<br />

main ()<br />

{<br />

int count, i;<br />

int to_par[2], to_chil[2]; /* for pipes to parent, child */<br />

char buf[256];<br />

pipe(to_par);<br />

pipe(to_chil);<br />

if (fork() == 0)<br />

{<br />

/* child process executes here * /<br />

close(O); /* close old standard input */<br />

dup(to_chil[O]); /* dup pipe read to standard input */<br />

close(1); /* close old standard output */<br />

dup(to_par[l]); /* dup pipe write to standard out */<br />

close(to_par[1]); /* close unnecessary pipe descriptors */<br />

close(to_chil[O]);<br />

close(to_par[O]);<br />

close(to_chil[1]);<br />

for (;;)<br />

{<br />

if((count == read (0, buf, sizeof(buf))) == 0)<br />

exit();<br />

write(O, buf, count);<br />

}<br />

}<br />

/* parent process executes here * /<br />

close(1); /* rearrange standard in, out */<br />

dup(to_chi1[1]);<br />

close(O);<br />

dup(to_par[O]);<br />

close(to_chil[1]);<br />

close(to_par[O]);<br />

close (to_chil[0]);<br />

close(to_par[1]);<br />

for (i = 0; i < 15; i++)<br />

{<br />

write(l, string, strlen(string));<br />

read(O, buf, sizeof(buf));<br />

}<br />

}<br />

Figure 23. Use of Pipe, Dup and Fork<br />

The processes thus exchange messages over two pipes.<br />

Signals<br />

Signals inform processes of <strong>the</strong> occurrence of asynchronous events. Processes may send each o<strong>the</strong>r signals <strong>with</strong> <strong>the</strong><br />

"kill" system call.<br />

Use of Signals:<br />

! termination of a process<br />

- "exit", "signal" death of child<br />

! process induced exceptions<br />

- access memory outside address space<br />

! unrecoverable conditions<br />

- running out of system resources<br />

! unexpected error condition<br />

- making non existent system call – writing a pipe that has no reader – illegal reference to "1seek"<br />

! originating from process in user mode<br />

106

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

Saved successfully!

Ooh no, something went wrong!