//
$Id$
/* *
* @file waitpid_test.c
* @brief waitpid 测试例子
*
* <br>版权所有© 2004~2005,罗智勇 <br>
* All rights reserved. <br>
*
* @version $Revision$
* @author 罗智勇
* @date 2006年10月13日 15:43:02
* */
#include < sys / wait.h >
#include < stdio.h >
#include < stdlib.h >
#include < errno.h >
int main( int argc, const char * argv[])
{
int status = 0 ;
pid_t ret_pid;
pid_t child_pid;
const char * child_name = " test " ;
const char * default_child_argv[] = { child_name, 0 };
const char ** child_argv = default_child_argv;
fprintf(stdout, " waitpid test. v1.0 " );
if (argc >= 2 ){
child_name = argv[ 1 ];
child_argv = & (argv[ 1 ]);
} else {
fprintf(stdout, " Useage : %s [app_name [app_param]] " , argv[ 0 ]);
}
pid_t pid;
pid = fork();
if ( pid < 0 ){
perror( " Error in fork " );
return - 1 ;
} else if ( pid > 0 ){
// 父进程空间
child_pid = pid;
} else {
// 子进程空间
fprintf( stderr, " ---------------------------------- " );
printf( " Child PID is %ld " , ( long ) getpid());
# if 1
chdir( " /tmp " );
// 设置文件权限掩码
umask( 0 );
execv( child_name, ( char * const * )child_argv );
fprintf( stderr, " execv("%s"...) error. " , child_name);
return - 1 ;
# else
if (argc == 1 )
pause(); /* Wait for signals */
_exit(atoi(argv[ 1 ]));
#endif
}
do {
ret_pid = waitpid( child_pid, & status, WUNTRACED | WCONTINUED ); // WNOHANG
fprintf( stderr, " ---------------------------------- " );
if ( ret_pid == - 1 )
{
fprintf( stderr, " waitpid error : (%d)%s " , errno, strerror(errno) );
return - 1 ;
} else if ( ret_pid != child_pid ){
fprintf( stderr, " waitpid abnormal, wait %d, but return %d, return val = %d. Error : (%d)%s "
, child_pid , ret_pid, status
, errno, strerror(errno) );
return - 1 ;
}
if ( WIFEXITED( status ) )
{
status = WEXITSTATUS( status );
fprintf(stderr, " The child return. Return value = 0x%x " , status);
break ;
}
else if (WIFSIGNALED(status))
{
fprintf( stderr, " Killed by signal 0x%x. Return value = 0x%x " , WTERMSIG(status), status);
#ifdef WCOREDUMP
if (WCOREDUMP(status))
fprintf( stderr, " The child produced a core dump. " );
#endif
break ;
}
else if (WIFSTOPPED(status))
{
// 子进程暂停,继续等待
fprintf(stderr, " Stopped by signal %d " , WSTOPSIG(status));
}
else
{
#ifdef WIFCONTINUED
if (WIFCONTINUED(status))
fprintf( stderr, " (Continued. " );
else
#endif
{
// 子进程因为其它原因使 wait_pid 返回
fprintf(stderr, " waitpid exit. The reason is unknow. return value = 0x%x " , status);
}
}
} while ( 1 );
return status;
}
/* *
* @file waitpid_test.c
* @brief waitpid 测试例子
*
* <br>版权所有© 2004~2005,罗智勇 <br>
* All rights reserved. <br>
*
* @version $Revision$
* @author 罗智勇
* @date 2006年10月13日 15:43:02
* */
#include < sys / wait.h >
#include < stdio.h >
#include < stdlib.h >
#include < errno.h >
int main( int argc, const char * argv[])
{
int status = 0 ;
pid_t ret_pid;
pid_t child_pid;
const char * child_name = " test " ;
const char * default_child_argv[] = { child_name, 0 };
const char ** child_argv = default_child_argv;
fprintf(stdout, " waitpid test. v1.0 " );
if (argc >= 2 ){
child_name = argv[ 1 ];
child_argv = & (argv[ 1 ]);
} else {
fprintf(stdout, " Useage : %s [app_name [app_param]] " , argv[ 0 ]);
}
pid_t pid;
pid = fork();
if ( pid < 0 ){
perror( " Error in fork " );
return - 1 ;
} else if ( pid > 0 ){
// 父进程空间
child_pid = pid;
} else {
// 子进程空间
fprintf( stderr, " ---------------------------------- " );
printf( " Child PID is %ld " , ( long ) getpid());
# if 1
chdir( " /tmp " );
// 设置文件权限掩码
umask( 0 );
execv( child_name, ( char * const * )child_argv );
fprintf( stderr, " execv("%s"...) error. " , child_name);
return - 1 ;
# else
if (argc == 1 )
pause(); /* Wait for signals */
_exit(atoi(argv[ 1 ]));
#endif
}
do {
ret_pid = waitpid( child_pid, & status, WUNTRACED | WCONTINUED ); // WNOHANG
fprintf( stderr, " ---------------------------------- " );
if ( ret_pid == - 1 )
{
fprintf( stderr, " waitpid error : (%d)%s " , errno, strerror(errno) );
return - 1 ;
} else if ( ret_pid != child_pid ){
fprintf( stderr, " waitpid abnormal, wait %d, but return %d, return val = %d. Error : (%d)%s "
, child_pid , ret_pid, status
, errno, strerror(errno) );
return - 1 ;
}
if ( WIFEXITED( status ) )
{
status = WEXITSTATUS( status );
fprintf(stderr, " The child return. Return value = 0x%x " , status);
break ;
}
else if (WIFSIGNALED(status))
{
fprintf( stderr, " Killed by signal 0x%x. Return value = 0x%x " , WTERMSIG(status), status);
#ifdef WCOREDUMP
if (WCOREDUMP(status))
fprintf( stderr, " The child produced a core dump. " );
#endif
break ;
}
else if (WIFSTOPPED(status))
{
// 子进程暂停,继续等待
fprintf(stderr, " Stopped by signal %d " , WSTOPSIG(status));
}
else
{
#ifdef WIFCONTINUED
if (WIFCONTINUED(status))
fprintf( stderr, " (Continued. " );
else
#endif
{
// 子进程因为其它原因使 wait_pid 返回
fprintf(stderr, " waitpid exit. The reason is unknow. return value = 0x%x " , status);
}
}
} while ( 1 );
return status;
}