c - Why does using `execl` instead of `system` stops my program from working? -
i'm trying basic ipc using pipes. spent hours searching internet, doing , that, reading api documentations, , ended code below. not work, quite expected. making code 'work' many thanks.
<edit>
i've found using system
instead of execl
makes program run expected. going wrong here when use execl
, while doesn't happen system
function?
</edit>
#include <stdio.h> #include <stdlib.h> #include <unistd.h> int main(void){ int hinpipe[2]; int houtpipe[2]; file *hinfile; file *houtfile; char *s; pipe(hinpipe); pipe(houtpipe); if(fork()){ close(hinpipe[0]); close(houtpipe[1]); hinfile=fdopen(hinpipe[1],"w"); fprintf(hinfile,"2^100\n"); fclose(hinfile); houtfile=fdopen(houtpipe[0],"r"); fscanf(houtfile,"%ms",&s); fclose(houtfile); printf("%s\n",s); free(s); }else{ dup2(hinpipe[0],stdin_fileno); dup2(houtpipe[1],stdout_fileno); close(hinpipe[0]); close(hinpipe[1]); close(houtpipe[0]); close(houtpipe[1]); system("bc -q");/*this works*/ /*execl("bc","-q",null);*/ /*but doesn't*/ } }
read fine man page. :)
execl(const char *path, const char *arg0, ... /*, (char *)0 */);
arg0
(aka argv[0], name program told invoked under) not same argument path (the location of executable said program). moreover, execl
takes, first argument, fully-qualified pathname.
thus, want:
execl("/usr/bin/bc", "bc", "-q", null);
...or, search path bc
rather hardcoding location:
execlp("bc", "bc", "-q", null);
Comments
Post a Comment