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