Monitoring Program (process group number)

Source: Internet
Author: User

# 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.