execlp() system call error in output

≡放荡痞女 提交于 2019-12-04 06:37:59

问题


This very simple example of exec() system call. Here, I am trying to call execlp() twice. But, I am not getting excepted output. It shows output only for first call with current directory.

#include <stdio.h>
#include <unistd.h>

int main() { 

        int ret1,ret2;
        ret1 = execlp( "pwd", "pwd", (char *) 0);
        ret2 = execlp( "date", "date", (char *) 0);
        return 0;
}

OUTPUT :

/home/aniket

Here, I can't see output for date. Can anyone please explain why is this happening?


回答1:


execlp() replaces the current process image with a new process image. It does not return (unless there was an error starting the new process).

Therefore the second execlp() call is never reached.




回答2:


execlp () replaces the process that called it by the process which was called.

From this link:

"The exec() family of functions replaces the current process image with a new process image."

To retain both the processes, use fork().




回答3:


#include <stdio.h>
#include <unistd.h>

int main() { 

        int ret1,ret2;

        pid_t chd;
        chd=fork(); 
        if(chd==0)
        ret1 = execlp( "pwd", "pwd", (char *) 0);
        else if(chd>0)
        ret2 = execlp( "date", "date", (char *) 0);
        return 0;
}


来源:https://stackoverflow.com/questions/22121464/execlp-system-call-error-in-output

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!