forkxv6

In xv6, how can i make child process run first after fork()?


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.


Solution

  • Self ask, self answer, how funny!

      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;
    }
    
    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!