diff options
| Commit | 609b7864d38fddb96617473fbc6aea16c64e8285 (patch) | |
|---|---|---|
| Author | 2024-01-20 15:54:05 +0200 | |
| Committer | 2024-01-20 15:54:05 +0200 | |
| Files | 8f203b247508a774b4cbde63da224a084ada8538 | |
| uni-projects-609b7864d38fddb96617473fbc6aea16c64e8285.tar uni-projects-609b7864d38fddb96617473fbc6aea16c64e8285.tar.gz uni-projects-609b7864d38fddb96617473fbc6aea16c64e8285.zip | ||
| Parent | 92478827872214a442c96891a607d0781cfa6dd8 (diff) | |
[SP/P10] Solved exercises
5 files changed, 157 insertions, 0 deletions
| -rw-r--r-- | Year 3/System programming/Practice10/ex1.c | 25 | ||||
| -rw-r--r-- | Year 3/System programming/Practice10/ex2-1.c | 21 | ||||
| -rw-r--r-- | Year 3/System programming/Practice10/ex2-2.c | 24 | ||||
| -rw-r--r-- | Year 3/System programming/Practice10/ex3.c | 38 | ||||
| -rw-r--r-- | Year 3/System programming/Practice10/ex4.c | 49 |
diff --git a/Year 3/System programming/Practice10/ex1.c b/Year 3/System programming/Practice10/ex1.c new file mode 100644 index 0000000..c04f7dd --- /dev/null +++ b/Year 3/System programming/Practice10/ex1.c @@ -0,0 +1,25 @@ +#include <string.h> +#include <sys/mman.h> +#include <sys/stat.h> +#include <sys/types.h> +#include <fcntl.h> +#include <unistd.h> + +int main() { + char msg[] = "hello world"; + + int shmd = shm_open("/shmemory", O_RDWR | O_CREAT | O_TRUNC, 0755); + ftruncate(shmd, strlen(msg)); + char* mem = mmap(NULL, strlen(msg) + 1, PROT_READ | PROT_WRITE, MAP_SHARED, shmd, 0); + + strncpy(mem, msg, strlen(msg) + 1); + + char buf[1024]; + strncpy(buf, mem, strlen(msg) + 1); + write(1, buf, strlen(buf) + 1); + + close(shmd); + shm_unlink("/shmemory"); + + return 0; +} diff --git a/Year 3/System programming/Practice10/ex2-1.c b/Year 3/System programming/Practice10/ex2-1.c new file mode 100644 index 0000000..db839c1 --- /dev/null +++ b/Year 3/System programming/Practice10/ex2-1.c @@ -0,0 +1,21 @@ +#include <string.h> +#include <sys/mman.h> +#include <sys/stat.h> +#include <sys/types.h> +#include <fcntl.h> +#include <unistd.h> + +int main() { + const char msg[] = "hello world"; + size_t msglen = strlen(msg); + + int shmd = shm_open("/shm", O_RDWR | O_CREAT | O_TRUNC, 0755); + ftruncate(shmd, msglen); + char* memory = mmap(NULL, msglen, PROT_WRITE, MAP_SHARED, shmd, 0); + + strncpy(memory, msg, msglen); + + munmap(memory, msglen); + close(shmd); + return 0; +} diff --git a/Year 3/System programming/Practice10/ex2-2.c b/Year 3/System programming/Practice10/ex2-2.c new file mode 100644 index 0000000..2043360 --- /dev/null +++ b/Year 3/System programming/Practice10/ex2-2.c @@ -0,0 +1,24 @@ +#include <string.h> +#include <sys/mman.h> +#include <sys/stat.h> +#include <sys/types.h> +#include <fcntl.h> +#include <unistd.h> + +int main() { + const char msg[] = "hello world"; + size_t msglen = strlen(msg) + 1; + + int shmd = shm_open("/shm", O_RDONLY, 0755); + ftruncate(shmd, msglen); + char* memory = mmap(NULL, msglen, PROT_READ, MAP_SHARED, shmd, 0); + + char buf[1024]; + strncpy(buf, memory, msglen); + write(1, buf, msglen); + + munmap(memory, msglen); + close(shmd); + shm_unlink("/shm"); + return 0; +} diff --git a/Year 3/System programming/Practice10/ex3.c b/Year 3/System programming/Practice10/ex3.c new file mode 100644 index 0000000..0580a38 --- /dev/null +++ b/Year 3/System programming/Practice10/ex3.c @@ -0,0 +1,38 @@ +#include <stdio.h> +#include <string.h> +#include <sys/mman.h> +#include <sys/stat.h> +#include <sys/types.h> +#include <fcntl.h> +#include <unistd.h> + +int main() { + int shmd = shm_open("/shm", O_RDWR | O_TRUNC | O_CREAT, 0755); + ftruncate(shmd, sizeof(int)); + int* memory = mmap(NULL, sizeof(int), PROT_READ | PROT_WRITE, MAP_SHARED, shmd, 0); + + *memory = 0; + + int fod = fork(); + if (fod == 0) { + while (*memory < 100) { + *memory += 1; + printf("%d %d\n", getpid(), *memory); + sleep(1); + } + } + else { + while (*memory < 100) { + sleep(2); + *memory += 1; + printf("%d %d\n", getpid(), *memory); + } + } + + munmap(memory, sizeof(int)); + close(shmd); + if (fod != 0) + shm_unlink("/shm"); + + return 0; +} diff --git a/Year 3/System programming/Practice10/ex4.c b/Year 3/System programming/Practice10/ex4.c new file mode 100644 index 0000000..c0505fb --- /dev/null +++ b/Year 3/System programming/Practice10/ex4.c @@ -0,0 +1,49 @@ +#include <sys/wait.h> +#include <stdlib.h> +#include <time.h> +#include <stdio.h> +#include <string.h> +#include <sys/mman.h> +#include <sys/stat.h> +#include <sys/types.h> +#include <fcntl.h> +#include <unistd.h> + +int main() { + int shmd = shm_open("/shm", O_RDWR | O_TRUNC | O_CREAT, 0755); + ftruncate(shmd, sizeof(int[20])); + int* memory = mmap(NULL, sizeof(int[20]), PROT_READ | PROT_WRITE, MAP_SHARED, shmd, 0); + + srand(time(NULL)); + for (int i = 0; i < 20; i++) { + memory[i] = rand() % 100; + } + + int fod = fork(); + if (fod == 0) { + for (int i = 0; i < 20; i++) { + for (int j = 0; j < 20; j++) { + if (memory[i] < memory[j]) { + int temp = memory[i]; + memory[i] = memory[j]; + memory[j] = temp; + } + } + } + } + else { + while (!waitpid(fod, NULL, WNOHANG)) { + for (int i = 0; i < 20; i++) { + printf("%d ", memory[i]); + } + printf("\n"); + } + } + + munmap(memory, sizeof(int[20])); + close(shmd); + if (fod != 0) + shm_unlink("/shm"); + + return 0; +} |
