Summary Files Commits Branches @commit @diff @stats
path: root / Year 3 / System programming / Practice11 / ex3-2.c
diff options
context:
space:
mode:
1 files changed, 29 insertions, 0 deletions
-rw-r--r--Year 3/System programming/Practice11/ex3-2.c29
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;
+}