16.05.2015 Views

Working with the Unix OS

Working with the Unix OS

Working with the Unix OS

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Processes I<br />

- <strong>the</strong> kernel assigns <strong>the</strong> parent process ID field in <strong>the</strong> child slot, putting <strong>the</strong> child in <strong>the</strong> process tree structure,<br />

initialises scheduling parameters such as priority, CPU usage, timing.<br />

- <strong>the</strong> kernel increments reference counts for files. Both processes manipulate <strong>the</strong> same file table entries, <strong>the</strong><br />

effect of "fork" is similar to that of dup.<br />

- <strong>the</strong> kernel allocates memory for <strong>the</strong><br />

child process u area, regions and page<br />

tables.<br />

- <strong>the</strong> kernel create a context layer for <strong>the</strong><br />

child containing registers and sets <strong>the</strong><br />

program counter. The child state is set<br />

to "ready-to-run".<br />

Figure 21. Fork Creating New Process Context<br />

#include <br />

int fdrd, fdwt;<br />

char c;<br />

main(argc, argv)<br />

int argc;<br />

char *argv[];<br />

{<br />

if (argc != 3)<br />

exit(1);<br />

if((fdrd = open(argv[1], O_RDONLY)) == -1)<br />

exit(1);<br />

if((fdwt = creat(argv[2], 0666)) == -1)<br />

exit(1);<br />

fork();<br />

/* both procs execute same code */<br />

rdwrt();<br />

exit(0);<br />

}<br />

rdwrt()<br />

{<br />

for (;;)<br />

{<br />

if (read (fdrd, &c, 1) != 1)<br />

return;<br />

write(fdwt, &c, 1);<br />

}<br />

}<br />

Figure 22. Example of Parent and Child Share File A<br />

105

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

Saved successfully!

Ooh no, something went wrong!