home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
ftp.disi.unige.it
/
2015-02-11.ftp.disi.unige.it.tar
/
ftp.disi.unige.it
/
pub
/
.person
/
GianuzziV
/
SO1
/
fork3.c
< prev
next >
Wrap
C/C++ Source or Header
|
2001-11-07
|
1KB
|
60 lines
/* fork3.c */
#include <string.h>
#include <fcntl.h>
char stringa[20];
int i;
/* Due processi comunicano attraverso due pipe, uno
da padre a figlio, l'altro da figlio a padre.
------to_chil--------
| |
| V
padre figlio
^ |
| |
------from_chil-----
*/
main (argc, argv)
int argc;
char *argv[];
{ int cont, i, status, c = 0;
int to_chil[2], from_chil[2]; /* pipe dal padre e pipe dal figlio */
char buf[256];
pipe (to_chil);
pipe (from_chil);
if ( fork() == 0 )
{ /* processo figlio */
close(to_chil[1]);
close(from_chil[0]);
for (;;)
{ for (i=0; i<256; i++) buf[i]='\0';
if ((cont = read(to_chil[0], buf, sizeof(buf))) == 0)
{printf("fine pipe, letti %d stringhe \n",c);
exit(3); }
printf("letto da pipe %s \n", buf);
c++;
write (from_chil[1], buf, cont);
}
}
/* processo padre */
close(from_chil[1]);
close(to_chil[0]);
for ( i=0; i<15; i++)
{ printf("scrivere stringa da inviare ");
scanf("%s",stringa);
cont = write (to_chil[1], stringa, strlen(stringa));
c++;
cont = read(from_chil[0], buf, sizeof(buf));
}
printf("scritte %d stringhe. \n",c);
close(to_chil[1]);
close(from_chil[0]);
wait(&status);
printf("%x \n",status);
}