cours_progra/bac2/os/chap1/ex3.c
Debucquoy b0f02b0d5d
.
2023-10-18 20:27:40 +02:00

39 lines
611 B
C

#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
int counter=1;
void handler(int sig){
if(sig == SIGINT){
printf("Early ending. the current ammount of application is : %d\n", counter);
exit(0);
}
}
int collatz(int n){
sleep(1);
if (n % 2 == 0)
return n / 2;
return 3 * n + 1;
}
int main(int argc, char *argv[])
{
signal(SIGINT, handler);
if (argc != 2)
exit(1);
int test = atoi(argv[1]);
printf("%d\n", test);
while(test != 1){
counter++;
test = collatz(test);
printf("%d\n", test);
}
printf("---\n%d\n", counter);
return 0;
}