linux进程控制开发实例

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应用程序开发标准教程》

上一篇:Java中关于类型自动提升的两个注意点。


下一篇:java学习二 数据类型自动提升 0x开头的数据是 16进制且是int型