Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <sys/wait.h>
- #include <unistd.h>
- #include <time.h>
- /*
- Pequano programa que mantém um processo a correr.
- Uso: ./daemon ./worldserver
- */
- int main(int argc, char *argv[]){
- time_t t;
- int status;
- int pid;
- // verifica o numero de argumentos
- if(argc != 2){
- printf("Error: Invalid number of arguments\n");
- return 0;
- }
- time(&t);
- printf("Process Started on %s", ctime(&t));
- // corre o executável passado como argumento
- pid = fork();
- if(!pid) execlp(argv[1], argv[1], NULL);
- while(1){
- // espera por alteração do filho
- wait(&status);
- // caso o filho terminou correctamente
- if(WIFEXITED(status)){
- time(&t);
- printf("Process Terminated on %s", ctime(&t));
- exit(0);
- }
- // caso o filho terminou inesperadamente
- time(&t);
- printf("Crashed (signal: %d) on %s", WTERMSIG(status), ctime(&t));
- // corre o executável passado como argumento
- pid = fork();
- if(!pid) execlp(argv[1], argv[1], NULL);
- }
- }
Add Comment
Please, Sign In to add comment