This commit is contained in:
Debucquoy
2023-10-18 20:27:40 +02:00
parent 4de4dcf2c2
commit b0f02b0d5d
29 changed files with 700 additions and 2 deletions

38
bac2/os/chap1/ex3.c Normal file
View File

@ -0,0 +1,38 @@
#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;
}