linux - how to use exec() without any parameter? -
i wanna run "ls" command exec(), , code exec("/bin/ls", null) got text shows "a null argv[0] passed through exec system call." if add "all" parameter woks. exec("/bin/ls","all",null)
however, when use exec("/bin/ps", null), works properly. me figure out whats wrong program?
btw: use execl()
#include <iostream> #include <unistd.h> //required fork() #include <sys/types.h> //required wait() #include <sys/wait.h> //required wait() using namespace std; int main(){ string cmd=""; string cmdpath="/bin/"; cout<<endl<<getcwd(null,0)<<" >> "; cin>>cmd; cout<<endl; string cmdcmdpath = cmdpath+cmd; const char* charcmd = cmdcmdpath.c_str(); int x = fork(); if(x!=0){ cout<<"the command "<<cmd<<" running"<<endl; wait(null); cout<<"im parent!"<<endl; }else if (x==0){ cout<<"im child!"<<endl; execl(charcmd,null); cout<<"child done"<<endl; } }
carefully read desctiption of execl:
the first argument, convention, should point filename associated file being executed.
it means, second execl
parameter should path, referred same file first one. usually, first , second parameters same:
execl(charcmd, charcmd, null);
Comments
Post a Comment