Renaming folders

This commit is contained in:
Fabio Scotto di Santolo
2024-09-11 10:47:59 +02:00
parent 67447f7370
commit 3c4b9bdc56
31 changed files with 0 additions and 0 deletions

54
chp3_process/collatz.c Normal file
View File

@@ -0,0 +1,54 @@
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
int collatz(int, int[], int);
void print_buffer(int buff[]);
int main(int argc, char *argv[]) {
if (argc < 2) {
perror("Usage: collatz NUMBER\n");
return EXIT_FAILURE;
}
int n = atoi(argv[1]);
if (n <= 1) {
perror("The number MUST be greater than 1\n");
return EXIT_FAILURE;
}
printf("Collatz of %d\n", n);
pid_t pid = fork();
if (pid == 0) {
int buff[1024] = {0};
collatz(n, buff, 0);
print_buffer(buff);
} else if (pid > 0) {
wait(NULL);
}
return EXIT_SUCCESS;
}
int collatz(int n, int buff[], int steps) {
buff[steps] = n;
if (n == 1) {
return n;
} else if (n % 2 == 0) {
return collatz(n / 2, buff, steps + 1);
} else {
return collatz(3 * n + 1, buff, steps + 1);
}
}
void print_buffer(int buff[]) {
int i = 0;
while (buff[i] != 0) {
printf("%d ", buff[i]);
i++;
}
printf("\n");
}

View File

@@ -0,0 +1,64 @@
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#define SIZE 4096
#define SHM_NAME "collatz"
int collatz(int, void *);
int main(int argc, char *argv[]) {
if (argc < 2) {
perror("Usage: collatz NUMBER\n");
return EXIT_FAILURE;
}
int n = atoi(argv[1]);
if (n <= 1) {
perror("The number MUST be greater than 1\n");
return EXIT_FAILURE;
}
printf("Collatz of %d\n", n);
// Create memory shared object
int fd = shm_open(SHM_NAME, O_CREAT | O_RDWR, 0600);
ftruncate(fd, SIZE);
void *ptr = (int *)mmap(0, SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
pid_t pid = fork();
if (pid == 0) {
collatz(n, ptr);
} else if (pid > 0) {
wait(NULL);
printf("%s\n", (char *)ptr);
shm_unlink(SHM_NAME);
}
return EXIT_SUCCESS;
}
int collatz(int n, void *shm) {
char buff[50];
sprintf(buff, "%d", n);
sprintf(shm, "%s", buff);
shm += strlen(buff);
sprintf(shm, "%s", " ");
shm += 1;
if (n == 1) {
return n;
} else if (n % 2 == 0) {
return collatz(n / 2, shm);
} else {
return collatz(3 * n + 1, shm);
}
}

View File

@@ -0,0 +1,60 @@
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
int collatz(int, int *, int);
void print_buffer(int buff[]);
int main(int argc, char *argv[]) {
if (argc < 2) {
perror("Usage: collatz NUMBER\n");
return EXIT_FAILURE;
}
int n = atoi(argv[1]);
if (n <= 1) {
perror("The number MUST be greater than 1\n");
return EXIT_FAILURE;
}
printf("Collatz of %d\n", n);
int *buff = mmap(NULL, sizeof(int *) * 1024, PROT_READ | PROT_WRITE,
MAP_SHARED | MAP_ANONYMOUS, -1, 0);
pid_t pid = fork();
if (pid == 0) {
collatz(n, buff, 0);
} else if (pid > 0) {
wait(NULL);
print_buffer(buff);
}
return EXIT_SUCCESS;
}
int collatz(int n, int *shm, int steps) {
*(shm + steps) = n;
if (n == 1) {
return n;
} else if (n % 2 == 0) {
return collatz(n / 2, shm, steps + 1);
} else {
return collatz(3 * n + 1, shm, steps + 1);
}
}
void print_buffer(int buff[]) {
int i = 0;
while (buff[i] != 0) {
printf("%d ", buff[i]);
i++;
}
printf("\n");
}

21
chp3_process/cons.c Normal file
View File

@@ -0,0 +1,21 @@
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/shm.h>
#include <sys/mman.h>
#include <sys/stat.h>
int main()
{
const int SIZE = 4096;
const char *name = "OS";
int shm_fd;
void *ptr;
shm_fd = shm_open(name, O_RDONLY, 0666);
ptr = mmap(0, SIZE, PROT_READ, MAP_SHARED, shm_fd, 0);
printf("%s", (char *)ptr);
shm_unlink(name);
}

46
chp3_process/ex1.c Normal file
View File

@@ -0,0 +1,46 @@
#include <sys/types.h>
#include <sys/wait.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
struct MyStruct {
int value;
};
int value = 5;
int main(void)
{
struct MyStruct *str;
str->value = 5;
int *p = malloc(sizeof(int));
*p = 100;
printf("PARENT: %p\n", &value);
printf("PARENT: %p\n", str);
printf("PARENT: %p\n", p);
pid_t pid;
pid = fork();
if (pid == 0) {
value += 15;
str->value += 15;
*p += 10;
printf("CHILD: %p, %d\n", &value, value);
printf("CHILD: %p, %d\n", &str->value, str->value);
printf("CHILD: %p, %d\n", p, *p);
return 0;
} else if (pid > 0) {
wait(NULL);
printf("PARENT: value = %d\n", value);
printf("PARENT: value = %d\n", str->value);
printf("PARENT: value = %d\n", *p);
free(p);
return 0;
}
}

39
chp3_process/ex12.c Normal file
View File

@@ -0,0 +1,39 @@
#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
void add_children(pid_t children[], pid_t pid);
int main(void)
{
pid_t parent_pid = getpid();
pid_t children[1024] = {-1};
for (int i = 0; i < 4; i++) {
pid_t pid = fork();
add_children(children, pid);
}
if (parent_pid = getpid()) {
wait(NULL);
printf("pids: ");
for (int i = 0; i < 1024; i++) {
if (children[i] == -1) {
printf("\n");
break;
}
printf("%d ", children[i]);
}
}
return 0;
}
void add_children(pid_t children[], pid_t pid)
{
for (int i = 0; i < 1024; i++) {
if (children[i] < 0) {
children[i] = pid;
break;
}
}
}

23
chp3_process/ex13.c Normal file
View File

@@ -0,0 +1,23 @@
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
int main(void)
{
pid_t pid;
pid = fork();
if (pid < 0) {
fprintf(stderr, "Fork failed\n");
return 1;
} else if (pid == 0) {
execlp("/bin/ls", "ls", NULL);
printf("LINEA J\n");
} else {
wait(NULL);
printf("Child complete\n");
}
return 0;
}

27
chp3_process/ex14.c Normal file
View File

@@ -0,0 +1,27 @@
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
int main(void) {
pid_t pid, pid1;
pid = fork();
if (pid < 0) {
fprintf(stderr, "Fork failed");
return EXIT_FAILURE;
} else if (pid == 0) {
pid1 = getpid();
printf("child: pid = %d\n", pid); // A
printf("child: pid1 = %d\n", pid1); // B
} else {
pid1 = getpid();
printf("parent: pid = %d\n", pid); // C
printf("parent: pid1 = %d\n", pid1); // D
wait(NULL);
}
return EXIT_SUCCESS;
}

29
chp3_process/ex17.c Normal file
View File

@@ -0,0 +1,29 @@
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <unistd.h>
#define SIZE 5
int nums[SIZE] = {0, 1, 2, 3, 4};
int main(void) {
int i;
pid_t pid;
pid = fork();
if (pid == 0) {
for (i = 0; i < SIZE; i++) {
nums[i] *= -i;
printf("CHILD : %d\n", nums[i]); // Linea X
}
} else if (pid > 0) {
wait(NULL);
for (i = 0; i < SIZE; i++) {
printf("PARENT: %d\n", nums[i]); // Linea Y
}
}
return EXIT_SUCCESS;
}

18
chp3_process/ex19.c Normal file
View File

@@ -0,0 +1,18 @@
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
int main(void) {
pid_t parent_pid = getpid();
pid_t pid = fork();
if (pid > 0) {
printf("PARENT PID = %d, CHILD PID = %d\n", parent_pid, pid);
sleep(10);
wait(NULL);
}
return EXIT_SUCCESS;
}

2
chp3_process/osh_shell/.gitignore vendored Normal file
View File

@@ -0,0 +1,2 @@
osh_history
osh

View File

@@ -0,0 +1,18 @@
CC=gcc
CFLAGS=-o osh -Wall
SRCS=history.c osh.c
.DEFAULT_GOAL := build
build:
$(CC) $(CFLAGS) -fsanitize=address -static-libasan $(SRCS)
release:
$(CC) $(CFLAGS) $(SRCS)
debug:
$(CC) $(CFLAGS) -fsanitize=address -static-libasan -ggdb $(SRCS)
gdb -q osh
clean:
rm -f osh

View File

@@ -0,0 +1,109 @@
#include "history.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#define HISTORY_FILENAME "osh_history"
#define W_MODE "w"
#define R_MODE "r"
#define A_MODE "a+"
int allocate_history(void) {
FILE *file;
if (access(HISTORY_FILENAME, F_OK) == -1) {
if ((file = fopen(HISTORY_FILENAME, W_MODE)) == NULL) {
perror("Error");
return -1;
}
fclose(file);
}
if ((file = fopen(HISTORY_FILENAME, R_MODE)) == NULL) {
perror("Error");
return -1;
}
int count = -1;
char line[MAXLEN];
while (!feof(file)) {
fgets(line, sizeof(line), file);
count++;
}
fclose(file);
return count;
}
history_entry_t *get_history(int history_count) {
FILE *file = fopen(HISTORY_FILENAME, R_MODE);
if (!file) {
perror("Error");
exit(EXIT_FAILURE);
}
int num = 0;
char line[MAXLEN];
const int offset = history_count - HISTORY_SIZE;
history_entry_t *history = calloc(HISTORY_SIZE, sizeof(history_entry_t));
while (fgets(line, sizeof(line), file)) {
if (num >= offset) {
history_entry_t entry;
entry.line_number = num + 1;
strcpy(entry.command, line);
history[num - offset] = entry;
}
num++;
}
fclose(file);
return history;
}
int add_history(int count, char *cmd) {
FILE *file = fopen(HISTORY_FILENAME, A_MODE);
if (!file) {
perror("Error");
exit(EXIT_FAILURE);
}
fprintf(file, "%s\n", cmd);
fclose(file);
return count + 1;
}
int last_command(char *last) {
FILE *file = fopen(HISTORY_FILENAME, A_MODE);
if (!file) {
perror("Error");
exit(EXIT_FAILURE);
}
char line[MAXLEN];
while (!feof(file)) {
fgets(line, sizeof(line), file);
}
fclose(file);
strcpy(last, line);
return 0;
}
int nth_command(char *s, int n) {
FILE *file = fopen(HISTORY_FILENAME, R_MODE);
if (!file) {
perror("Error");
exit(EXIT_FAILURE);
}
int num = 0;
char line[MAXLEN];
while (fgets(line, sizeof(line), file)) {
if ((num + 1) == n) {
strcpy(s, line);
return 0;
}
num++;
}
fclose(file);
return -1;
}

View File

@@ -0,0 +1,29 @@
#ifndef __HISTORY_H__
#define __HISTORY_H__
#define MAXLEN 80
#define HISTORY_SIZE 10
typedef struct __HISTORY_ENTRY__ {
int line_number;
char command[MAXLEN];
} history_entry_t;
// Create new history file if not exists,
// otherwise read last command count and return it
int allocate_history(void);
// Return last ten command in the history file
history_entry_t *get_history(int);
// Write a record in the history file
int add_history(int, char *);
// Read last command in the history file
int last_command(char *);
// Search nth command in the history file,
// if not found nth command return -1.
int nth_command(char *, int);
#endif // __HISTORY_H__

View File

@@ -0,0 +1,147 @@
#include "history.h"
#include <ctype.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
#include <unistd.h>
struct command_t {
char **args;
bool bg_flag;
};
struct command_t parse_command(char *);
int digits(char *);
int main(void) {
// Create and/or read history file
int history_count = allocate_history();
if (history_count < 0) {
perror("Error");
exit(EXIT_FAILURE);
}
int should_run = 1;
while (should_run) {
char raw[MAXLEN / 2 + 1];
printf("osh> ");
fflush(stdout);
fgets(raw, MAXLEN / 2 + 1, stdin);
// check if user press newline
if (strcmp(raw, "\n") == 0) {
continue;
}
// FIXME: handle ^D shortcut
if (strcmp(raw, "\000") == 0) {
exit(EXIT_SUCCESS);
}
char *c;
if (strcmp(raw, "!!\n") == 0) {
if (nth_command(raw, history_count) != -1) {
printf("%s\n", raw);
} else {
printf("History is empty\n");
continue;
}
} else if ((c = strchr(raw, '!')) != NULL) {
const int n = digits(c);
if (nth_command(raw, n) != -1) {
printf("%s\n", raw);
} else {
printf("Command not found in position %d\n", n);
continue;
}
}
// remove carriage return
raw[strlen(raw) - 1] = '\0';
// save command in the history file
history_count = add_history(history_count, raw);
struct command_t cmd = parse_command(raw);
if (strcmp(cmd.args[0], "exit") == 0) {
should_run = 0;
} else if (strcmp(cmd.args[0], "history") == 0) {
// print on the stdout last 10 commands in reverse way
history_entry_t *history = get_history(history_count);
for (int i = HISTORY_SIZE - 1; i >= 0; i--) {
history_entry_t entry = history[i];
if (entry.line_number != 0) {
printf("%5d\t%s", entry.line_number, entry.command);
}
}
free(history);
history = NULL;
} else {
pid_t pid = fork();
switch (pid) {
case -1:
perror("Fork\n");
exit(EXIT_FAILURE);
case 0:
should_run = 0;
if (execvp(cmd.args[0], cmd.args)) {
perror("Error");
}
break;
default:
if (!cmd.bg_flag) {
wait(NULL);
}
break;
}
}
free(cmd.args);
cmd.args = NULL;
}
return EXIT_SUCCESS;
}
struct command_t parse_command(char *raw) {
int i = 0;
bool bg_flag = false;
char **args = (char **)calloc(MAXLEN, sizeof(char *));
char *tok = strtok(raw, " ");
while (tok != NULL) {
if (strcmp(tok, "&") == 0) {
bg_flag = true;
} else {
args[i] = tok;
}
tok = strtok(NULL, " ");
i++;
}
struct command_t cmd = {.args = args, .bg_flag = bg_flag};
return cmd;
}
int digits(char *s) {
char *sub = malloc(strlen(s) * sizeof(s));
char c;
int i = 0, j = 0;
while ((c = s[i]) != '\n') {
if (isdigit(c)) {
sub[j++] = c;
}
i++;
}
int n = atoi(sub);
free(sub);
sub = NULL;
return n;
}

View File

@@ -0,0 +1,63 @@
#include "pid_manager.h"
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#define MIN_PID 300
#define MAX_PID 5000
#define SIZE (MAX_PID - MIN_PID + 1)
bool pids[SIZE];
int allocate_map(void) {
for (int i = 0; i < SIZE; i++) {
pids[i] = false;
}
return 1;
}
int allocate_pid(void) {
for (int i = 0; i < SIZE; i++) {
if (!pids[i]) {
pids[i] = true;
return i + MIN_PID;
}
}
return -1;
}
void release_pid(int pid) {
if (pid >= MIN_PID && pid <= MAX_PID) {
pids[pid - MIN_PID] = false;
}
}
int main(void) {
if (allocate_map() != 1) {
perror("Failed to initialize PID Manager\n");
exit(1);
}
for (int i = 0; i < SIZE; i++) {
printf("Allocate PID = %d\n", allocate_pid());
}
int last = allocate_pid();
printf("Attempt to allocate another PID, result %d\n", last);
if (last == -1) {
printf("Release a PID 4000\n");
release_pid(4000);
}
printf("Allocate another PID: %d\n", allocate_pid());
printf("Release PIDs 300 and 5000\n");
release_pid(300);
release_pid(5000);
printf("Allocate another PID: %d\n", allocate_pid());
printf("Allocate another PID: %d\n", allocate_pid());
return 0;
}

View File

@@ -0,0 +1,16 @@
#if !defined(__PID_MANGER_H__)
#define __PID_MANGER_H__
#define _GNU_SOURCE
// Crea e inizializza una struttura per rappresentare i pid;
// restituisce -1 in caso di insuccesso e 1 in caso di successo.
int allocate_map(void);
// Alloca e restituisce un pid, restituisce -1 se non è possibile
// assegnare un PID (tutti i pid sono in uso).
int allocate_pid(void);
// Rilascia un pid
void release_pid(int pid);
#endif

31
chp3_process/prod.c Normal file
View File

@@ -0,0 +1,31 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/shm.h>
#include <sys/mman.h>
#include <sys/stat.h>
int main()
{
const int SIZE = 4096;
const char *name = "OS";
const char *message_0 = "Hello";
const char *message_1 = "World!";
int shm_fd;
void *ptr;
shm_fd = shm_open(name, O_CREAT | O_RDWR, 0666);
ftruncate(shm_fd, SIZE);
ptr = mmap(0, SIZE, PROT_WRITE, MAP_SHARED, shm_fd, 0);
sprintf(ptr, "%s", message_0);
ptr += strlen(message_0);
sprintf(ptr, "%s", " ");
ptr += 1;
sprintf(ptr, "%s", message_1);
ptr += strlen(message_1);
return 0;
}

View File

@@ -0,0 +1,10 @@
CompileFlags:
Compiler: make
Add: [-Wall, -I/lib/modules/6.10.7-200.fc40.x86_64/build/include]
InlayHints:
BlockEnd: false
Designators: true
Enabled: true
ParameterNames: true
DeducedTypes: true
TypeNameLimit: 24

View File

@@ -0,0 +1,11 @@
obj-m += task-driver.o
all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
sign:
/usr/src/kernels/$(shell uname -r)/scripts/sign-file sha256 kernel-sign/private_key.priv kernel-sign/public_key.der task-driver.ko

View File

@@ -0,0 +1 @@
/run/media/fscotto/Work/Configurations/kernel-sign

View File

@@ -0,0 +1,21 @@
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/proc_fs.h> // Module metadata
#include <linux/uaccess.h>
MODULE_AUTHOR("Fabio Scotto di Santolo");
MODULE_DESCRIPTION("List all tasks on the Linux system");
MODULE_LICENSE("GPL"); // Custom init and exit methods
static int __init custom_init(void) {
printk(KERN_INFO "Hello world driver loaded.");
kmal return 0;
}
static void __exit custom_exit(void) {
printk(KERN_INFO "Goodbye my friend, I shall miss you dearly...");
}
module_init(custom_init);
module_exit(custom_exit);