shellcmd xsh_prodcons(int nargs, char *args[])
   {
      //Argument verifications and validations
	if(nargs > 2){
		printf("Too many arguments.\nType prodcons --help for details.\n");
		return 0;
	}

	//initialize count to default value
	int count = MAX_COUNT;

	/*if there are command line args check if user has asked for help
	*if not check if user has provided valid input ((int) count and greater than 0)
	*else terminate execution 
	*/

	if (nargs == 2) {

		if(strncmp(args[1], "--help", 7) == 0){
			printf("Description:\n");
			printf("\t--help\t display this help and exit\n");
			printf("\t-f \t execute futures\n");
			printf("\t\t by default it will execute producer-consumer.\n");
			printf("Arguments in case of producer-consumer:\n");
			printf("\tmax value of shared variable (integer){default value is 2000}\n");
			return 0;
		}
			if(strncmp(args[1], "-n", 3) == 0){
			
			printf("Inside N Args");
			f_exclusive = future_alloc(FUTURE_EXCLUSIVE);
			resume( create(nw_cons, 1024, 20, "fcons1", 1, f_exclusive) );
			resume( create(nw_prods, 1024, 20, "fprod1", 1, f_exclusive) );
			return 0;		

}
			else if(strncmp(args[1], "-f", 3) == 0){

			f_exclusive = future_alloc(FUTURE_EXCLUSIVE);
			f_shared = future_alloc(FUTURE_SHARED);
			f_queue = future_alloc(FUTURE_QUEUE);
					 
			// Test FUTURE_EXCLUSIVE
			resume( create(future_cons, 1024, 20, "fcons1", 1, f_exclusive) );
			resume( create(future_prod, 1024, 20, "fprod1", 1, f_exclusive) );
			sleep(1);
			// Test FUTURE_SHARED
			resume( create(future_cons, 1024, 20, "fcons2", 1, f_shared) );
			resume( create(future_cons, 1024, 20, "fcons3", 1, f_shared) );
			resume( create(future_cons, 1024, 20, "fcons4", 1, f_shared) ); 
			resume( create(future_cons, 1024, 20, "fcons5", 1, f_shared) );
			resume( create(future_prod, 1024, 20, "fprod2", 1, f_shared) );
			sleep(1);
			// Test FUTURE_QUEUE
			resume( create(future_cons, 1024, 20, "fcons6", 1, f_queue) );
			resume( create(future_cons, 1024, 20, "fcons7", 1, f_queue) );
			resume( create(future_cons, 1024, 20, "fcons7", 1, f_queue) );
			resume( create(future_cons, 1024, 20, "fcons7", 1, f_queue) );
			resume( create(future_prod, 1024, 20, "fprod3", 1, f_queue) );
			resume( create(future_prod, 1024, 20, "fprod4", 1, f_queue) );
			resume( create(future_prod, 1024, 20, "fprod5", 1, f_queue) );
			resume( create(future_prod, 1024, 20, "fprod6", 1, f_queue) );
			
			
			return 0;		

		}else{

			//check args[1](integer) if present assign value to count
			if(!isNumeric(args[1])){
				printf("Invalid argument.\nType prodcons --help for details.\n");
				return 0; 
			}
			count = (atoi)(args[1]);

			//if command line count is 0 terminate. 
			if(count == 0){
				printf("Nothing to produce.\n");
				return 0;	
			}			
		}
	}

	n=0;	
	
	/*Initialise semaphores*/
	produced = semcreate(0);
	consumed = semcreate(1);            

	//create the process produer and consumer and put them in ready queue.
	//Look at the definations of function create and resume in exinu/system folder for reference.      
	resume( create(producer, 1024, 20, "producer", 1, count) );
	resume( create(consumer, 1024, 20, "consumer", 1, count) );

	return 0;
   }
Exemplo n.º 2
0
shellcmd xsh_prodcons(int nargs, char *args[])
{
    //Argument verifications and validations
	

    //This code block is for HW4
/*    if(nargs == 2 && strcmp(args[1], "-f") == 0)
    {
        future *f1, *f2, *f3;
        f1 = future_alloc(FUTURE_EXCLUSIVE);
        f2 = future_alloc(FUTURE_EXCLUSIVE);
        f3 = future_alloc(FUTURE_EXCLUSIVE);
        resume( create(future_cons, 1024, 20, "fcons1", 1, f1) );
        resume( create(future_prod, 1024, 20, "fprod1", 1, f1) );
        resume( create(future_cons, 1024, 20, "fcons2", 1, f2) );
        resume( create(future_prod, 1024, 20, "fprod2", 1, f2) );
        resume( create(future_cons, 1024, 20, "fcons3", 1, f3) );
        resume( create(future_prod, 1024, 20, "fprod3", 1, f3) );
        return OK;
    }
*/
  
    //This code block is for HW5

    if(nargs == 2 && strcmp(args[1], "-f") == 0)
    {
        future *f_exclusive, *f_shared, *f_queue;
     
        f_exclusive = future_alloc(FUTURE_EXCLUSIVE);
        f_shared = future_alloc(FUTURE_SHARED);
        f_queue = future_alloc(FUTURE_QUEUE);
    
        //TEST FUTURE_EXCLUSIVE
        resume( create(future_cons, 1024, 20, "fcons1", 1, f_exclusive) );
        resume( create(future_prod, 1024, 20, "fprod1", 1, f_exclusive) );

        //TEST FUTURE_SHARED
        resume( create(future_cons, 1024, 20, "fcons2", 1, f_shared) );
        resume( create(future_cons, 1024, 20, "fcons3", 1, f_shared) );
        resume( create(future_cons, 1024, 20, "fcons4", 1, f_shared) ); 
        resume( create(future_cons, 1024, 20, "fcons5", 1, f_shared) );
        resume( create(future_prod, 1024, 20, "fprod2", 1, f_shared) );

        //TEST FUTURE_QUEUE
        resume( create(future_cons, 1024, 20, "fcons6", 1, f_queue) );
        resume( create(future_cons, 1024, 20, "fcons7", 1, f_queue) );
        resume( create(future_cons, 1024, 20, "fcons7", 1, f_queue) );
        resume( create(future_cons, 1024, 20, "fcons7", 1, f_queue) );
        resume( create(future_prod, 1024, 20, "fprod3", 1, f_queue) );
        resume( create(future_prod, 1024, 20, "fprod4", 1, f_queue) );
        resume( create(future_prod, 1024, 20, "fprod5", 1, f_queue) );
        resume( create(future_prod, 1024, 20, "fprod6", 1, f_queue) );
	return OK;
    }


    /*Output help, if '--help' argument was supplied */
    if (nargs == 2 && strcmp(args[1], "--help") == 0)
    {
        printf("Usage: %s\n\n", args[0]);
        printf("Description:\n");
        printf("\tCrete 2 processes which can exchange data using global variables\n");
        printf("Options:\n");
        printf("\t--help\tdisplay this help and exit\n");
	printf("\tInt(Optional)\tInt is an Integer which is a global variable\n");
        return OK;
    }

    /* Check for correct number of arguments */
    if (nargs > 2)
    {
        fprintf(stderr, "%s: too many arguments\n", args[0]);
        fprintf(stderr, "Try '%s --help' for more information\n",
                args[0]);
        return SYSERR;
    }
    
    //check args[1] if present assign value to count
    if (nargs == 2 && !number_only(args[1]))
    {
        fprintf(stderr, "%s: is not a valid positive number\n", args[1]);
        fprintf(stderr, "Try '%s --help' for more information\n",
                args[0]);
        return SYSERR;

    }
  
    int count;

    if (nargs < 2)
        count = 2000; //local variable to hold count	
    else
	count = atoi(args[1]);

    //printf("The value of args[1] is %d\n", count);


    /*Initialize semaphores*/
    consumed = semcreate(1);
    produced = semcreate(0);
  
    //create the process producer and consumer and put them in ready queue.
    //Look at the definition of function create and resume in exinus/system folder for reference
   
    resume( create(producer, 1024, 20, "producer", 1, count));
    resume( create(consumer, 1024, 20, "consumer", 1, count));
}
Exemplo n.º 3
0
shellcmd xsh_prodcons(int nargs, char *args[])
{
    n = 0;
    int count = 2000;
    bool use_futures = FALSE;

    if (nargs > 3)
    {
        printf(
                "ERROR: Wrong number of arguments given. Expected 2, 1 or 0, found %d.\n"
                "USAGE: prodcons COUNT\n", nargs - 1);
        return 1;
    }

    use_futures = search_dash_f(args);
    count = search_count_arg(args, count);

    printf("use_futures: %d\n", use_futures);
    printf("count: %d\n", count);

    if (use_futures == FALSE)
    {
        /* run as before */
        consumed = semcreate(0);
        produced = semcreate(1);

        tid_typ producer_th = create(producer, 1024, 20, "producer", 1, count);
        tid_typ consumer_th = create(consumer, 1024, 20, "consumer", 1, count);

        resume(consumer_th);
        resume(producer_th);
    }
    else
    {
        /* run program that uses futures */
        future *f1, *f2, *f3;

        f1 = future_alloc(FUTURE_EXCLUSIVE);
        f2 = future_alloc(FUTURE_EXCLUSIVE);
        f3 = future_alloc(FUTURE_EXCLUSIVE);

        /* semaphore for printing */
        semaphore print_sem = semcreate(1);

        /* semaphores to wait on thrads */
        semaphore running = semcreate(0);

        future *f_exclusive, *f_shared, *f_queue;

        f_exclusive = future_alloc(FUTURE_EXCLUSIVE);
        f_shared = future_alloc(FUTURE_SHARED);
        f_queue = future_alloc(FUTURE_QUEUE);
        int i;

        printf("\ntesting FUTURE_EXCLUSIVE\n");
        resume(create(future_cons, 1024, 20, "fcons1", 3, f_exclusive, print_sem, running));
        resume(create(future_prod, 1024, 20, "fprod1", 3, f_exclusive, print_sem, running));
        wait(running); wait(running);

        printf("\ntesting FUTURE_SHARED\n");
        resume(create(future_cons, 1024, 20, "fcons2", 3, f_shared, print_sem, running));
        resume(create(future_cons, 1024, 20, "fcons3", 3, f_shared, print_sem, running));
        resume(create(future_cons, 1024, 20, "fcons4", 3, f_shared, print_sem, running));
        resume(create(future_cons, 1024, 20, "fcons5", 3, f_shared, print_sem, running));
        resume(create(future_prod, 1024, 20, "fprod2", 3, f_shared, print_sem, running));
        for (i = 0; i < 5; ++i) wait(running);

        printf("\ntesting FUTURE_QUEUE\n");
        resume(create(future_cons, 1024, 20, "fcons6", 3, f_queue, print_sem, running));
        resume(create(future_cons, 1024, 20, "fcons7", 3, f_queue, print_sem, running));
        resume(create(future_cons, 1024, 20, "fcons7", 3, f_queue, print_sem, running));
        resume(create(future_cons, 1024, 20, "fcons7", 3, f_queue, print_sem, running));
        resume(create(future_prod, 1024, 20, "fprod3", 3, f_queue, print_sem, running));
        resume(create(future_prod, 1024, 20, "fprod4", 3, f_queue, print_sem, running));
        resume(create(future_prod, 1024, 20, "fprod5", 3, f_queue, print_sem, running));
        resume(create(future_prod, 1024, 20, "fprod6", 3, f_queue, print_sem, running));
        for (i = 0; i < 8; ++i) wait(running);

        printf("\ntesting FUTURE_QUEUE (in different order)\n");
        resume(create(future_prod, 1024, 20, "fprod4", 3, f_queue, print_sem, running));
        resume(create(future_cons, 1024, 20, "fcons6", 3, f_queue, print_sem, running));
        resume(create(future_prod, 1024, 20, "fprod3", 3, f_queue, print_sem, running));
        resume(create(future_cons, 1024, 20, "fcons7", 3, f_queue, print_sem, running));
        resume(create(future_cons, 1024, 20, "fcons7", 3, f_queue, print_sem, running));
        resume(create(future_prod, 1024, 20, "fprod5", 3, f_queue, print_sem, running));
        resume(create(future_prod, 1024, 20, "fprod6", 3, f_queue, print_sem, running));
        resume(create(future_cons, 1024, 20, "fcons7", 3, f_queue, print_sem, running));
        for (i = 0; i < 8; ++i) wait(running);

        printf("\ntesting FUTURE_QUEUE (in different order)\n");
        resume(create(future_cons, 1024, 20, "fcons7", 3, f_queue, print_sem, running));
        resume(create(future_prod, 1024, 20, "fprod3", 3, f_queue, print_sem, running));
        resume(create(future_cons, 1024, 20, "fcons7", 3, f_queue, print_sem, running));
        resume(create(future_prod, 1024, 20, "fprod5", 3, f_queue, print_sem, running));
        resume(create(future_prod, 1024, 20, "fprod6", 3, f_queue, print_sem, running));
        resume(create(future_cons, 1024, 20, "fcons6", 3, f_queue, print_sem, running));
        resume(create(future_cons, 1024, 20, "fcons7", 3, f_queue, print_sem, running));
        resume(create(future_prod, 1024, 20, "fprod4", 3, f_queue, print_sem, running));
        for (i = 0; i < 8; ++i) wait(running);

        /* threads are done, free the semaphores and futures */
        semfree(print_sem);
        semfree(running);
        future_free(f1);
        future_free(f2);
        future_free(f3);
    }

    return 0;
}
/*------------------------------------------------------------------------
 * xsh_prodcons - Producer Consumer Problem
 *------------------------------------------------------------------------
 */
shellcmd xsh_prodcons(int nargs, char *args[])
{
	/* Output info for '--help' argument */

	if (nargs == 2 && strncmp(args[1], "--help", 7) == 0) {
		printf("Usage: %s <integer> \n\n", args[0]);
		printf("Description:\n");
		printf("\tProducer Consumer Problem\n");
		printf("\t--help\tdisplays this help and exit\n");
		return SYSERR;
	}
	
	if(nargs >2){
	/* throw error saying too many arguements */
		fprintf(stderr, "%s: too many arguements\n", args[0]);
		fprintf(stderr, "Try '%s --help' for more information\n",
			args[0]);
		return SYSERR;
	}
	

	if (nargs == 2 && strncmp(args[1], "-f", 7) == 0) {
	/* the -f option is enabled */
		
		main_pid = currpid;
		mutex = 0;
		
		printf("----- FUTURE_EXCLUSIVE -----\n");
		/* Allocate futures */
		f_exclusive = future_alloc(FUTURE_EXCLUSIVE);
		if(f_exclusive == NULL) {
			printf("Future Allocation Failed\n");
			return SYSERR;
		}

		/* Create the producer & consumer and put them in the ready queue */
		resume( create(future_cons, 1024, 20, "fcons1", 1, f_exclusive) );
		resume( create(future_prod, 1024, 20, "fprod1", 1, f_exclusive) );

		sleep(1);

		printf("----- FUTURE_SHARED -----\n");
		/* Allocate futures */
		f_shared = future_alloc(FUTURE_SHARED);
		if(f_shared == NULL) {
			printf("Future Allocation Failed\n");
			return SYSERR;
		}

		/* Create the producer & consumer and put them in the ready queue */
		resume( create(future_cons, 1024, 20, "fcons2", 1, f_shared) );
		resume( create(future_cons, 1024, 20, "fcons3", 1, f_shared) );
		resume( create(future_cons, 1024, 20, "fcons4", 1, f_shared) );
		resume( create(future_cons, 1024, 20, "fcons5", 1, f_shared) );
		resume( create(future_prod, 1024, 20, "fprod2", 1, f_shared) );	
		
		/* wait for a message */
		//receive();

		sleep(1);

		printf("----- FUTURE_QUEUE -----\n");

		/* Allocate futures */
		f_queue = future_alloc(FUTURE_QUEUE);
		if(f_queue == NULL) {
			printf("Future Allocation Failed\n");
			return SYSERR;
		}

		resume( create(future_cons, 1024, 20, "fcons6", 1, f_queue) );
		resume( create(future_cons, 1024, 20, "fcons7", 1, f_queue) );
		resume( create(future_cons, 1024, 20, "fcons8", 1, f_queue) );
		resume( create(future_cons, 1024, 20, "fcons9", 1, f_queue) );
		resume( create(future_prod, 1024, 20, "fprod3", 1, f_queue) );	
		resume( create(future_prod, 1024, 20, "fprod4", 1, f_queue) );	
		resume( create(future_prod, 1024, 20, "fprod5", 1, f_queue) );
		resume( create(future_prod, 1024, 20, "fprod6", 1, f_queue) );	
		
		/* wait for a message */
		receive();

		
		/* Delete the exclusive future */
		future_free(f_exclusive);

		/* de-allocate the futures created */
		future_free(f_shared);

		/* de-allocate the futures created */
		future_free(f_queue);
		return OK;
	}
	else {
	/* Semapaphore calls */
		/* default count value of 2000 */
		int count = 2000;
		n = 0;
		
		/* Assign value of arg to count */
		
		if (nargs == 2){
			count = atoi(args[1]);
		}

		if(count <= 0) {
			fprintf(stderr, "Please enter a valid integer greater than 0\n");
			return SYSERR;
		}	
		
		/* initialize the semaphores */
		produced = semcreate(0);
		consumed = semcreate(1);

		/* Create the producer & consumer and put them in the ready queue */
		resume( create(producer, 1024, 20, "producer", 1, count) );
		resume( create(consumer, 1024, 20, "consumer", 2, count, currpid) );

		/* wait for a message to be received */
		receive();

		/* Delete the semaphores */
		semdelete(produced);
		semdelete(consumed);
	}

}
void future_producer_consumer(void){
	futures *f1=NULL,*f2=NULL,*f3=NULL;
	f1=future_alloc(0);
	//f2=future_alloc(0);
	//f3=future_alloc(0);
	//resume( create(future_cons, 1024, 20, "fcons1", 1, f1) );
	//resume( create(future_prod, 1024, 20, "fprod1", 1, f1) );
	
	//resume( create(future_cons, 1024, 20, "fcons1", 1, f1) );
	//resume( create(future_cons, 1024, 20, "fcons1", 1, f1) );
	//future_free(&f1);
	//f1=NULL;
	//printf("f1 flag %d\n",f1->value);
	//resume( create(future_cons, 1024, 20, "fcons1", 1, f1) );
	//resume( create(future_cons, 1024, 20, "fcons1", 1, f1) );
	//resume( create(future_prod, 1024, 20, "fprod1", 1, f1) );
	
	//resume( create(future_prod, 1024, 20, "fprod1", 1, f1) );
	
	//resume( create(future_cons, 1024, 20, "fcons1", 1, f1) );
	//resume( create(future_cons, 1024, 20, "fcons1", 1, f1) );
	
	//resume( create(future_cons, 1024, 20, "fcons1", 1, f1) );
	//resume( create(future_prod, 1024, 20, "fprod1", 1, f1) );
	//resume( create(future_cons, 1024, 20, "fcons1", 1, f1) );
	
	resume( create(future_prod, 1024, 20, "fprod1", 1, f1) );
	//resume( create(future_prod, 1024, 20, "fprod1", 1, f1) );
	//resume( create(future_prod, 1024, 20, "fprod1", 1, f1) );
	resume( create(future_cons, 1024, 20, "fcons1", 1, f1) );
	resume( create(future_cons, 1024, 20, "fcons1", 1, f1) );
	



	//resume( create(future_cons, 1024, 20, "fcons1", 1, f1) );
	//resume( create(future_cons, 1024, 20, "fcons1", 1, f1) );
	//resume( create(future_prod, 1024, 20, "fprod1", 1, f1) );
	//resume( create(future_prod, 1024, 20, "fprod1", 1, f1) );
	//resume( create(future_prod, 1024, 20, "fprod1", 1, f1) );
	
	//resume( create(future_cons, 1024, 20, "fcons1", 1, f1) );
	
	//resume( create(future_prod, 1024, 20, "fprod1", 1, f1) );
	//resume( create(future_cons, 1024, 20, "fcons1", 1, f1) );
	
	//resume( create(future_prod, 1024, 20, "fprod1", 1, f1) );
	//resume( create(future_cons, 1024, 20, "fcons1", 1, f1) );
	//resume( create(future_cons, 1024, 20, "fcons1", 1, f1) );
	
	//resume( create(future_cons, 1024, 20, "fcons1", 1, f1) );
	

	//resume( create(future_cons, 1024, 20, "fcons1", 1, f1) );
	//resume( create(future_cons, 1024, 20, "fcons1", 1, f1) );
	



	//resume( create(future_cons, 1024, 20, "fcons1", 1, f1) );
	

	//resume( create(future_prod, 1024, 20, "fprod1", 1, f1) );
	//resume( create(future_prod, 1024, 20, "fprod1", 1, f1) );
	//resume( create(future_cons, 1024, 20, "fcons1", 1, f1) );
	
	//resume( create(future_prod, 1024, 20, "fprod1", 1, f1) );
	//resume( create(future_prod, 1024, 20, "fprod1", 1, f1) );
	

	//resume( create(future_cons, 1024, 20, "fcons2", 1, f2) );
	//resume( create(future_prod, 1024, 20, "fprod2", 1, f2) );
	//resume( create(future_cons, 1024, 20, "fcons3", 1, f3) );
	//resume( create(future_prod, 1024, 20, "fprod3", 1, f3) );
	//resume( create(future_cons, 1024, 20, "fcons1", 1, f1) );
	//printf("hi");	
}
Exemplo n.º 6
0
shellcmd xsh_prodcons(int nargs, char *args[])
{
	
	int count = 0;
	//Argument verifications and validations

	if (nargs == 2 && strncmp(args[1], "--help", 7) == 0) {
		printf("Usage: %s\n\n", args[0]);
		printf("Description:\n");
		printf("\tDisplays Producer Consumer Problem\n");
		printf("\tAccepts 1 optional integer argument\n");
		printf("\tDefault Integer argument = 2000\n");
		return 0;
	} else if (nargs == 2 && strncmp(args[1], "-f", 3) == 0) {
		
		future *f_queue;
		future *f_exclusive;
		future *f_shared;
		f_exclusive = future_alloc(FUTURE_EXCLUSIVE);
		f_shared = future_alloc(FUTURE_SHARED);
		f_queue = future_alloc(FUTURE_QUEUE);
 
		//Test FUTURE_EXCLUSIVE
		resume( create(future_cons, 1024, 20, "fcons1", 1, f_exclusive) );
		resume( create(future_prod, 1024, 20, "fprod1", 1, f_exclusive) );

		// Test FUTURE_SHARED
		resume( create(future_cons, 1024, 20, "fcons2", 1, f_shared) );
		resume( create(future_cons, 1024, 20, "fcons3", 1, f_shared) );
		resume( create(future_cons, 1024, 20, "fcons4", 1, f_shared) ); 
		resume( create(future_cons, 1024, 20, "fcons5", 1, f_shared) );
		resume( create(future_prod, 1024, 20, "fprod2", 1, f_shared) );

		// Test FUTURE_QUEUE

		resume( create(future_cons, 1024, 20, "fcons6", 1, f_queue) );
		resume( create(future_cons, 1024, 20, "fcons7", 1, f_queue) );
		resume( create(future_cons, 1024, 20, "fcons7", 1, f_queue) );
		resume( create(future_cons, 1024, 20, "fcons7", 1, f_queue) );
		resume( create(future_prod, 1024, 20, "fprod3", 1, f_queue) );
		resume( create(future_prod, 1024, 20, "fprod4", 1, f_queue) );
		resume( create(future_prod, 1024, 20, "fprod5", 1, f_queue) );
		resume( create(future_prod, 1024, 20, "fprod6", 1, f_queue) );

	} else if (nargs > 2) {
		fprintf(stderr, "%s: too many arguments\n", args[0]);
		fprintf(stderr, "Try '%s --help' for more information\n",
			args[0]);
		return 1;
	} else if (nargs == 1) {
		count = 2000;
	} else {
 		char *s = args[1];
		int i;
		for(i = 0; i<strlen(s); i++) {
			if(s[i] >= '0' && s[i] <='9') {
				count *= 10;
				count += s[i] - '0'; 
			} else {
				fprintf(stderr, "%s: invalid arguments\n", args[0]);
				fprintf(stderr, "Try '%s --help' for more information\n",
				args[0]);
				return 1;
			} 

		}
	}
	consumed = semcreate(0);      
      	produced = semcreate(1);

      	//create the process producer and consumer and put them in ready queue.
      	//Look at the definations of function create and resume in exinu/system folder for reference.      
      	resume( create(producer, 1024, 20, "producer", 1, count) );
      	resume( create(consumer, 1024, 20, "consumer", 1, count) );
}