第一关
#include <unistd.h>
#include <sys/types.h>
#include <stdio.h>
/**********************
* pid: 当前进程ID
* ppid: 父进程ID
***********************/
struct procIDInfo
{
pid_t pid;
pid_t ppid;
};
/************************
* 返回值: 需要被打开的目录路径
*************************/
struct procIDInfo getProcInfo()
{
struct procIDInfo ret; //存放进程ID信息,并返回
/********** BEGIN **********/
ret.pid= getpid();
ret.ppid=getppid();
/********** END **********/
return ret;
}
第二关
#include <unistd.h>
#include <sys/types.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
/************************
* 提示: 不要在子进程或父进程中使用exit函数或者return来退出程序
*************************/
void createProcess()
{
/********** BEGIN **********/
pid_t pid=fork();
if(pid==-1)
{
printf("创建进程失败(%s)!\n", strerror(errno));
}
else if(!pid)
{
printf("Children",getpid(),getppid());
}
else
{
printf("Parent",getpid(),getppid());
}
/********** END **********/
}
第三关
#include <unistd.h>
#include <sys/types.h>
#include <stdlib.h>
#include <stdio.h>
/************************
* 提示: 用户需要在exitProcess函数中使用atexit函数注册一个自定义函数,并在自定义函数中打印出当前进程ID号
*************************/
void out()
{
printf("%d\n",getpid());
}
void exitProcess()
{
/********** BEGIN **********/
if(atexit(out)!=0)
{
printf("调用atexit函数错误\n");
}
}