summaryrefslogtreecommitdiff
path: root/test/signalbychild.c
diff options
context:
space:
mode:
authorfadhil riyanto <me@fadev.org>2024-09-28 08:32:03 +0700
committerfadhil riyanto <me@fadev.org>2024-09-28 08:32:03 +0700
commit1df6e9b87d30cd331a9b4b0abd7b655f933c7f93 (patch)
tree3ae5f273e50438115e339d6ec4b450cef2eda7cc /test/signalbychild.c
parent85964a41fd991a40f28c770f542c15432eafb715 (diff)
test fork()
Signed-off-by: fadhil riyanto <me@fadev.org>
Diffstat (limited to 'test/signalbychild.c')
-rw-r--r--test/signalbychild.c60
1 files changed, 60 insertions, 0 deletions
diff --git a/test/signalbychild.c b/test/signalbychild.c
new file mode 100644
index 0000000..462daf4
--- /dev/null
+++ b/test/signalbychild.c
@@ -0,0 +1,60 @@
+
+#include <signal.h>
+
+#include <stdio.h>
+#include <unistd.h>
+#include <sys/wait.h>
+
+int volatile g_signal_need_exit = 0;
+int saved_ppid = 0;
+
+void signal_notify(int signum)
+{
+ if (saved_ppid == getpid()) {
+
+ printf("parent got signal %d\n", signum);
+ }
+
+ g_signal_need_exit = 1;
+
+}
+
+static void install_signal(void)
+{
+ struct sigaction sa;
+
+ sa.sa_handler = signal_notify;
+
+ if (sigaction(SIGINT, &sa, NULL) == -1) {
+ perror("sigaction");
+ }
+
+}
+
+static void enter_eventloop(void)
+{
+ while(!g_signal_need_exit) {
+
+ }
+ printf("thread exit\n");
+
+ _exit(0);
+
+}
+
+int main(void)
+{
+ saved_ppid = getpid();
+ install_signal();
+
+ for(int i = 0; i < 5; i++) {
+ if (fork() == 0) {
+ enter_eventloop();
+
+ /* raise(SIGUSR1); */
+
+ }
+ }
+
+ while(wait(NULL) > 0);
+} \ No newline at end of file