Beispiel #1
0
int main(int argc, char **argv)
{
  pthread_t nthread;
  pthread_t sthread;
  pthread_t ethread;
  pthread_t wthread;
  char *dirs;
  int i;

  setvbuf(stdout, NULL, _IOLBF,0);

  if (argc != 2)
    {
      printf("usage: %s <directions>\n",argv[0]);
      return 1;
    }

  dirs = argv[1];

  /*initialize four queues; one for each direction*/
  q_init();
  /*add carts to queues from command line args*/
  for (i=0; dirs[i] != '\0'; ++i)
    {
      if (dirs[i] == 'n')
	q_putCart('n');
      else if (dirs[i] == 's') 
	q_putCart('s');
      else if (dirs[i] == 'e')
	q_putCart('e');
      else if (dirs[i] == 'w')
	q_putCart('w');
      else
	printf("Invalid character %c supplied, ignoring...\n",dirs[i]);
    }
  
  printf("\nStarting queues:\n");
  q_print('n');
  q_print('w');
  q_print('s');
  q_print('e');
  printf("\n");

  /*init monitor*/
  monitor_init();

  /*start threads*/
  if (pthread_create(&nthread,NULL,&north,NULL))
    {
      printf("North thread creation failed.\n");
      return -1;
    }
  if (pthread_create(&sthread,NULL,&south,NULL))
    {
      printf("South thread creation failed.\n");
      return -1;
    }
  if (pthread_create(&ethread,NULL,&east,NULL))
    {
      printf("East thread creation failed.\n");
      return -1;
    }
  if (pthread_create(&wthread,NULL,&west,NULL))
    {
      printf("West thread creation failed.\n");
      return -1;
    }

  /*wait until all threads exit*/
  pthread_join(nthread,NULL);
  pthread_join(sthread,NULL);
  pthread_join(ethread,NULL);
  pthread_join(wthread,NULL);

  printf("\nEnding queues (should all be NULL):\n");
  q_print('n');
  q_print('w');
  q_print('s');
  q_print('e');
  printf("\n");

  /*free up memory and exit program*/
  q_shutdown();
  monitor_shutdown();

  return 0;
}
int main(int argc, char *argv[]) {
  char *directions;
  int i;
  pthread_t tid[4];
  char *arg;
  int rc;

/* get command line */
  if (argc != 2) {
    fprintf(stderr, "usage: %s <string: any length/combination of n,e,w,s>\n", argv[0]);
    exit(1);
  }
  directions = argv[1];

/* init other modules */
  q_init();
  monitor_init();
  pthread_barrier_init(&endBarrier, NULL, 4);

/* 1. place all carts on appropriate queue */
  for (i=0; i<strlen(directions); i++)
    q_putCart(directions[i]);
#if 1
  q_print(Q_NORTH);
  q_print(Q_SOUTH);
  q_print(Q_EAST);
  q_print(Q_WEST);
#endif

/* 2. create 4 threads, one for each direction */
  arg = malloc(sizeof(char));
  *arg = Q_NORTH;
  if ((rc = pthread_create(&tid[0], NULL, moveCart, (void *)arg)) != 0)
    fprintf(stderr, "thread create %c failed (%s)\n", *arg, strerror(rc)), exit(1);
  arg = malloc(sizeof(char));
  *arg = Q_SOUTH;
  if ((rc = pthread_create(&tid[1], NULL, moveCart, (void *)arg)) != 0)
    fprintf(stderr, "thread create %c failed (%s)\n", *arg, strerror(rc)), exit(1);
  arg = malloc(sizeof(char));
  *arg = Q_EAST;
  if ((rc = pthread_create(&tid[2], NULL, moveCart, (void *)arg)) != 0)
    fprintf(stderr, "thread create %c failed (%s)\n", *arg, strerror(rc)), exit(1);
  arg = malloc(sizeof(char));
  *arg = Q_WEST;
  if ((rc = pthread_create(&tid[3], NULL, moveCart, (void *)arg)) != 0)
    fprintf(stderr, "thread create %c failed (%s)\n", *arg, strerror(rc)), exit(1);

/* 3. join threads as they complete */
  if ((rc = pthread_join(tid[0], NULL)) != 0)
    fprintf(stderr, "thread join 0 failed (%s)\n", strerror(rc)), exit(1);
  if ((rc = pthread_join(tid[1], NULL)) != 0)
    fprintf(stderr, "thread join 1 failed (%s)\n", strerror(rc)), exit(1);
  if ((rc = pthread_join(tid[2], NULL)) != 0)
    fprintf(stderr, "thread join 2 failed (%s)\n", strerror(rc)), exit(1);
  if ((rc = pthread_join(tid[3], NULL)) != 0)
    fprintf(stderr, "thread join 3 failed (%s)\n", strerror(rc)), exit(1);

/* release resources */
  q_shutdown();
  monitor_shutdown();
  pthread_barrier_destroy(&endBarrier);

  return 0;
}
int main(int argc, char* argv[]){
 	char *carts;
 	int index;
 	pthread_t tids[4];
 	struct queue_info *queue_infos[4];

 	if(argc != 2){
 		fprintf(stderr, "wrong argument number");
 		exit(1);
 	}

 	carts = argv[1];
 	index = 0;
 	q_init();
 	pthread_barrier_init(&barrier, NULL, THREAD_NUM);
  monitor_init();

 	/*
 	 * check input string is valid
 	 */
 	if(strlen(carts) <= 0){
		fprintf(stderr, "input is empty");
		exit(1);
	}

	index = 0;
	while(index < strlen(carts)){
		if(isValidChar(carts[index]) == 0){
			fprintf(stderr, "input direction is invalid");
			exit(1);
		}
		q_putCart(carts[index]);
    thread_index = getIndexByDir(carts[index]);
		index++;
	}

  index = 0;
  while(index < THREAD_NUM){
    finishes[index] = 0;
    index++;
  }

	/*
	 * 0 -> North
	 * 1 -> West
	 * 2 -> South
	 * 3 -> East
	 */
	queue_infos[0] = malloc(sizeof(struct queue_info));
	queue_infos[1] = malloc(sizeof(struct queue_info));
	queue_infos[2] = malloc(sizeof(struct queue_info));
	queue_infos[3] = malloc(sizeof(struct queue_info));
	queue_infos[0]->dir = 'n';
	queue_infos[0]->thread_index = 0;
	queue_infos[1]->dir = 'w';
	queue_infos[1]->thread_index = 1;
	queue_infos[2]->dir = 's';
	queue_infos[2]->thread_index = 2;
	queue_infos[3]->dir = 'e';
	queue_infos[3]->thread_index = 3;

	/*
	 * Create four threads
	 */
	index = 0;
	while(index < 4){
		if(pthread_create(&tids[index], NULL, threadFunc, queue_infos[index]) != 0)
			perror("pthread create error"), exit(EXIT_FAILURE);
		index++;
	}

	index = 0;
	while(index < 4){
		pthread_join(tids[index], NULL);
		index++;
	}

	monitor_shutdown();
  q_shutdown();

	return 1;
}
Beispiel #4
0
void machine_shutdown(void)
{
    file_system_detach_disk_shutdown();

    machine_specific_shutdown();

    autostart_shutdown();

#ifdef HAS_JOYSTICK
    joystick_close();
#endif

    sound_close();

    printer_shutdown();
    gfxoutput_shutdown();

    fliplist_shutdown();
    file_system_shutdown();
    fsdevice_shutdown();

    tape_shutdown();

    traps_shutdown();

    kbdbuf_shutdown();
    keyboard_shutdown();

    monitor_shutdown();

    console_close_all();

    cmdline_shutdown();

    resources_shutdown();

    drive_shutdown();

    machine_maincpu_shutdown();

    video_shutdown();

    ui_shutdown();

    sysfile_shutdown();

    log_close_all();

    event_shutdown();

    network_shutdown();

    autostart_resources_shutdown();
    fsdevice_resources_shutdown();
    disk_image_resources_shutdown();
    machine_resources_shutdown();
    sysfile_resources_shutdown();
    zfile_shutdown();
    ui_resources_shutdown();
    log_resources_shutdown();
    fliplist_resources_shutdown();
    romset_resources_shutdown();
#ifdef HAVE_NETWORK
    monitor_network_resources_shutdown();
#endif
    archdep_shutdown();

    lib_debug_check();
}
Beispiel #5
0
int main(int argc, char** argv) {

    int rc;
    arg_t n,s,e,w;
    pthread_t north, south, east, west;

    if (argc == 2 && check_match(argv[1],"^[nsew]*$") > 0) {
        init(argv[1]);
        monitor_init();

        if (gl_env.n) {
            n.direction = 'n';
            rc = pthread_create(&north, NULL, cart, (void*)&n);
            if(rc){
                perror("Error creating North thread");
                cleanexit();
            }
        }

        if (gl_env.s) {
            s.direction = 's';
            rc = pthread_create(&south, NULL, cart, (void*)&s);
            if (rc) {
                perror("Error creating South thread");
                cleanexit();
            }
        }

        if (gl_env.e) {
            e.direction = 'e';
            rc = pthread_create(&east, NULL, cart, (void*)&e);
            if(rc){
                perror("Error creating East thread");
                cleanexit();
            }
        }

        if (gl_env.w) {
            w.direction = 'w';
            rc = pthread_create(&west, NULL, cart, (void*)&w);
            if (rc) {
                perror("Error creating West thread");
                cleanexit();
            }
        }

        if(gl_env.n)
            pthread_join(north, NULL);
        if(gl_env.s)
            pthread_join(south, NULL);
        if(gl_env.e)
            pthread_join(east, NULL);
        if(gl_env.w)
            pthread_join(west, NULL);

        monitor_shutdown();

    } else {
        /* invalid arguements entered */
        argerror();
    }


    cleanexit();
}