|
发表于 2004-4-7 20:50:26
|
显示全部楼层
wait.h的作用就是那个if 中的宏,不要也可以(只是程序没那么健壮)
[code:1]
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <time.h>
//if you can't get through compile while including <wait.h>,comment out <wait.h>
//#include <wait.h>
int main(){
pid_t pid;
char *message;
int n;
struct tm *tm_ptr;
time_t cur_time;
time(&cur_time);
tm_ptr = localtime(&cur_time);
printf("Local time: %s",asctime(tm_ptr));
pid = fork();
switch(pid){
case -1:
perror("fork failed!\n");
exit(0);
case 0:
for (n=0;n<3;n++){
cur_time = time(NULL);
tm_ptr = localtime(&cur_time);
printf("Current time: %02d:%02d:%02d\n",tm_ptr->tm_hour,tm_ptr->tm_min,tm_ptr->tm_sec);
sleep(1);
}
break;
default:
break;
}
if(pid !=0){
int status;
pid_t child_pid;
child_pid = wait(&status);
// if (!WIFEXITED(status))
//you can capture other exit status use MACRO WIFSIGNALED and WIFSTOPPED
// printf("abnormal termination\n");
}
exit(0);
}
[/code:1] |
|