Systémové programovanie domáca č. 2 - riesenie

Created: 2010-03-14 - 18:05

time ./a.out

real 0m4.106s
user 0m0.542s
sys 0m1.639s


#include <stdio.h>
#include <stdlib.h>
#include <unistd.h> /**
Funkcia pre zapisovanie a citanie rodicom - posiela data a potom caka na potvrdenie
*/
void parents(const char* message, FILE* stream, FILE* sstream) { int c = 1000; while(--c>0){ char buffer[1024]; fprintf(stream,"%s\n",message); fflush(stream); fgets(buffer,1024,sstream); //fputs(buffer,stdout); //nechceme spomalit vypisom
} } /**
Funkcia pre zapisovanie a citanie dietatom - prijma data a posiela potvrdenie
*/
void childs(const char* message, FILE* stream, FILE* sstream) { int c = 1000; while(--c>0){ char buffer[1024]; fgets(buffer,1024,sstream); //fputs(buffer,stdout); //nechceme spomalit vypisom
fprintf(stream,"%s\n",message); fflush(stream); } } int main() { pid_t pid; int fds[2]; //dve polia na file descriptory
int fdss[2]; pipe(fds); //dve pipe-i
pipe(fdss); pid = fork(); //pid procesu
if (pid == (pid_t) 0) { /* child */ FILE* stream; FILE* sstream; stream = fdopen(fds[0],"r"); //otvorenie jednotlivych streamov na citanie a zapis
sstream = fdopen(fdss[1],"w"); childs("Dik za data.",sstream,stream); close(fds[1]); //uzavretie fds
close(fdss[0]); close(fds[0]); close(fdss[1]); waitpid (pid, NULL, 0); //ukoncenie dietata
} else { /* parent */ FILE* stream; FILE* sstream; stream = fdopen(fds[1],"w"); //otvorenie jednotlivych streamov na zapis a citanie
sstream = fdopen(fdss[0],"r"); parents("Posielam data.",stream,sstream); close(fds[0]); //uzavretie fds
close(fdss[1]); close(fds[1]); close(fdss[0]); waitpid (pid, NULL, 0); //ukoncenie dietata
} return 0; }