PipeLine

Published on December 2016 | Categories: Documents | Downloads: 52 | Comments: 0 | Views: 379
of 1
Download PDF   Embed   Report

PipeLine Ngôn Ngữ Lập Trình

Comments

Content

#include <stdio.h>
#include <unistd.h>
#include <errno.h>
/*
* hien thi msg & giao tiep giua cha-con
*/
void error(char *msg) {
perror(msg);
exit(-1);
}
void main() {
int p;
int tube[2];
char c;
int rep;
setbuf(stdout,NULL); /* std output khong dung vung dem Les entrées */
if (pipe(tube)==-1) /* tao pipe */
error("pipe");
p=fork(); /* tao con */
if (p==-1)
error("fork");
if (p==0) { /* QT con */
close(tube[1]);
while (read(tube[0], &c, sizeof(char))) { /* Doc tung char tu pipe.*/
if ((c<='z') && (c>='a')) {
printf("Con: <%c>\n",c);
}
}
close(tube[0]);
}
else { /* cha */
close(tube[0]);
printf("PID con: %d\n",p);
while ((rep=getchar())!=EOF) { /* Doc ky ty tu stdinput & goi qua
pipe. */
c=(char) rep;
write(tube[1],&c,sizeof(char));
}
close(tube[1]);
}
}

Sponsor Documents

Or use your account on DocShare.tips

Hide

Forgot your password?

Or register your new account on DocShare.tips

Hide

Lost your password? Please enter your email address. You will receive a link to create a new password.

Back to log-in

Close