Beispiel #1
0
void undo_command(){
  int n;
  n =0;
  while (strcmp(history[n],"undo\n") != 0){
    n++;
  }
  free(history[n]);
 int com,m;
  const char *canvas_file = "canvas.txt";
  FILE *fp;
 

 if ((fp = fopen(canvas_file, "w")) == NULL) {
    fprintf(stderr, "error: cannot open %s.\n", canvas_file);
 
  }
 init_canvas();
print_canvas(fp);
  
  for(m = 0; m < n-1; m ++){
    com = interpret_command(history[m]);
    if(strcmp(history[m],"undo\n") == 0)
      break;
 }
  }
void master()
{
	int number_processors;
	int count = X_RES; //the number of rows in the image
	int row = 0;
	MPI_Status status;
	MPI_Comm_size(MPI_COMM_WORLD, &number_processors);
	
	
	int canvas[X_RES][Y_RES];
	int color[Y_RES]; 	
	
	//sending tasks to all the processors
	for(int i=1; i<number_processors; i++)
	{
		//send row and slave_id
		MPI_Send(&row, 1, MPI_INT, i, DATA_TAG, MPI_COMM_WORLD);			
		count--;
		row++;
		printf("%d\n", count);
	}
	
	
	printf("%d\n", count);
	//when slaves are idle, then keep sending		
	do
	{
		//color contains the values for the row colors
		MPI_Recv(color, Y_RES, MPI_INT, MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &status);
		//row is row id in the canvas
		MPI_Recv(&row, 1, MPI_INT, MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &status);			
		//slave_id is the proccesor that is idle now			
		//count++;
		
		if(row<X_RES)
		{
			for(int i=0; i<Y_RES; i++)
			{
				canvas[row][i] = color[i];
			}

			MPI_Send(&row, 1, MPI_INT, status.MPI_SOURCE, DATA_TAG, MPI_COMM_WORLD);
			count--; //minus one row processed			
			row++;
			printf("%d\n", count);
		}
		else
		{
			for(int i=0; i<number_processors; i++)
			{
				MPI_Send(0, 0, MPI_INT, i, TERMINATE_TAG, MPI_COMM_WORLD);
				
			}
		}
	}while(count>0);
	
	print_canvas(canvas);
}
Beispiel #3
0
int main()
{
  int com, n, i;
  const char *canvas_file = "canvas.txt";
  FILE *fp;
  char buf[BUFSIZE];

  if ((fp = fopen(canvas_file, "w")) == NULL) {
    fprintf(stderr, "error: cannot open %s.\n", canvas_file);
    return 1;
  }

  init_canvas();
  print_canvas(fp);

  n = 0;
  while (1) {
    printf("%d > ", n);
    fgets(buf, BUFSIZE, stdin);

    history[n] = (char*)malloc(sizeof(char) * strlen(buf));
    strcpy(history[n], buf);
    if (++n >= HISTORY_SIZE) break;
			       
    com = interpret_command(buf);
    if (com == COM_QUIT) break;
    print_canvas(fp);
  }

  fclose(fp);

  if ((fp = fopen("history.txt", "w")) != NULL) {
    for (i = 0; i < n; i++) 
      fprintf(fp, "%s", history[i]);
    fclose(fp);
  }
}