Linux wait(), waitpid(). , . , ( ), waitpid() . - , . waitpid() , .
waitpid(). ³ POSIX :
#include<sys/wait.h>
pid_t waitpid(pid_t pid, //pid ,
int *status, //
int options); // 0
pid 0, , , 1, - . :
pid_t pid;
if((pid=fork())==-1)
exit(-1);
if(pid==0)
{
// exec()
}
else
{
//
int status;
waitpid(pid,&status,0);
//
}
dz status -, . <sys/wait.h>:
WIFEXITED(status) , ;
WEXITSTATUS(status) ( WIFEXITED()!=0).
:
waitpid(pid,&status,0);
if(WIFEXITED(status))
printf( %d\n,
WEXITSTATUS(status));
䳿, . , POSIX, . (signal handler), 䳿 . , . .
䳿 UNIX-, , , 䳿.
. ( , , ). ֳ
, .
|
|
- :
, , POSIX- kill(), ;
( , - ).
ϳ , , .
( ):
;
, 䳿;
, ( , ).
.
, . , .
, , , . ( , , , . - .
, POSIX Linux ( ).
, , SIGSEGV (11), .
:
SIGHUP (1) (, );
SIGINT SIGQUIT (2,3) ( Ctrl+C Ctrl+\);
SIGKILL (9) ( );
SIGUSR1 SIGUSR2 (10,12) , ;
SIGTERM (15) ( , SIGKILL, ).
ij , SIGSEGV, ( SIGSEGV (core dump) , ).
sigaction().
#include<signal.h>
int sigaction(int signum, //
struct sigaction *action, //
struct sigaction *old_action); //
sigaction :
sa_handler - ;
sa_mask , , ;
|
|
sa_flag .
sa_mask sa_flag ( ):
struct sigaction action={0};
sa_handler , :
void user_handler(int signum)
{
//
}
.
signum , ( sigaction()).
ϳ ,
:
#include<signal.h>
void sigint_handler(int signum)
{
// SIGINT
}
//..
action.sa_handler=sigint_handler;
sigaction(SIGINT, &action,0);
, pause(). , - :
// sigaction()
pause(); //
, . kill().
#include(signal.h>
int kill(pid_t pid, //
int signum); //
, SIGHUP , pid :
kill(atoi(argv[1],SIGHUP);
, . , waitpid() .
- , - waitpid() . - -.
, , - - SIGCHLD. waitpid(), - -, .
void clear_zombie(int signum)
{
waitpid(-1,NULL,0);
}
struct sigaction action={0};
action.sa_handler=clear_zombie;
sigaction(SIGCHLD,&action,0);
if((pid=fork())==-1)
_exit();
if(pid==0)
{
//
exit();
}
else
{
// waitpid()
}