aboutsummaryrefslogtreecommitdiff
path: root/semestr-5/so/lista2/signals.c
diff options
context:
space:
mode:
Diffstat (limited to 'semestr-5/so/lista2/signals.c')
-rw-r--r--semestr-5/so/lista2/signals.c30
1 files changed, 30 insertions, 0 deletions
diff --git a/semestr-5/so/lista2/signals.c b/semestr-5/so/lista2/signals.c
new file mode 100644
index 0000000..a7593bc
--- /dev/null
+++ b/semestr-5/so/lista2/signals.c
@@ -0,0 +1,30 @@
+#include <signal.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <string.h>
+#include <sys/wait.h>
+#include <stdlib.h>
+#include <errno.h>
+
+void sig_handler(int signum) {
+ char text[100];
+ sprintf(text, "[%d] Hello, I'm a signal handler :)\n", getpid());
+ write(STDOUT_FILENO, text, strlen(text));
+}
+
+int main() {
+ signal(SIGUSR1, sig_handler);
+ printf("Parent: [%d]\n", getpid());
+ fflush(stdout);
+ if (fork() == 0) {
+ printf("Child: [%d]\n", getpid());
+ // while(1) {}
+ if (execl("./echo-my", "echo-my", NULL) < 0) {
+ fprintf(stderr, "Exec error: %s\n", strerror(errno));
+ exit(0);
+ }
+ }
+
+ int result;
+ wait(&result);
+}