void init(char* arg) { int i; q_init; gl_env.n = 0; gl_env.s = 0; gl_env.e = 0; gl_env.w = 0; for(i = 0; arg[i] != '\0'; i++){ switch (arg[i]) { case 'n': gl_env.n = 1; q_putCart('n'); break; case 's': gl_env.s = 1; q_putCart('s'); break; case 'e': gl_env.e = 1; q_putCart('e'); break; case 'w': gl_env.w = 1; q_putCart('w'); break; } } }
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(ðread,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; }