【问题标题】:How to get return value using Fork() in NachOs?如何在 NachOs 中使用 Fork() 获取返回值?
【发布时间】:2013-10-26 20:01:50
【问题描述】:

这是我的代码: 无效playerproc(int player) {

         switch(player)
         {
                 case 1:
                         move=(numSticks+4)%5;
                         return move;
                 break;
                 case 2:
                         srand(5);
                         move=(rand()%4)+1;
                         return move;
         }
 }

 void nimmanager(int numStick)
 {
         numSticks=numStick;
         int player;
         Thread *t=new Thread("Players");
         while(numSticks!=0)
         {
                 player=1;
                 int move=t->Fork(playerproc,player);
                 numSticks-=move;
                 printf("Name of the player: %d\nNumber of sticks taken:%d\nNumber of sticks left=%d",player,move,numSticks);
                 player=2;
                 int move=t->Fork(playerproc,player);
                 numSticks-=move;
                 printf("Name of the player: %d\nNumber of sticks taken:%d\nNumber of sticks left=%d",player,move,numSticks);

         }
         printf("Winner is player 1");
 }

这是 NIM 游戏的代码。现在我想从方法 playerproc(我使用 fork() 生成)返回值,并在方法 NIM 管理器中获取它的返回值。我无法得到它。我正在使用 NachOs。

【问题讨论】:

    标签: operating-system kernel nachos


    【解决方案1】:

    我认为你应该看看 /threads/thread.cc 中的 Fork(...) 源代码。我在使用 NACHOS 时总是在源代码中找到答案。

    Fork(...) 返回无效。除非您将函数的返回值设为全局变量,否则您应该无法访问它。 Fork(...) 正在分配一个新堆栈并添加一个线程来运行该函数到就绪队列。因此,函数的返回变量的范围仅限于该函数。

    你究竟为什么要为 playerproc() 函数分叉线程?多线程在这里没有多大意义,因为您的主线程必须等待 playerproc() 无论如何返回才能继续。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-06
      • 2023-03-31
      • 2018-02-09
      • 1970-01-01
      • 2023-02-09
      • 2018-06-15
      • 2023-03-18
      • 1970-01-01
      相关资源
      最近更新 更多