I am learning linux programming and came across exec function which is kind of very useful. But the problem is exec function arguments are very confusing and I am unable to grasp which argument is for what purpose.. In the following code execl() function is called from a child created through fork(), What is the purpose of the last argument (NULL) in execl()?
execl("/bin/ls","ls","-l",NULL);
If any one can explain what is the purpose of NULL argument and other arguments and the purpose of arguments of exec() family function, It would be a great help to me!
To create undefined behavior. That is not a legal call to
execl. Acorrect call might be:
The last argument must be
(char*)0, or you have undefined behavior.The first argument is the path of the executable. The following
arguments appear in
argvof the executed program. The list of thesearguments is terminated by a
(char*)0; that’s how the called functionknows that the last argument has been reached. In the above example,
for example, the executable at
"/bin/ls"will replace your code; inits
main, it will haveargcequal 2, withargv[0]equal"ls",and
argv[1]equal"-l".Immediately after this function, you should have the error handling
code. (
execlalways returns -1, when it returns, so you don’t need totest it. And it only returns if there was some sort of error.)