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;
}
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.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.