分类: 嵌入式
2010-08-10 11:05:31
完成一个进程实验。
原理:
1 进程ID
函数:getpid, geppid
2 创建进程
函数:fork
3 等待进程
函数:waitpid
步骤:
1 宿主机下进行编辑,交叉编译。
2 开发板终端上进行程序的执行(NFS)。
现象:
宿主机
开发板
代码来自周立功:
#include
#include
#include
#include
#include
#include
int main(int argc, char **argv)
{
int stat;
pid_t child;
printf("\nTry to create new process.\n");
child=fork();
switch(child)
{
case -1:
perror("fork.\n");
exit(EXIT_FAILURE);
case 0:
printf("This is child.\n");
printf("\tchild pid is %d\n",getpid());
printf("\tchild ppid is %d\n",getppid());
exit(EXIT_SUCCESS);
default:
waitpid(child,&stat,0);
printf("This is parent.\n");
printf("\tparent pid is %d\n",getpid());
printf("\tparent ppid is %d\n",getppid());
printf("\tchild exited with %d\n",stat);
}
exit(EXIT_SUCCESS);
}