fork.c
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h> int main(void)
{
pid_t result; result = fork(); if (result == -)
{
printf("Fork Error!\n");
}
else
{
if (result == )
{
printf("The returned value is %d\nThe child process!!\nMy PID is %d\n", result, getpid());
}
else
{
printf("The returned value is %d\nThe father process!!\nMy PID is %d\n", result, getpid());
}
} return ;
}
exec.c
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h> int main(void)
{
pid_t result; result = fork(); if (result == )
{
if (execlp("ps", "ps", "-ef", NULL) < )
{
printf("Execlp error\n");
}
} return ;
}
waitpid.c
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h> int main(void)
{
pid_t pc, pr; pc = fork(); if (pc < )
{
printf("Error fork\n");
}
else if (pc == )
{
sleep();
exit();
}
else
{
do
{
pr = waitpid(pc, NULL, WNOHANG); if(pr == )
{
printf("The child process has not exited!\n");
sleep();
}
}while (pr == ); if (pr == pc)
{
printf("Get child exit code: %d\n", pr);
}
else
{
printf("Some error occured.\n");
}
} return ;
}
参考资料:《嵌入式Linux应用程序开发标准教程》