Summary Files Commits Branches @commit @diff @stats
diff options
context:
space:
mode:
Commit2dc3695bf5e288779741f95629ba6ec9e1cbee8c (patch)
Author
 Syndamia <kamen@syndamia.com>
2024-01-20 16:24:36 +0200
Committer
 Syndamia <kamen@syndamia.com>
2024-01-20 16:24:36 +0200
Filesbef3fceb45d912665d29b9970b037e8d45caa826
uni-projects-2dc3695bf5e288779741f95629ba6ec9e1cbee8c.tar
uni-projects-2dc3695bf5e288779741f95629ba6ec9e1cbee8c.tar.gz
uni-projects-2dc3695bf5e288779741f95629ba6ec9e1cbee8c.zip
Parent609b7864d38fddb96617473fbc6aea16c64e8285 (diff)
[SP/P11] Solved exercises
diff --git a/Year 3/System programming/Practice11/ex1.c b/Year 3/System programming/Practice11/ex1.c
new file mode 100644
index 0000000..4086ca7
--- /dev/null
+++ b/Year 3/System programming/Practice11/ex1.c
@@ -0,0 +1,17 @@
+#include <stdio.h>
+#include <semaphore.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+
+int main() {
+ sem_t* sem = sem_open("/sem", O_CREAT);
+
+ int sval = 0;
+ sem_getvalue(sem, &sval);
+ printf("%d\n", sval);
+
+ sem_close(sem);
+ sem_unlink("/sem");
+
+ return 0;
+}
diff --git a/Year 3/System programming/Practice11/ex2.c b/Year 3/System programming/Practice11/ex2.c
new file mode 100644
index 0000000..94ca590
--- /dev/null
+++ b/Year 3/System programming/Practice11/ex2.c
@@ -0,0 +1,30 @@
+#include <unistd.h>
+#include <stdio.h>
+#include <semaphore.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+
+int main() {
+ sem_t* sem = sem_open("/sem", O_CREAT, 0755, 0);
+
+ int fod = fork();
+ if (fod == 0) {
+ while (1) {
+ sem_wait(sem);
+ printf("world\n");
+ }
+ }
+ else {
+ while (1) {
+ printf("hello\n");
+ sem_post(sem);
+ sleep(1);
+ }
+ }
+
+ sem_close(sem);
+ if (fod != 0)
+ sem_unlink("/sem");
+
+ return 0;
+}
diff --git a/Year 3/System programming/Practice11/ex3-1.c b/Year 3/System programming/Practice11/ex3-1.c
new file mode 100644
index 0000000..68f26f6
--- /dev/null
+++ b/Year 3/System programming/Practice11/ex3-1.c
@@ -0,0 +1,30 @@
+#include <unistd.h>
+#include <stdio.h>
+#include <semaphore.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+
+int main() {
+ sem_t* sem1 = sem_open("/sem1", O_CREAT, 0755, 0);
+ sem_t* sem2 = sem_open("/sem2", O_CREAT, 0755, 0);
+
+ int fd = open("test.txt", O_WRONLY | O_CREAT, 0755);
+
+ while (1) {
+ sleep(1);
+
+ lseek(fd, 0, SEEK_END);
+ write(fd, "(1)", 3);
+ printf("written\n");
+
+ sem_post(sem2);
+ sem_wait(sem1);
+ }
+
+ close(fd);
+ sem_close(sem1);
+ sem_close(sem2);
+ sem_unlink("/sem1");
+
+ return 0;
+}
diff --git a/Year 3/System programming/Practice11/ex3-2.c b/Year 3/System programming/Practice11/ex3-2.c
new file mode 100644
index 0000000..6d19bda
--- /dev/null
+++ b/Year 3/System programming/Practice11/ex3-2.c
@@ -0,0 +1,29 @@
+#include <unistd.h>
+#include <stdio.h>
+#include <semaphore.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+
+int main() {
+ sem_t* sem1 = sem_open("/sem1", O_CREAT, 0755, 0);
+ sem_t* sem2 = sem_open("/sem2", O_CREAT, 0755, 0);
+
+ int fd = open("test.txt", O_WRONLY | O_CREAT, 0755);
+
+ while (1) {
+ sem_wait(sem2);
+
+ lseek(fd, 0, SEEK_END);
+ write(fd, "[2]", 3);
+ printf("written\n");
+
+ sem_post(sem1);
+ }
+
+ close(fd);
+ sem_close(sem1);
+ sem_close(sem2);
+ sem_unlink("/sem2");
+
+ return 0;
+}