If the process is a car, then at least there are three states of startup, running, and stopping. However, the biggest difference between the process and the car is that the process can be self-replicating, as long as the use of function fork can be like the Monkey King again to become a self. But there are no two identical things in the world, and the process of changing it has its own ID number (PID), its own age (running time). These are different from the parent process. Through the getpid process the car can also obtain its own license plate number, GETPPID can get the PID of the parent process.
Then the parent process cannot get the PID of the child process. Only through the return value of the fork.
#include <sys/types.h>
//pid_t This type is to define the//./usr/include/sys/types.h #include inside this header file
<stdio.h >
int Main ()
{
pid_t pid;
PID = fork ();
Fork has a return value from both the child process and the parent process/
/He returns a 0//in the subprocess
pid
if (pid<0)
{
printf ("Fork error!\ n ");
Exit (1);
}
else if (pid==0)
{
printf ("I am a child process.") PID=[%D] ppid=[%d] \ n ", Getpid (), Getppid ());
} else
{
printf ("I am the parent process.") PID=[%D] ppid=[%d] \ n ", Getpid (), Getppid ());
}
Getpid () gets the PID exit (0) of the process's
parent process ()//getppid ().
Example 2
#include <sys/types.h>
#include <stdio.h>
#include <fcntl.h>//This header file contains the function int for operation file
Main ()
{
int fd = open ("Tmp.txt", O_RDWR);//read-write to open
if (FD < 0)
{
printf ("Open file failed \ n");
}
pid_t pid;
PID = fork ();
if (pid<0)
{
printf ("fork error!\n");
Exit (1);
}
else if (pid==0)//subprocess Append content
{
char string_tmp[10]= "012345678";
Write (Fd,string_tmp,strlen (string_tmp));
else//Parent Process file offset
{
if (Lseek (fd,0l,2) <0)
{
printf ("File offset failed \ n");
Exit (1);
}
}
Close (FD);
Exit (0);
}
Zombie Process
The parent process is not finished, the child process is finished, but the parent process does not have the resources to reclaim the child process, at which point the subprocess becomes a zombie (his life is over, but still hangs there).
[Back@dd ~]$ ps-ef|grep a.out
back 5013 3257 0 06:44 pts/0 00:00:00 ./a.out
back 5014 5013 0 06:44 pts/0 00:00:00 [a.out] <defunct>
back 5016 4987 0 06:45 PTS/1 00:00:00 grep--color=auto a.out
Kill 5013
Wait can prevent the zombie process from happening
#include <sys/types.h>
#include <stdio.h>
int main ()
{
pid_t pid;
PID = fork ();
if (PID < 0)
{
printf ("Create Child process failed");
} else if (pid>0)
{wait
(0);//Workaround: Wait lets the parent process reclaim the child process's resources while
(1)/Let the parent process loop
{sleep
(1);
}
}else
{
printf ("child process starts executing \ n");
printf ("Child process end execution \ n");
Exit (1);
}
return 0;
}
The orphan process, the parent process is over, the subprocess is still running, and will be adopted by INIT.
#include <sys/types.h>
#include <stdio.h>
int main ()
{
pid_t pid;
PID = fork ();
if (PID < 0)
{
printf ("Create Child process failed");
} else if (pid>0)
{
sleep (3);/Break
printf ("Child pid:%d,ppid:%d\n", Getpid (), Getppid ());
Exit (1);
} else
{
printf ("Child pid:%d,ppid:%d\n", Getpid (), Getppid ());
}
Sleep (7)//Break
printf ("Child pid:%d,ppid:%d\n", Getpid (), Getppid ());
return 0;
}