SHPORA.net :: PDA | |
Main FAQ гуманитарные науки естественные науки математические науки технические науки ,PIPE1 #include <sys/types.h>
#include <sys/stat.h> #include <sys/wait.h> #include <fcntl.h> #include <unistd.h> ^int main(int argc, char *argv[]) { ^int fd; ^int p[2]; ^char buf[16]; ^int k,i,stat; ^if (argc !=2){ ^printf("Syntax error! "); ^exit(-1); } ^pipe(p); ^creat("lock", 0000); ^if(fork() == 0) { /* proces consumator */ ^close(p[1]); ^fcntl(p[0], O_NDELAY); ^while(1) { ^k=read(p[0],buf,16); ^if ((k==0) [and][and] (open("lock", O_RDWR) != -1)) break; ^for(i=0;i<k;i++) printf("[procent]02X ",buf[i]); ^printf(" "); } ^unlink("lock"); ^printf("exit consumator... "); ^exit(0); } /* proces producator */ ^close(p[0]); ^fd=open(argv[1],O_RDONLY); ^while((k=read(fd,buf,16)) > 0) { ^write(p[1],buf,k); } ^chmod("lock",0777); ^printf("exit producator... "); ^exit(0); } |