From c1ce5075cfdce8702b0d039a3c4eb1574ac3ebd9 Mon Sep 17 00:00:00 2001 From: FKHals <5229803-FKHals@users.noreply.gitlab.com> Date: Wed, 27 Sep 2023 17:06:41 +0200 Subject: [PATCH] Make test program multi-threaded --- kernel/exit.c | 6 ++- kernel/fork.c | 7 +++- pb_utils/pb_submitter/test_prog.c | 63 ++++++++++++++++++++++--------- 3 files changed, 54 insertions(+), 22 deletions(-) diff --git a/kernel/exit.c b/kernel/exit.c index 830ac3bcc9cd..97470434dad1 100644 --- a/kernel/exit.c +++ b/kernel/exit.c @@ -768,9 +768,11 @@ void __noreturn do_exit(long code) int group_dead; // call the readout before the process is terminated - if (strcmp(tsk->real_parent->comm, "bash") == 0) { + if (strcmp(tsk->real_parent->comm, "bash") == 0 + || strcmp(tsk->real_parent->real_parent->comm, "bash") == 0 + || strcmp(tsk->real_parent->real_parent->real_parent->comm, "bash") == 0) { stop_counting(); - printk(KERN_EMERG "EXIT: %u, CMD: '%s', PTR: %llu\n", tsk->pid, tsk->comm, (u64)tsk); + printk(KERN_EMERG "EXIT: %u, CMD: '%s', PARENT-CMD: '%s', PTR: %llu\n", tsk->pid, tsk->comm, tsk->real_parent->comm, (u64)tsk); } TASKS_RCU(int tasks_rcu_i); diff --git a/kernel/fork.c b/kernel/fork.c index fd4167b88d3b..2c2aab1bbb6a 100644 --- a/kernel/fork.c +++ b/kernel/fork.c @@ -2072,10 +2072,13 @@ long _do_fork(unsigned long clone_flags, } put_pid(pid); - printk(KERN_EMERG "FORKED!!!!: %u\n", p->pid); + printk(KERN_EMERG "FORKED!!!!: %u, Parent: %s, Super-Parent:%s\n", + p->pid, p->comm, p->real_parent->real_parent->comm); //FIXME At this point p->comm is not up to date but shows the command of the parent! // (This may not be a problem since the name of the forked processes are not needed?) - if (strcmp(p->comm, "bash") == 0) { + if (strcmp(p->comm, "bash") == 0 + || strcmp(p->real_parent->comm, "bash") == 0 + || strcmp(p->real_parent->real_parent->comm, "bash") == 0) { start_counting(p); } } else { diff --git a/pb_utils/pb_submitter/test_prog.c b/pb_utils/pb_submitter/test_prog.c index 98e6bece6dc9..5a6e77fbdb28 100644 --- a/pb_utils/pb_submitter/test_prog.c +++ b/pb_utils/pb_submitter/test_prog.c @@ -1,27 +1,54 @@ #include <stdio.h> #include <unistd.h> +#include <sys/prctl.h> int main(void) { - int i; - int j; - - // Make sure program is not finished before pb scheduler takes control - sleep(1); - - // this outer for loop is around 8 instructions per iteration - for (i = 0; i < 100; i++) { - // this for loop results in 4 cpu instructions per iteration - // nop, add, compare, jmp - for (j = 0; j < 100000; j++){ - __asm__ volatile("nop"); - } - - // check if program runs && syscall to switch tasks - printf("run #%d completed\n", i); - usleep(1); + int i, j; + int status = 0; + pid_t wpid; + + // Creating first child + pid_t n1 = fork(); + + // Creating second child. First child + // also executes this line and creates + // grandchild. + pid_t n2 = fork(); + + // this for loop results in 4 cpu instructions per iteration + // nop, add, compare, jmp + for (j = 0; j < 1000000; j++){ + __asm__ volatile("nop"); } - printf("main end\n"); + // check if program runs && syscall to switch tasks + printf("run completed\n"); + usleep(1); + + if (n1 > 0 && n2 > 0) { + // barrier: let the father wait for all the child processes + while ((wpid = wait(&status)) > 0) {}; + + fprintf(stderr, "This is the parent %d (%d, %d)\n", getpid(), n1, n2); + + // syscall to switch tasks + usleep(1); + } + else if (n1 == 0 && n2 > 0) + { + // set a different process name (comm) for the child to differentiate it from the parent + prctl(PR_SET_NAME, "test_child_1"); + fprintf(stderr, "This is the first child %d (%d, %d)\n", getpid(), n1, n2); + } + else if (n1 > 0 && n2 == 0) + { + prctl(PR_SET_NAME, "test_child_2"); + fprintf(stderr, "This is the second child %d (%d, %d)\n", getpid(), n1, n2); + } + else { + prctl(PR_SET_NAME, "test_child_3"); + fprintf(stderr, "This is the third child %d (%d, %d)\n", getpid(), n1, n2); + } return 0; } -- GitLab