0

This is my code: void 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");
 }

This is a code for NIM game. Now I want to return value from the method playerproc(which i m spawning using fork()) and get it's return vale in the method NIM manager. I am not able to get it. I am using NachOs.

1 Answers1

0

I think you should have a look at the Fork(...) source code in /threads/thread.cc. I always found answers in the source code when I was using NACHOS.

Fork(...) returns void. Unless you make the return value of your function a global variable, you shouldn't be able to access it. Fork(...) is allocating a new stack and adding a thread to run the function to the ready queue. So the scope of the return variable of your function is limited to that function.

Why exactly are you forking the threads for the playerproc() function? Multiple threads doesn't make that much sense here, since your main thread must wait for playerproc() to return anyway before continuing.

Francesca Nannizzi
  • 1,695
  • 13
  • 18