孤儿进程
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main(int argc, const char *argv[])
{
int cpid = fork();
if(cpid < 0)
{
return -1;
}
else if(0 == cpid)
{
while(1)
{
printf("子进程\n");
sleep(1);
}
}
else
{
printf("父进程\n");
exit(0);
}
return 0;
}
僵尸进程
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
int main(int argc, const char *argv[])
{
pid_t cpid = fork();
if(cpid >0)
{
while(1)
{
printf("父进程\n");
sleep(1);
}
}
else if(0 == cpid)
{
printf("子进程\n");
exit(0);
}
else
{
perror("fork");
return -1;
}
return 0;
守护进程
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main(int argc, const char *argv[])
{
if(fork() ==0)
{
setsid();
chdir("/");
umask(0);
for(int i=0;i<getdtablesize();i++)
{
close(i);
}
while(1)
{
sleep(1);
}
}
return 0;
}