Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I\'m having trouble implementing the threads/semaphores for this project. How wo

ID: 3862363 • Letter: I

Question

I'm having trouble implementing the threads/semaphores for this project. How would I be able to make sure that the wizard threads wait on the user interface thread while waiting for input?

I'm including both the wizard function, and the user interface function.

void *
wizard_func(void *wizard_descr)
{
struct cube* cube;
struct room *newroom;
struct room *oldroom;
struct wizard* self;
struct wizard* other;


self = (struct wizard*)wizard_descr;
assert(self);
cube = self->cube;
assert(cube);
  
/* Sets starting room */
oldroom = cube->rooms[self->x][self->y];
assert(oldroom);
  
/* Chooses the new room */
newroom = choose_room(self);

/* Infinite loop */
while (1)
{
  
/* Loops until he's able to get a hold on both the old and new rooms */
while (1)
   {
   printf("Wizard %c%d in room (%d,%d) wants to go to room (%d,%d) ",
       self->team, self->id, oldroom->x, oldroom->y, newroom->x, newroom->y);
  

   if (try_room(self, oldroom, newroom))
   {
   /* Waits a random amount of time */
   dostuff();
  
   /* Chooses the new room */
   newroom = choose_room(self);
  
   /* Goes back to the initial state and try again */
   continue;
   }
   else
   {
   break;
   }
   }
  
printf("Wizard %c%d in room (%d,%d) moves to room (%d,%d) ",
   self->team, self->id,
   oldroom->x, oldroom->y, newroom->x, newroom->y);

/* Fill in */
  
/* Self is active and has control over both rooms */
switch_rooms(self, oldroom, newroom);

other = find_opponent(self, newroom);

/* If there is not another wizard does nothing */
if (other == NULL)
   {
  
   printf("Wizard %c%d in room (%d,%d) finds nobody around ",
       self->team, self->id, newroom->x, newroom->y);
   /* Fill in */
   }
else
   {
   /* Other is from opposite team */
   if (other->team != self->team)
   {


   /* Checks if the opponent is active */
   if (other->status == 0)
       {
       printf("Wizard %c%d in room (%d,%d) finds active enemy ",
           self->team, self->id, newroom->x, newroom->y);

       fight_wizard(self, other, newroom);
       }
   else
       {
       printf("Wizard %c%d in room (%d,%d) finds enemy already frozen ",
           self->team, self->id, newroom->x, newroom->y);


       }
   }
   /* Other is from same team */
   else
   {
   /* Checks if the friend is frozen */
   if (other->status == 1)
       {
       free_wizard(self, other, newroom);
       }
   }

   /* Fill in */

   }

/* Thinks about what to do next */
dostuff();

oldroom = newroom;
newroom = choose_room(self);
}
  
return NULL;
}

int
interface(void *cube_ref)
{
struct cube* cube;
char *line;
char *command;
int i;

cube = (struct cube *)cube_ref;
assert(cube);

using_history();
while (1)
{
line = readline("cube> ");
if (line == NULL) continue;
if (strlen(line) == 0) continue;
  
add_history(line);

i = 0;
while (isspace(line[i])) i++;
  
command = &line[i];
if (!strcmp(command, "exit"))
   {
   return 0;
   }
else if (!strcmp(command, "show"))
   {
   print_cube(cube);
   }
else if (!strcmp(command, "start"))
   {
   if (cube->game_status == 1)
   {
   fprintf(stderr, "Game is over. Cannot be started again ");
   }
   else if (cube->game_status == 0)
   {
   fprintf(stderr, "Game is in progress. Cannot be started again ");
   }
   else
   {
   cube->game_status = 0;
  
   /* Start the game */

   /* Fill in */

   }
   }
else if (!strcmp(command, "stop"))
   {
   /* Stop the game */
   return 1;
   }
else
   {
   fprintf(stderr, "unknown command %s ", command);
   }

free(line);
}

return 0;
}

A. Description In this assignment you will have to develop a multi-threaded game simulator, called cube (inspired by the "Cube" movie. Original project specifications courtesy of Prof. Giovanni Vigna). The cube is a structure of n by n rooms. Each room has four doors, each located on one of the walls (North, South, East, West). The rooms are bent in space so that they are structured in a toroidal fashion (that is, the rooms are "wrapped around"). The doors of a room automatically lock when two people are in the room. The doors are unlocked otherwise. The cube is inhabited by wizards, which can move from room to room in no particular order. This means that a particular wizard may be sometimes faster and perform a move before another wizard can make a move, ie wizards do not take turns when moving A wizard can cast a spell on another wizard. If the spell is successful the victim wizard freezes. A wizard can a "wakeup" spell on a frozen wizard, which will bring the wizard back to The wizards are organized in two competing teams. A team wins when all of the wizards of the opposing team are frozen. Once this happens, you must terminate all threads. Check what threads are still running by running: $ps -eLf I grep Kyour-username on the same machine, but different terminal, before entering exit e.g. During a game run with 10 wizards, you will have 11 threads (10 wizards a thread for the cube) (youridac 4lab15 ~J ps eLf grep yourid 00:00:00 sshd: yourid [priv] root 1729 1729 1 05:1 00:00:00 sha: your idept your id 1731 1729 1731 1 05:1 your id 173 1731 173 1 05:12 pts/ 00:00:00 tcsh root 1810 84 1810 1 05 4 00:00:00 sshd: yourid [priv] 00:00:00 sshd your id pts your id 1812 1810 181 1 05 4 your id 1813 1812 1813 1 05 4 pts/3 00:00 00 tcsh 11 05:28 pts/2 00:00:00 cube our id 1943 1732 1943 11 05:29 pts/2 00:00:01 /cube your id 1943 1732 1950 24 11 05:29 pts/2 00:00:02 /cube your id 1943 1732 1951 33 11 05:29 pts/2 your id. 1943 1732 1952 20 00:00:01 cube 11 05:29 pts/2 00:00:02 /cube your id. 1943 1732 1953 29 11 05:29 pts/2 your id. 1943 1732 1955 23 00:00:01 cube 11 05:29 pts/2 00:00:01 /cube your id. 1943 1732 1956 23 11 05:29 pts/2 00:00:01 /cube your id 1943 1732 1958 20 1943 1732 1959 22 11 05:29 pts/2 00:00 01 /cube your id 11 05:29 pts/2 00:00:02 /cube your id. 1943 1732 1960 35 11 05:29 pts/2 your id. 1943 1732 1961 23 00:00:01 cube your id 1964 1813 1964 1 05 9 pts/3 00:00:00 ps -e Lf your id 1965 1813 1965 1 05 9 pts/3 00:00:00 grep yourid

Explanation / Answer

Hi,

As you have stated in your problem that you want your thread should be waiting on the user interface rather than waiting for user input.So, in order to solve your problem you should create a main thread and after that you include that sub thread into that main thread block so that you can avoid that problem and also you can assign the higher priority to the thread so that it can execute at first and for semaphore problem you can refer to the below example:

I hope it will solve your problem.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote