# Include <sys/IPC. h>
# Include <sys/SEM. h>
# Include <sys/Wait. H>
# Include <signal. h>
# Include <sys/types. h>
# Include <sys/STAT. h>
# Include <unistd. h>
# Include <stdio. h>
# Include <errno. h>
# Include <sys/types. h>
# Include <sys/STAT. h>
# Include <fcntl. h>
Static void sig_child (INT signo)
{
Pid_t PID;
Int Stat;
While (pid = waitpid (-1, & stat, wnohang)> 0 );
Return;
}
Int main ()
{
Signal (sigchld, sig_child );
Int npid =-1;
Npid = fork ();
If (npid = 0) {// child
Clearfile ();
} Else {// parent
While (1 ){
Sleep (3 );
If (getpgid (npid) <0 ){
Kill (npid, 9 );
Npid = fork ();
If (npid = 0 ){
Clearfile ();
}
}
}
}
}
Void clearfile ()
{
Struct stat statbuf;
Char szpath [] = "/home/jzb/source/myprint. log ";
Int nret;
Int FD;
While (1 ){
Nret = Stat (szpath, & statbuf );
If (statbuf. st_size> = 1024*1024*1024 ){
FD = open (szpath, o_creat | o_trunc );
Close (FD );
}
Sleep (1 );
}
}
The main program continuously checks whether the process is valid through the while loop (generally, when the process is terminated, the process group number is-1. Therefore, it determines whether the process is running ). If the sub-process is detected to be completed, the sub-process is restarted.