.
This commit is contained in:
38
bac2/os/chap1/ex3.c
Normal file
38
bac2/os/chap1/ex3.c
Normal 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;
|
||||
}
|
||||
|
Reference in New Issue
Block a user