execl() arguments in Ubuntu

后端 未结 4 1714
慢半拍i
慢半拍i 2021-01-12 07:42

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

4条回答
  •  难免孤独
    2021-01-12 08:10

    To create undefined behavior. That is not a legal call to execl. A correct call might be:

    execl( "/bin/ls", "ls", "-l", (char*)0 );
    

    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 argv of the executed program. The list of these arguments is terminated by a (char*)0; that's how the called function knows that the last argument has been reached. In the above example, for example, the executable at "/bin/ls" will replace your code; in its main, it will have argc equal 2, with argv[0] equal "ls", and argv[1] equal "-l".

    Immediately after this function, you should have the error handling code. (execl always returns -1, when it returns, so you don't need to test it. And it only returns if there was some sort of error.)

提交回复
热议问题