11.07.2014 Views

UNIX: testo della esercitazione 5 in laboratorio su primitive per la ...

UNIX: testo della esercitazione 5 in laboratorio su primitive per la ...

UNIX: testo della esercitazione 5 in laboratorio su primitive per la ...

SHOW MORE
SHOW LESS

Create successful ePaper yourself

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

4 Esercizi <strong>su</strong>lle pipe<br />

Esercizio 7:<br />

Esempio di comunicazione tra padre e figlio attraverso una pipe.<br />

#<strong>in</strong>clude <br />

#<strong>in</strong>clude <br />

#<strong>in</strong>clude <br />

#def<strong>in</strong>e MSGSIZE 256<br />

<strong>in</strong>t ma<strong>in</strong>()<br />

{<br />

<strong>in</strong>t pid, j, c;<br />

<strong>in</strong>t piped[2];<br />

char <strong>in</strong>pbuf[MSGSIZE];<br />

}<br />

/*<br />

Apre <strong>la</strong> pipe creando due file descriptor,<br />

uno <strong>per</strong> <strong>la</strong> lettura e l’altro <strong>per</strong> <strong>la</strong> scrittura<br />

(vengono memorizzati nell’array piped[])<br />

*/<br />

if (pipe(piped) < 0)<br />

exit(1);<br />

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

exit(2);<br />

else if (pid == 0) /* figlio, che ha una copia di piped[] */<br />

{<br />

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

for (j = 1; j

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

Saved successfully!

Ooh no, something went wrong!