Tuesday 15 March 2011

c multiple pipes pipes and problems with file descriptor -



c multiple pipes pipes and problems with file descriptor -

hello i'm bit lost on utilize of pipes

i have develop shell programme have flex programme working , expressions feed function

edit

as pointed dave did declare pipe within function still have same problem here update on function have beingness stuck on while looks im not closing propely pipes because sec fork hanging on waitpid()

but did seek close forks on main process , childs sec fork still hanging

int execute(expression *e , int wait, int fdin,int fdout,int fderror){ int status; pid_t childpid; int fd; int pp[2]; switch (e->type) { case simple: childpid = fork(); if(childpid >= 0) //fork successful { if(childpid == 0) //child process { if(fdin != 0){ dup2(fdin,0); close(fdin); if(fdin > 2){ close(fdin +1); } } if(fdout != 0){ dup2(fdout,1); close(fdout); if(fdout > 3){ close(fdout -1); } } if(fderror != 2){ dup2(fderror,2); close(fderror); } status = execvp(e->arguments[0], &e->arguments[0]); perror(e->arguments[0]); exit(1); } else//parent process { if(fdin > 2){ close(fdin); close(fdin +1); } if(fdout > 3){ close(fdout); close(fdout -1); } if(wait == 1){ printf("%s\n","going wait" ); waitpid(childpid, &status, 0); } putchar('\n'); break; } } else// fork failed { perror("fork"); } break; case sequence: execute(e->gauche,1,fdin,fdout,fderror); execute(e->droite,1,fdin,fdout,fderror); break; case sequence_et: execute(e->gauche,0,fdin,fdout,fderror); execute(e->droite,1,fdin,fdout,fderror); break; case sequence_ou: execute(e->gauche,0,fdin,fdout,fderror); execute(e->droite,1,fdin,fdout,fderror); break; case bg: execute(e->gauche,0,fdin,fdout,fderror); break; case pipe: if(pipe(pp) < 0){ perror("pipe"); exit(1); } execute(e->gauche,0,fdin,pp[1],fderror); execute(e->droite,1,pp[0],fdout,fderror); break; case redirection_i: fd = open(e->arguments[0],o_rdonly, 0666); execute(e->gauche,1,fd,fdout,fderror); break; case redirection_o: fd = open(e->arguments[0],o_creat | o_rdwr, 0666); execute(e->gauche,1,fdin,fd,fderror); break; case redirection_a: fd = open(e->arguments[0], o_trunc | o_creat | o_rdwr, 0666); execute(e->gauche,1,fdin,fd,fderror); break; case redirection_e: fd = open(e->arguments[0], o_creat | o_rdwr, 0666); execute(e->gauche,1,fdin,fdout,fd); break; case redirection_eo: fd = open(e->arguments[0], o_creat | o_rdwr, 0666); execute(e->gauche,1,fdin,fd,fd); break; default: homecoming 0; break; } homecoming 0; }

my problem working simple comands

ls | grep

os

ls > test

but if mess goes finish wrong

exemple

ls | grep > test

i out set on terminal , not in file

or if utilize

ls | grep | grep c

the pipes goes mess gues need more pipes have no thought how create pipes on fly

thanks

edit

just figured out after prepare of pipe did seek commands bigger info exemple instead of ls did cat on file worked looks command stops before info gets pipe

you using global variable pipes. when create have ls | grep a global pipe works fine. when have multiple pipes reuse them different purposes! solution pull pipe creation code in function. when need new pipe create 1 there , pass right file descriptors in recursive phone call execute (that's why need fdin , fdout parameters.

c pipe stdout stdin

No comments:

Post a Comment