.
This commit is contained in:
@ -1,5 +1,7 @@
|
||||
#!/bin/sh
|
||||
|
||||
set -xe
|
||||
|
||||
for i in $(ls *.c); do
|
||||
gcc $i -o $(echo $i | cut -d '.' -f 1)
|
||||
done
|
||||
|
@ -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);
|
||||
}
|
26
bac2/os/chap2/Makefile
Normal file
26
bac2/os/chap2/Makefile
Normal file
@ -0,0 +1,26 @@
|
||||
.PHONY: clean, mrproper
|
||||
CC = gcc
|
||||
CFLAGS = -g -Wall
|
||||
|
||||
all: ex3 ex4
|
||||
|
||||
%.o: %.c
|
||||
$(CC) $(CFLAGS) -c -o $@ $<
|
||||
|
||||
ex3: ex3.o
|
||||
$(CC) $(CFLAGS) -o $@ $+
|
||||
|
||||
ex4: ex4.o
|
||||
$(CC) $(CFLAGS) -o $@ $+
|
||||
|
||||
ex5: ex5.o
|
||||
$(CC) $(CFLAGS) -o $@ $+
|
||||
|
||||
clean:
|
||||
rm -f *.o core.*
|
||||
|
||||
mrproper: clean
|
||||
rm -f ex3 ex4 ex5
|
||||
|
||||
run: ex5
|
||||
./ex5
|
BIN
bac2/os/chap2/chapitre2-memoire.pdf
Normal file
BIN
bac2/os/chap2/chapitre2-memoire.pdf
Normal file
Binary file not shown.
14
bac2/os/chap2/ex1.c
Normal file
14
bac2/os/chap2/ex1.c
Normal file
@ -0,0 +1,14 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#define TEST_SIZE 100
|
||||
|
||||
int main(void)
|
||||
{
|
||||
char* blop[TEST_SIZE];
|
||||
for (int i = 0; i < TEST_SIZE; ++i) {
|
||||
blop[i] = malloc(sizeof(char));
|
||||
printf("%p\n", blop[i]);
|
||||
}
|
||||
while(1);
|
||||
}
|
34
bac2/os/chap2/ex2.c
Normal file
34
bac2/os/chap2/ex2.c
Normal file
@ -0,0 +1,34 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
char* expand_tabs(const char* string);
|
||||
|
||||
int main(void)
|
||||
{
|
||||
const char* str = "This is a text This is another one. ' ' <- space; ' ' <-tab";
|
||||
printf("%s - %d\n", str, (int)strlen(str));
|
||||
printf("%s - %d\n", expand_tabs(str),(int) strlen(expand_tabs(str)));
|
||||
|
||||
}
|
||||
|
||||
char* expand_tabs(const char* string){
|
||||
int tab_count = 0, letter_count = 0;
|
||||
for(int i = 0; string[i] != '\0'; i++){
|
||||
letter_count++;
|
||||
if(string[i] == '\t')
|
||||
tab_count++;
|
||||
}
|
||||
char* ret = malloc(sizeof(char) * letter_count + 3*tab_count);
|
||||
char* filler = ret;
|
||||
for(int i = 0; string[i] != '\0'; i++){
|
||||
if(string[i] == '\t'){
|
||||
for (int j = 0; j < 4; ++j) {
|
||||
*(filler++) = ' ';
|
||||
}
|
||||
}else{
|
||||
*(filler++) = string[i];
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
BIN
bac2/os/chap2/ex3
Executable file
BIN
bac2/os/chap2/ex3
Executable file
Binary file not shown.
62
bac2/os/chap2/ex3.c
Normal file
62
bac2/os/chap2/ex3.c
Normal file
@ -0,0 +1,62 @@
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#define print_vec(vec) for (int i = 0; i < (vec)->size; ++i) { printf("%d, ", (vec)->data[i]); } printf("\n")
|
||||
|
||||
struct vec {
|
||||
int size;
|
||||
int *data;
|
||||
};
|
||||
|
||||
|
||||
struct vec* new(unsigned int n);
|
||||
struct vec* add(const struct vec *v, const struct vec *w);
|
||||
struct vec* smul(double s, const struct vec *v);
|
||||
|
||||
int main(void)
|
||||
{
|
||||
struct vec *v = new(4);
|
||||
v->data[0] = 1;
|
||||
v->data[1] = 2;
|
||||
v->data[2] = 3;
|
||||
v->data[3] = 4;
|
||||
print_vec(v);
|
||||
|
||||
struct vec *w = new(4);
|
||||
w->data[0] = 5;
|
||||
w->data[1] = 6;
|
||||
w->data[2] = 7;
|
||||
w->data[3] = 8;
|
||||
print_vec(w);
|
||||
|
||||
struct vec *added = add(v, w);
|
||||
print_vec(added);
|
||||
|
||||
struct vec *multiplied = smul(3, v);
|
||||
print_vec(multiplied);
|
||||
}
|
||||
|
||||
struct vec* new(unsigned int n){
|
||||
struct vec* ret = malloc(sizeof(struct vec));
|
||||
ret->size = n;
|
||||
ret->data = malloc(sizeof(int) * n);
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
struct vec* add(const struct vec *v, const struct vec *w){
|
||||
if(v->size != w->size)
|
||||
return NULL;
|
||||
struct vec *ret = new(v->size);
|
||||
for (int i = 0; i < v->size; ++i) {
|
||||
ret->data[i] = v->data[i] + w->data[i];
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
struct vec* smul(double s, const struct vec *v){
|
||||
struct vec *ret = new(v->size);
|
||||
for (int i = 0; i < v->size; ++i) {
|
||||
ret->data[i] = v->data[i] * s;
|
||||
}
|
||||
return ret;
|
||||
}
|
BIN
bac2/os/chap2/ex3.o
Normal file
BIN
bac2/os/chap2/ex3.o
Normal file
Binary file not shown.
BIN
bac2/os/chap2/ex4
Executable file
BIN
bac2/os/chap2/ex4
Executable file
Binary file not shown.
61
bac2/os/chap2/ex4.c
Normal file
61
bac2/os/chap2/ex4.c
Normal file
@ -0,0 +1,61 @@
|
||||
#include <inttypes.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#define AT(mat, x, y) ((mat)->data[((x)-1) + ( ((y)-1) * ((mat)->w) )])
|
||||
|
||||
#define PRINT_MAT(mat) for (int i = 1; i <= (mat)->h; ++i) { for (int j = 1; j <= (mat)->w; ++j) { printf("%d,", AT((mat),(j), (i))); } printf("\n"); } printf("\n");
|
||||
|
||||
typedef struct {
|
||||
uint8_t w, h;
|
||||
int *data;
|
||||
} MAT;
|
||||
|
||||
MAT *new(uint8_t, uint8_t);
|
||||
MAT *mul(MAT*, MAT*);
|
||||
|
||||
int main(void)
|
||||
{
|
||||
MAT* a = new(3, 2);
|
||||
MAT* b = new(3, 3);
|
||||
MAT* mult;
|
||||
|
||||
|
||||
AT(a, 1, 1) = 0; AT(a, 2, 1) = 1; AT(a, 3, 1) = 2;
|
||||
AT(a, 1, 2) = 3; AT(a, 2, 2) = 4; AT(a, 3, 2) = 5;
|
||||
/* AT(a, 1, 3) = 6; AT(a, 2, 3) = 7; AT(a, 3, 3) = 8; */
|
||||
PRINT_MAT(a);
|
||||
|
||||
AT(b, 1, 1) = 1; AT(b, 2, 1) = 0; AT(b, 3, 1) = 0;
|
||||
AT(b, 1, 2) = 0; AT(b, 2, 2) = 1; AT(b, 3, 2) = 0;
|
||||
AT(b, 1, 3) = 0; AT(b, 2, 3) = 0; AT(b, 3, 3) = 1;
|
||||
PRINT_MAT(b);
|
||||
|
||||
mult = mul(a,b);
|
||||
PRINT_MAT(mult);
|
||||
|
||||
}
|
||||
|
||||
MAT *new(uint8_t w, uint8_t h){
|
||||
MAT* ret = malloc(sizeof(MAT));
|
||||
ret->w = w;
|
||||
ret->h = h;
|
||||
ret->data = malloc(sizeof(int) * w * h);
|
||||
return ret;
|
||||
}
|
||||
|
||||
MAT *mul(MAT *a, MAT *b){
|
||||
if(a->w != b->h)
|
||||
return NULL;
|
||||
MAT* ret = new(b->w, a->h);
|
||||
for (int i = 1; i <= a->h; ++i) {
|
||||
for (int j = 1; j <= b->w; ++j) {
|
||||
int tmp = 0;
|
||||
for (int k = 1; k <= a->w; ++k) {
|
||||
tmp += AT(a, k, i) * AT(b, j, k);
|
||||
}
|
||||
AT(ret, j, i) = tmp;
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
BIN
bac2/os/chap2/ex4.o
Normal file
BIN
bac2/os/chap2/ex4.o
Normal file
Binary file not shown.
Binary file not shown.
41
bac2/os/chap2/ex5.c
Normal file
41
bac2/os/chap2/ex5.c
Normal file
@ -0,0 +1,41 @@
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
struct node{
|
||||
struct node* prev;
|
||||
void* data;
|
||||
};
|
||||
|
||||
typedef struct node* LIFO;
|
||||
|
||||
LIFO* mklifo ();
|
||||
void push(LIFO* lst , void*);
|
||||
void* pop(LIFO* lst);
|
||||
|
||||
int main(void)
|
||||
{
|
||||
char* t = "test";
|
||||
LIFO* lifo = mklifo();
|
||||
push(lifo, t);
|
||||
printf("%s", (char *)pop(lifo));
|
||||
|
||||
}
|
||||
|
||||
LIFO *mklifo (){
|
||||
LIFO* ret = malloc(sizeof(struct node));
|
||||
return ret;
|
||||
}
|
||||
|
||||
void push(LIFO *lst , void *el){
|
||||
LIFO* next = mklifo();
|
||||
(*lst)->data = el;
|
||||
(*next)->prev = *lst;
|
||||
lst = next;
|
||||
}
|
||||
|
||||
void *pop(LIFO *lst){
|
||||
void* el;
|
||||
(*lst)->data = el;
|
||||
*lst = (*lst)->prev;
|
||||
return el;
|
||||
}
|
BIN
bac2/os/chap2/ex5.o
Normal file
BIN
bac2/os/chap2/ex5.o
Normal file
Binary file not shown.
Reference in New Issue
Block a user