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

}<br />

while (1){<br />

printf("pid1 is alive\n");<br />

sleep(1);<br />

}<br />

}<br />

if((pid2=fork())== 0){ /* second child */<br />

while (1){<br />

printf("pid2 is alive\n");<br />

sleep(1) ;<br />

}<br />

}<br />

sleep(3);<br />

kill(pid1, SIGSTOP) ; /* suspend first child */<br />

sleep(3);<br />

kill(pid1, SIGCONT) ; /* resume first child */<br />

sleep(3);<br />

kill(pid1, SIGINT) ; /* kill first child */<br />

kill(pid2, SIGINT) ; /* kill second child */<br />

pidl is alive<br />

pid2 is alive<br />

pidl is alive<br />

pid2 is alive<br />

pidl is alive<br />

pid2 is alive<br />

pid2 is alive<br />

pid2 is alive<br />

pid2 is alive<br />

pidl is alive<br />

pid2 is alive<br />

pidl is alive<br />

pid2 is alive<br />

pidl is alive<br />

pid2 is alive<br />

... just second child runs<br />

... first child is resumed<br />

Process Groups<br />

Every process is a member of a process group. Several processes can be members of <strong>the</strong> same process group.<br />

When a process forks, <strong>the</strong> child inherits its process group from its parent. A process may change its process group<br />

to a new value by using setpgrp().<br />

Every process can have an associated control terminal. A child process inherits its control terminal from its parent.<br />

When a process execs, its control terminal stays <strong>the</strong> same.<br />

Every terminal can be associated <strong>with</strong> a single control process. When ^C is detected, <strong>the</strong> terminal sends <strong>the</strong><br />

appropriate signal to all processes in <strong>the</strong> process group of its control process.<br />

/* proc_groupl.c */<br />

#include <br />

#include <br />

void sigintHandler();<br />

main ()<br />

{<br />

signal (SIGINT, sigintHandler); /* handle ^C */<br />

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

printf("child PID %d PGRP %d waits\n", getpid(), getpgrp(0));<br />

else<br />

printf("parent PID %d PGRP %d waits\n", getpid(), getpgrp(0));<br />

126

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

Saved successfully!

Ooh no, something went wrong!