1

I've seen plenty of related questions and replys, however, no one can provide a good solution, could you help me?

Add: we may need to add a system call to determine whether to use this order or not.

1 Answers1

1

Self ask, self answer, how funny!

  • First, add a system call int fork_winner(int winner) which needs an integer argument. If winner == 1, child process runs first. If winner == 0, parent runs first as usual. To achieve this, you may need help from:

  1. how do i add a system call / utility in xv6

  2. how to pass a value into a system call function in xv6

The definition of int sys_fork_winner(void) is as follow which is in sysproc.c:

int 
sys_fork_winner(void)
{
  if(argint(0, &child_first) < 0) // fetch parameter
    return -1;  
  return 0;
}
  • Second, set a global variable, for example, child_first, to save winner's value.

  • Third, modify fork() in proc.c. if winner == 1, call yield() after child process being created. Otherwise, do nothing.

  • Finally, you may need a user program to test whether it works. The core code is follow:

void test(){
    int i = 0;   
    int ret = 0;
 for (i = 0; i < TOTAL_TEST_TRIALS; i++)
    {
        printf(1, "\nTrial %d: ", i);
        ret = fork();
        if (ret < 0)
        {
            printf(1, "fork() failed (%d)\n", ret);
            exit();
        }
        else if (ret == 0) // child
        {
            printf(1, " child! ");
            exit();
        }

        // parent
        printf(1, " parent! ");
        if (ret != wait())
        {
            printf(1, "wait() failed!\n");
        }
    }

    printf(1, "\n");
}

int
main(int argc, char *argv[])
{


    printf(1,"Fork test\nSet child as winner");

    fork_winner(1);  
    test();

    printf(1,"\nSet parent as winner");
    fork_winner(0);//the default
    test();


    exit();
}

This article can help you add a user program to xv6.

Thank you for your time!

  • Could you post your new system call. Moreover, I don't think you need to use a global variable, you could choose to call `yield()` in `fork_winner`. – Mathieu Apr 27 '20 at 07:39
  • In my case, system call is used to pass parameter which is needed by function `fork_winner()` in test program. The `fork_winner()`'s code has been put in which `child_first` is the global variable. – Changyu Zhou Apr 29 '20 at 08:34