Exemplo n.º 1
0
// 1. Extract arguments
// 2. Initialize modules
// 3. Launch candy-factory threads
// 4. Launch kid threads
// 5. Wait for requested time
// 6. Stop candy-factory threads
// 7. Wait until no more candy
// 8. Stop kid threads
// 9. Print statistics
// 10. Cleanup any allocated memory
int main(int argc, char* argv[])
{	
	//1. Extract Arguments

	int factories = 0, kids = 0, seconds = 0;
	int* array[3] = {&factories, &kids, &seconds};

	if(invalid_args(argc, argv)) {
		printf(INVALID_ARG_ERR);
		exit(1);
	}

	for(int i=1; i<=ARG_COUNT; i++){
		sscanf(argv[i], "%d", array[i-1]);
	}
	
	
	//2. Initialize Modules
	srand(time(NULL));
	bbuff_init();
	stats_init(factories);


	//3. Launch candy-factory threads
	pthread_t* factory_thread_IDs = malloc(factories *(sizeof(pthread_t)));
	launch_threads(factories, factory_thread_IDs, (void*)factory_thread);
	

	//4. Launch kid threads
	pthread_t* kid_thread_IDs = malloc(kids *(sizeof(pthread_t)));
	launch_threads(kids, kid_thread_IDs, (void*)kid_thread);
	

	//5. Wait for requested time
	for(int i=0; i<seconds; i++) {
		sleep(1);
		printf("Time: %ds\n", i+1);
	}
	

	//6. Stop candy-factory threads
	
	stop_thread = true;
	
	for(int i=0; i<factories; i++) {
		pthread_join(factory_thread_IDs[i], NULL);
	}

	
	//7. Wait until no more candy
	while(!bbuff_is_empty()) {
		sleep(1);
	}
	
	
	//8. Stop kid threads
	for(int i=0; i<kids; i++) {
		pthread_cancel(kid_thread_IDs[i]);
		pthread_join(kid_thread_IDs[i], NULL);
	}


	//9. Print statistics
	stats_display();


	//10. Cleanup any allocated memory
	stats_cleanup();
	free(factory_thread_IDs);
	free(kid_thread_IDs);
	

	printf("Exiting program!\n");
	return 0;
}
Exemplo n.º 2
0
int main(int argc, char *argv[]) {

	bbuff_init();
	stats_init(atoi(argv[1]));
	
	int factory=0;
	int kid=0;
	int sec=0;
	
	//-----1. Extract arguments-----------------
	if (argc == 0){
		printf("Error: Invalid input\n");
		exit(1);
	}

	if (argc > 0 && argc <= 4){
		factory = atoi(argv[1]);
		kid = atoi(argv[2]);
		sec = atoi(argv[3]);
	}
	
	if (factory <= 0 || kid <= 0 || sec <= 0){
		printf("Error: Invalid input\n");
		exit(1);
	}
	
	 //-----2. Initialize modules-------------------

	
	//------3. Launch candy-factory threads-----------
	
	pthread_t factory_thread[factory]; //to hold factory threads
	int factory_num[factory]; //to pass factory number to factoryFunc
	
	for (int k=0; k<factory; k++){
		factory_num[k] = k;
	}
	
	for (int i = 0; i < factory; i++) {
		pthread_attr_t attr;
        pthread_attr_init(&attr);
		if(pthread_create(&factory_thread[i],&attr, factoryFunc, &factory_num[i])) {
			printf ("Create pthread error!\n");
			exit (1);
		}
	}
	
	pthread_t kid_thread[kid];
	
	//------4. Launch kid threads-------------------
	for (int i = 0; i < kid; i++) {
		pthread_attr_t attr;
        pthread_attr_init(&attr);
		if(pthread_create(&kid_thread[i], &attr, kidFunc, NULL)) {
			printf ("Create pthread error!\n");
			exit (1);
		}
	}
	
	//------5. Wait for requested time------------
	for (int p=1; p<sec; p++){
		sleep(1);
		printf("Time %ds\n", p);
	}
	
	//------6. Stop candy-factory threads-----------
	stop_thread = true;
	for (int x=0; x<factory; x++){
		pthread_join(factory_thread[x],  NULL);
	}
	
	//------7. Wait until no more candy--------------
	while (bbuff_is_empty() == false){
		printf("Waiting for all candy to be consumed\n");
		sleep(1);
	}

	//------8. Stop kid threads----------------------
	for (int i = 0; i < kid; i++) {
		pthread_cancel(kid_thread[i]);
		pthread_join(kid_thread[i], NULL);
	}

	//------9. Print statistics-----------------
	stats_display();
	
	//------10. Cleanup any allocated memory---------
	stats_cleanup();
	
	return 0;
}