From c6be556d3e21c1f456e44cbea0c4acb62dcbf23f Mon Sep 17 00:00:00 2001 From: mykola2312 <49044616+mykola2312@users.noreply.github.com> Date: Sat, 20 Jul 2024 12:47:48 +0300 Subject: [PATCH] make dummy target children and siblings (threads) --- src/dummy_target.cpp | 63 ++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 58 insertions(+), 5 deletions(-) diff --git a/src/dummy_target.cpp b/src/dummy_target.cpp index 5d95cd6..3e75621 100644 --- a/src/dummy_target.cpp +++ b/src/dummy_target.cpp @@ -1,16 +1,69 @@ #include +#include +#include #include extern char *program_invocation_name; extern char *program_invocation_short_name; +void status(const char* tag) +{ + printf("[%s] pid: %d\n", tag, getpid()); + printf("[%s] program_invocation_name: %s\n", tag, program_invocation_name); + printf("[%s] program_invocation_short_name: %s\n", tag, program_invocation_short_name); +} + +__attribute__((noreturn)) void slave1_job() +{ + status("slave1"); + + puts("[slave1] waiting for any key..."); + getc(stdin); + + _exit(0); +} + +__attribute__((noreturn)) void slave2_job() +{ + status("slave2"); + + puts("[slave2] will do something each second"); + while (true) + { + __asm__("nop"); + sleep(1); + } +} + +__attribute__((noreturn)) void* slave3_job(void*) +{ + status("slave3"); + + puts("[slave3] will do something each second but in a thread"); + while (true) + { + __asm__("nop"); + sleep(1); + } + + return NULL; +} + int main() { - printf("pid: %d\n", getpid()); - printf("program_invocation_name: %s\n", program_invocation_name); - printf("program_invocation_short_name: %s\n", program_invocation_short_name); + status("master"); - puts("waiting for any key..."); - getc(stdin); + pid_t slave1 = fork(); + if (!slave1) slave1_job(); + + pid_t slave2 = fork(); + if (!slave2) slave2_job(); + + pthread_t slave3; + pthread_create(&slave3, NULL, slave3_job, NULL); + + waitpid(slave1, NULL, 0); + waitpid(slave2, NULL, 0); + pthread_join(slave3, NULL); return 0; } \ No newline at end of file