Refactoring in chapters
This commit is contained in:
54
process_chp3/collatz.c
Normal file
54
process_chp3/collatz.c
Normal 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");
|
||||
}
|
||||
64
process_chp3/collatz_shm.c
Normal file
64
process_chp3/collatz_shm.c
Normal 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);
|
||||
}
|
||||
}
|
||||
60
process_chp3/collatz_shm2.c
Normal file
60
process_chp3/collatz_shm2.c
Normal 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
process_chp3/cons.c
Normal file
21
process_chp3/cons.c
Normal 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
process_chp3/ex1.c
Normal file
46
process_chp3/ex1.c
Normal 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
process_chp3/ex12.c
Normal file
39
process_chp3/ex12.c
Normal 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
process_chp3/ex13.c
Normal file
23
process_chp3/ex13.c
Normal 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
process_chp3/ex14.c
Normal file
27
process_chp3/ex14.c
Normal 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
process_chp3/ex17.c
Normal file
29
process_chp3/ex17.c
Normal 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
process_chp3/ex19.c
Normal file
18
process_chp3/ex19.c
Normal 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;
|
||||
}
|
||||
63
process_chp3/pid_manager.c
Normal file
63
process_chp3/pid_manager.c
Normal 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;
|
||||
}
|
||||
16
process_chp3/pid_manager.h
Normal file
16
process_chp3/pid_manager.h
Normal 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
process_chp3/prod.c
Normal file
31
process_chp3/prod.c
Normal 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;
|
||||
}
|
||||
Reference in New Issue
Block a user