SCR/SCR3.1/TP4/mytimeout.c

42 lines
819 B
C
Raw Normal View History

2023-09-29 12:32:18 +02:00
#include <stdlib.h>
#include <stdio.h>
2023-10-05 07:53:10 +02:00
#include <string.h>
2023-09-29 12:32:18 +02:00
#include <unistd.h>
#include <sys/types.h>
2023-10-05 07:53:10 +02:00
#include <sys/wait.h>
2023-09-29 12:32:18 +02:00
#include <signal.h>
2023-10-05 07:53:10 +02:00
#include "helpers.c"
#include "helpers.h"
#define BUF_SIZE 256
2023-09-29 12:32:18 +02:00
void kill_son(int){
2023-10-05 07:53:10 +02:00
if (waitpid(-1,NULL,WNOHANG)==0)
kill(0, SIGKILL);
}
2023-09-29 12:32:18 +02:00
int main(int argc, char **argv){
2023-10-05 07:53:10 +02:00
char* args[argc - 2];
char path[BUF_SIZE] = "/usr/bin/";
int secondes = (int) strtod(argv[1], NULL);
pid_t p;
2023-09-29 12:32:18 +02:00
if (argc < 3){
printf("Usage : %s <nbsec> <com> [arg]", argv[0]);
return EXIT_FAILURE;
}
2023-10-05 07:53:10 +02:00
strcat(path, argv[2]);
for (int i = 2; i < argc; i++)
args[i - 2] = argv[i];
2023-09-29 12:32:18 +02:00
p = fork();
switch(p){
2023-10-05 07:53:10 +02:00
case 0:
execv(path, args);
exit(0);
2023-09-29 12:32:18 +02:00
case -1:
2023-10-05 07:53:10 +02:00
exit(EXIT_FAILURE);
2023-09-29 12:32:18 +02:00
default:
2023-10-05 07:53:10 +02:00
alarm(secondes);
set_signal_handler(SIGALRM, &kill_son);
sleep(secondes);
}
exit(0);
}