1.孤儿进程
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
int main(int argc, const char *argv[])
{
if(fork() == 0)
{
//子进程运行
while(1)
{
printf("child %d %d\n", getppid(), getpid());
sleep(1);
}
}
return 0;
}
2.僵尸进程
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
int main(int argc, const char *argv[])
{
if(fork()>0)
{
//父进程运行
while(1)
{
printf("parent %d\n",getpid());
sleep(1);
}
}
return 0;
}
3.幽灵进程
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
int main(int argc, const char *argv[])
{
//创建一个孤儿进程
if(fork() == 0)
{
//创建新的进程组 会话组
pid_t sid = setsid();
printf("sid = %d\n", sid);
//修改运行目录为不可卸载的文件
chdir("/");
//将umask清0
umask(0);
//关闭所有文件描述符
for(int i=0; i<getdtablesize(); i++)
{
close(i);
}
while(1)
{
//功能代码
sleep(1);
}
}
return 0;
}