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

int readline(int fd, char *str)<br />

{<br />

int nread;<br />

while (((nread=read(fd, str, 1)) > 0) && (*str++ != NULL))<br />

return (nread > 0); /* false if end of file */<br />

}<br />

main(int argc,char *argv)<br />

{<br />

int fd;<br />

char str[100];<br />

mkfifo (" PIPE");<br />

if ((fd=open("PIPE", O_RDONLY)) == -1){<br />

fprintf(stderr, "%s: can't open PIPE\n", argv[0]);<br />

exit(1) ;<br />

}<br />

while (readline(fd, str))<br />

printf("%s\n", str);<br />

close(fd);<br />

unlink("PIPE") ;<br />

}<br />

/* writer.c */<br />

#include <br />

#include <br />

main()<br />

{<br />

int fd,i;<br />

char str[100];<br />

mkfifo("PIPE"); /* if it does not already exist */<br />

while ((fd=open ("PIPE", O_WRONLY) < 0))<br />

sleep(1); /* wait for reader */<br />

}<br />

sprintf(str, "hello from PID %d", getpid());<br />

for (i=0; i

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

Saved successfully!

Ooh no, something went wrong!