.
This commit is contained in:
		
										
											Binary file not shown.
										
									
								
							@ -1,5 +1,7 @@
 | 
			
		||||
#!/bin/sh
 | 
			
		||||
 | 
			
		||||
set -xe
 | 
			
		||||
 | 
			
		||||
for i in $(ls *.c); do
 | 
			
		||||
	gcc $i -o $(echo $i | cut -d '.' -f 1)
 | 
			
		||||
done
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										
											BIN
										
									
								
								bac2/os/chap1/chapitre1-evenements.pdf
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								bac2/os/chap1/chapitre1-evenements.pdf
									
									
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							@ -19,9 +19,16 @@ void handler(int sig, siginfo_t* info, void* context) {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int main(int argc, char *argv[]){
 | 
			
		||||
 | 
			
		||||
	SIGS context = {.sa_flags = SA_SIGINFO, .sa_sigaction = handler};
 | 
			
		||||
	while(1)
 | 
			
		||||
		pause();
 | 
			
		||||
	sigaction(SIGUSR1, &context, NULL);
 | 
			
		||||
	sigaction(SIGUSR2, &context, NULL);
 | 
			
		||||
 | 
			
		||||
	while(1){
 | 
			
		||||
		printf("The program is running...\n");
 | 
			
		||||
		sleep(1);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										33
									
								
								bac2/os/chap1/ex2.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										33
									
								
								bac2/os/chap1/ex2.c
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,33 @@
 | 
			
		||||
#include <signal.h>
 | 
			
		||||
#include <stdio.h>
 | 
			
		||||
#include <stdlib.h>
 | 
			
		||||
#include <stdint.h>
 | 
			
		||||
#include <unistd.h>
 | 
			
		||||
 | 
			
		||||
typedef struct sigaction SIGS;
 | 
			
		||||
 | 
			
		||||
uint8_t kill_counter = 0;
 | 
			
		||||
const char* sentences[] = {
 | 
			
		||||
	"Just give me a moment.",
 | 
			
		||||
	"I said I need a moment!",
 | 
			
		||||
	"Fine. I'm out of here....",
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
void handler(int sig, siginfo_t* info, void* context) {
 | 
			
		||||
	printf("%s\n", sentences[kill_counter++]);
 | 
			
		||||
	if(kill_counter >= 3)
 | 
			
		||||
		abort();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int main(int argc, char *argv[])
 | 
			
		||||
{
 | 
			
		||||
	SIGS event = {
 | 
			
		||||
		.sa_sigaction = handler,
 | 
			
		||||
		.sa_flags = SA_SIGINFO
 | 
			
		||||
	};
 | 
			
		||||
	sigaction(SIGINT, &event, NULL);
 | 
			
		||||
	while(1)
 | 
			
		||||
		pause();
 | 
			
		||||
 | 
			
		||||
	return 0;
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										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;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										26
									
								
								bac2/os/chap1/ex4.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										26
									
								
								bac2/os/chap1/ex4.c
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,26 @@
 | 
			
		||||
#include <signal.h>
 | 
			
		||||
#include <stdio.h>
 | 
			
		||||
 | 
			
		||||
typedef struct sigaction SIGS;
 | 
			
		||||
 | 
			
		||||
void handler(int sig, siginfo_t* info, void* context){
 | 
			
		||||
	if(sig == SIGFPE){
 | 
			
		||||
		printf("Division par zero... rip in peperoni...\n");
 | 
			
		||||
		exit(1);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int main(int argc, char *argv[])
 | 
			
		||||
{
 | 
			
		||||
	SIGS event = {
 | 
			
		||||
		.sa_flags = SA_SIGINFO,
 | 
			
		||||
		.sa_sigaction = handler	
 | 
			
		||||
	};
 | 
			
		||||
 | 
			
		||||
	sigaction(SIGFPE, &event, NULL);
 | 
			
		||||
 | 
			
		||||
	printf("I want to break free... \n");
 | 
			
		||||
	printf("%d\n", 1/0);
 | 
			
		||||
	return 0;
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										19
									
								
								bac2/os/chap1/ex5.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										19
									
								
								bac2/os/chap1/ex5.c
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,19 @@
 | 
			
		||||
#include <signal.h>
 | 
			
		||||
#include <stdio.h>
 | 
			
		||||
#include <unistd.h>
 | 
			
		||||
 | 
			
		||||
void handler(int sig){
 | 
			
		||||
	if(sig == SIGINT)
 | 
			
		||||
		while(1)
 | 
			
		||||
			printf("I cannot DIE !!!!\n");
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int main(int argc, char *argv[])
 | 
			
		||||
{
 | 
			
		||||
	signal(SIGINT, handler);
 | 
			
		||||
	signal(SIGQUIT, handler);
 | 
			
		||||
	signal(SIGTERM, handler);
 | 
			
		||||
	while(1)
 | 
			
		||||
		pause();
 | 
			
		||||
	return 0;
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										22
									
								
								bac2/os/chap1/ex6.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										22
									
								
								bac2/os/chap1/ex6.c
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,22 @@
 | 
			
		||||
#include <signal.h>
 | 
			
		||||
#include <stdlib.h>
 | 
			
		||||
#include <stdio.h>
 | 
			
		||||
#include <unistd.h>
 | 
			
		||||
 | 
			
		||||
void handler(int sig){
 | 
			
		||||
	printf("test");
 | 
			
		||||
	exit(0);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int main(int argc, char *argv[])
 | 
			
		||||
{
 | 
			
		||||
	signal(SIGALRM, handler);
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
	alarm(5);
 | 
			
		||||
	
 | 
			
		||||
	getchar();
 | 
			
		||||
	printf("finished without a thing");
 | 
			
		||||
 | 
			
		||||
	return 0;
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										50
									
								
								bac2/os/chap1/group.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										50
									
								
								bac2/os/chap1/group.c
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,50 @@
 | 
			
		||||
#include <signal.h>
 | 
			
		||||
#include <stdlib.h>
 | 
			
		||||
#include <stdio.h>
 | 
			
		||||
#include <unistd.h>
 | 
			
		||||
#include <termios.h>
 | 
			
		||||
#include <assert.h>
 | 
			
		||||
#include <string.h>
 | 
			
		||||
 | 
			
		||||
struct {
 | 
			
		||||
	int index;
 | 
			
		||||
	char tab[1024];
 | 
			
		||||
} buf = {0};
 | 
			
		||||
 | 
			
		||||
struct termios term, previous; // the previous state has to be restored... if not the term stay in that mode
 | 
			
		||||
 | 
			
		||||
void handler(int sig){
 | 
			
		||||
	switch (sig) {
 | 
			
		||||
		case SIGALRM:
 | 
			
		||||
			for (int i = 0; i < buf.index; ++i) {
 | 
			
		||||
				if(!buf.tab[i])
 | 
			
		||||
					continue;
 | 
			
		||||
				printf("%c", buf.tab[i] + 'A' - 'a');
 | 
			
		||||
			}
 | 
			
		||||
			alarm(5);
 | 
			
		||||
			break;
 | 
			
		||||
		default:
 | 
			
		||||
			tcsetattr(STDIN_FILENO, TCSANOW, &previous);
 | 
			
		||||
			raise(sig);
 | 
			
		||||
			assert(1);
 | 
			
		||||
			break;
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int main(int argc, char *argv[])
 | 
			
		||||
{
 | 
			
		||||
	signal(SIGALRM, handler);
 | 
			
		||||
	signal(SIGINT, handler);
 | 
			
		||||
 | 
			
		||||
	tcgetattr(STDIN_FILENO, &previous);
 | 
			
		||||
    memcpy(&term, &previous, sizeof(struct termios));
 | 
			
		||||
	term.c_lflag &= ~ICANON;
 | 
			
		||||
	tcsetattr(STDIN_FILENO, TCSANOW, &term);
 | 
			
		||||
 | 
			
		||||
	alarm(5);
 | 
			
		||||
	
 | 
			
		||||
	while(1){
 | 
			
		||||
		buf.tab[buf.index++] = getchar();
 | 
			
		||||
	}
 | 
			
		||||
	assert(1);
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user