Ejemplo n.º 1
0
int
sys_waitstat(void)
{
  int *turnaround;
  int *runningtime;
  int n;
  if(argptr(0, &turnaround,n) < 0)
    return -1;
  if(argptr(1, &runningtime,n) < 0)
    return -1;
  return waitstat(turnaround, runningtime);
}
Ejemplo n.º 2
0
/*Separate the function of wait stat test from main to make it look cleaner.*/
void waitstattest(int n)
{
  /*We need three counter variables, plus a pid, return value for our waitstat,
  * our running/turnaround times/averages, and our calculations (in doubles 
  * because of the 0.9 factor in the equation.*/
  int runningTotal, runningTurnaround, i, j, k, pid, wsReturn, running, turnaround;
  runningTotal = 0;
  runningTurnaround = 0;
  double childCalc, childSum;
  /*Fork 20 children.*/
  for(i = 1; i <= MAX_CHILD; i++)
  {
    pid = fork();
    /*check if pid fails·*/
    if(pid < 0)
    {
      /*Print to stderr.*/
      printf(2, "Fork has failed!\n");
      exit();
    }
    /*Start doing the calculations for the child.*/
    else if(pid == 0)
    {
      for(j = 1; j <= MAX_CALC; j++)
      {
	childCalc = 1.0/j;
	childSum = 0;
	for(k = 1; k <= ((21-i)*n); k++)
	  childSum += 1.0 / (k/0.9 + childCalc);
	printf(1, "Result of %dth sum of %dth child = ", j, i);
	conv2Doub(childSum);
	printf(1,"\n");
      }
      exit();
    }
    /*Let parent do calculations now once children is finished.*/
    else
    {
      wsReturn = waitstat(&running, &turnaround);
      runningTotal += running;
      runningTurnaround += turnaround;
      
      /*Check for our wait, aka waitstat is our new wait, and we return 0 once
       * all children have run. */
      if(wsReturn < 0)
      {
	printf(2, "Wait failed.\n");
	exit();
      }
      else
      {
	printf(1, "%d: Child = %d | Turnaround = %d |  Running = %d\n", pid, i, turnaround, running);
	
	childSum = 0;
	for(k = 1; k <= 5*n; k++)
	  childSum += 1.0/ ((k/0.9) + (1.0/i));
      }
      printf(1, "Result of %dth sum of the parent = ", i);
      conv2Doub(childSum);
      printf(1, "\n");
    }
    
    /*Calculate the average runtimes.*/
    if(pid > 0)
    {
      printf(1, "Average Running = ");
      conv2Doub((double) runningTotal / 20);
      printf(1, " | Average Turnaround = ");
      conv2Doub((double) runningTurnaround / 20);
      printf(1, "\n");
    }
  }
}
Ejemplo n.º 3
0
int main(int argc, char *argv[])
{
	int i, seed;

	int seed_pipe[NUM_PLAYERS][2];
	int score_pipe[NUM_PLAYERS][2];
	pid_t pids[NUM_PLAYERS];

        /* TODO: Use the following variables in the exec system call. Using the
	 * function sprintf and the arg1 variable you can pass the id parameter
	 * to the children
	 */

	char arg0[] = "./shooter";
	char arg1[10];
	char *args[] = {arg0, arg1, NULL};

	/* TODO: initialize the communication with the players */
	for (i = 0; i < NUM_PLAYERS; i++) {
	  if(pipe(seed_pipe[i]) == -1)
	    perror("problem i seed pipe!:/");

	  if(pipe(score_pipe[i]) == -1)
	    perror("problem i score pipen");
	}
	
	
	pid_t tmp_pid;

	for (i = 0; i < NUM_PLAYERS; i++) {
	  /* TODO: spawn the processes that simulate the players */  
	  tmp_pid = fork();

	  if(tmp_pid == 0) {
	    dup2(score_pipe[i][WRITE], STDOUT_FILENO);
	    dup2(seed_pipe[i][READ], STDIN_FILENO);
	    
	    //Close pipes that will not be used by child
	    close(score_pipe[i][READ]);
	    close(seed_pipe[i][WRITE]);

	    sprintf(args[1], "%d", i);
	    
	    //   shooter(i, seed_pipe[i][READ], score_pipe[i][WRITE]);

	    if(execv(arg0, args) == -1)
	      perror("Knas med execv!");

	    exit(EXIT_SUCCESS);
	    int n;
  
	  }

	  else if(tmp_pid == -1){
	    perror("fork failure");
	    exit(-1);
	  }

	  else{
	     pids[i] = tmp_pid;
	     
	     //Close used pipes
	     close(seed_pipe[i][READ]);
	     close(score_pipe[i][WRITE]);
	  }

	}

	seed = time(NULL);
	for (i = 0; i < NUM_PLAYERS; i++) {
		seed++;
		
		write(seed_pipe[i][WRITE], &seed, sizeof(int));
		close(seed_pipe[i][WRITE]);
		/* TODO: send the seed to the players */
	}
	
	
	/* TODO: get the dice results from the players, find the winner */
	int score = 0;
	int highest_score = 0;
	pid_t winner_pid;

	for (i = 0; i < NUM_PLAYERS; i++) {
	  read(score_pipe[i][READ], &score, sizeof(int));
	  close(score_pipe[i][READ]);

	  if(score > highest_score){
	    highest_score = score;
	    winner = i;
	    winner_pid = pids[i];
	  }

	}

	printf("master: player %d WINS\n", winner);

	/* TODO: signal the winner */
	kill(pids[winner], SIGUSR1);

	/* TODO: signal all players the end of game */
	for (i = 0; i < NUM_PLAYERS; i++) {
	  kill(pids[i], SIGUSR2);
	}

	printf("master: the game ends\n");

	/* TODO: cleanup resources and exit with success */
	int exit_value;
	for (i = 0; i < NUM_PLAYERS; i++) {
	  waitstat(pids[i], exit_value);
	}

	return 0;
}