我試圖用fork和execlp程序,其中父地址空間替換爲「ls」命令。叉和Execlp
#include<stdio.h>
main()
{
int pid,j=10,fd;
pid=fork();
if(pid==0)
{
printf("\nI am the child\n");
execlp("/bin/ls","ls",NULL);
printf("\nStill I am the child\n");
}
else if (pid > 0)
{
printf("\n I am the parent\n");
wait();
}
}
當我執行程序的孩子
printf("\nStill I am the child\n");
的最後一行不打印。爲什麼?
幾個好點,但在新的過程中,pid肯定是不同的。另外,如果exec成功,exec只會替換進程,否則它將在exec調用之後繼續 – techdude