Summary Files Commits Branches @commit @diff @stats
path: root / Year 3 / System programming / Practice13
diff options
context:
space:
mode:
2 files changed, 61 insertions, 0 deletions
-rw-r--r--Year 3/System programming/Practice13/ex1.c25
-rw-r--r--Year 3/System programming/Practice13/ex2.c36
diff --git a/Year 3/System programming/Practice13/ex1.c b/Year 3/System programming/Practice13/ex1.c
new file mode 100644
index 0000000..4ea65cb
--- /dev/null
+++ b/Year 3/System programming/Practice13/ex1.c
@@ -0,0 +1,25 @@
+#include <signal.h>
+#include <stdio.h>
+#include <unistd.h>
+
+void handler(int a) {}
+
+int main() {
+ signal(SIGUSR1, &handler);
+
+ int fpid = fork();
+ if (fpid == 0) {
+ while (1) {
+ pause();
+ printf("child\n");
+ }
+ }
+ else {
+ while (1) {
+ printf("parent\n");
+ kill(fpid, SIGUSR1);
+ sleep(1);
+ }
+ }
+ return 0;
+}
diff --git a/Year 3/System programming/Practice13/ex2.c b/Year 3/System programming/Practice13/ex2.c
new file mode 100644
index 0000000..029fcfa
--- /dev/null
+++ b/Year 3/System programming/Practice13/ex2.c
@@ -0,0 +1,36 @@
+#include <fcntl.h>
+#include <sys/stat.h>
+#include <mqueue.h>
+#include <signal.h>
+#include <stdio.h>
+#include <unistd.h>
+
+int main() {
+ struct mq_attr mqattr = {
+ .mq_maxmsg = 1,
+ .mq_msgsize = 1,
+ };
+ mqd_t mq = mq_open("/mq", O_RDWR | O_CREAT, 0755, &mqattr);
+
+ int fpid = fork();
+ if (fpid == 0) {
+ char tmp[1];
+ while (1) {
+ mq_receive(mq, tmp, 1, NULL);
+ printf("child\n");
+ }
+ }
+ else {
+ while (1) {
+ printf("parent\n");
+ mq_send(mq, "", 1, 0);
+ sleep(1);
+ }
+ }
+
+ mq_close(mq);
+ if (fpid != 0)
+ mq_unlink("/mq");
+
+ return 0;
+}