Exemple #1
0
/*------------------------------------------------------------------------
 * pipdelete  --  Delete a pipe by releasing its table entry
 *------------------------------------------------------------------------
 */
syscall	pipdelete(
	 	pipid32		pip			/* ID of semaphore to delete	*/
	)
{
	intmask	mask;				/* saved interrupt mask			*/

	mask = disable();

	if (isbadpip(pip)) {
		restore(mask);
		return SYSERR;
	}

	struct pentry *pipptr;		/* ptr to pipe table entry		*/
	
	pipptr = &piptab[pip];
	if (pipptr->pstate != PIPE_USED ||
		pipptr->powner != getpid()) {
		restore(mask);
		return SYSERR;
	}

	pipptr->pstate = PIPE_FREE;
	pipptr->powner = 0;
	pipptr->prdsem = semdelete(pipptr->prdsem);
	pipptr->pwrsem = semdelete(pipptr->pwrsem);

	restore(mask);
	return OK;
}
void consumer(int count)
{
    int j ;
    //Code to consume values of global variable 'n' until the value of n is less than or equal to count
    //print consumed value e.g. consumed : 8

    for(j = 1; j<=count; j++)
    {
        wait(produced);
        printf("Consumed Value :%d\n",n);
        signal(consumed);
    }
    printf("Ending the program, semaphores deleted..");
    semdelete(produced);
    semdelete(consumed);

}
Exemple #3
0
syscall pipdelete(	
		pipid32 pip  
	)
{
	struct pipent *pipptr;
	intmask mask;

	mask = disable();
	
	if(isbadpipid(pip)){
		restore(mask);
		return (pipid32) (SYSERR);
	}

	pipptr = &piptab[pip];

	if(pipptr->pipstate == PIPE_FREE){
		restore(mask);
		return (pipid32) (SYSERR);

	}
	
	if(pipptr->pipOwner != getpid()){

		restore(mask);
		return (pipid32) (SYSERR);
		
	}

	pipptr->pipstate = PIPE_FREE;
	memset(pipptr->buf, '\0', PIPE_SIZE);	
	
	pipptr->pipOwner = -1;
	pipptr->pipReader = -1;
	pipptr->pipWriter = -1;
	pipptr->bufCharCount = -1;
	pipcount--;

	semdelete(pipptr->fullSem);
	semdelete(pipptr->emptySem);
	restore(mask);
	return OK;

}
Exemple #4
0
process test_semaphore5(bool8 verbose)
{
    pid32 atid;
    bool8 passed = TRUE;
    sid32 s;
    byte testResult = 0;
    char msg[50];

    testPrint(verbose, "Semaphore creation: ");
    s = semcreate(0);
    if (isbadsem(s))
    {
        passed = FALSE;
        sprintf(msg, "%d", s);
        testFail(verbose, msg);
    }
    else if (test_checkSemCount(s, 0))
    {
        testPass(verbose, "");
    }
    else
    {
        passed = FALSE;
    }

    ready(atid =
          create((void *)test_semWaiter, INITSTK, getprio(getpid()) + 10,
                 "SEMAPHORE-A", 3, s, 1, &testResult), RESCHED_YES);

    testPrint(verbose, "Wait on semaphore: ");
    if (test_checkProcState(atid, PR_WAIT)
        && test_checkSemCount(s, -1) && test_checkResult(testResult, 0))
    {
        testPass(verbose, "");
    }
    else
    {
        passed = FALSE;
    }

    testPrint(verbose, "Reset semaphore: ");
    if ((OK == semreset(s, 0))
	&& test_checkProcState(atid, PR_FREE)
        && test_checkSemCount(s, 0) && test_checkResult(testResult, 1))
    {
        testPass(verbose, "");
    }
    else
    {
        passed = FALSE;
    }

    testPrint(verbose, "Reset semaphore (invalid count): ");
    if (SYSERR == semreset(s, -5))
    {
        testPass(verbose, "");
    }
    else
    {
        passed = FALSE;
    }

    testPrint(verbose, "Reset invalid semaphore: ");
    if (SYSERR == semreset(-1, 0))
    {   
        testPass(verbose, "");
    }
    else
    {
        passed = FALSE;
    }

    semdelete(s);

    testPrint(verbose, "Reset free semaphore: ");
    if (SYSERR == semreset(s, 0))
    {   
        testPass(verbose, "");
    }
    else
    {
        passed = FALSE;
    }

    if (TRUE == passed)
    {
        testPass(TRUE, "");
    }
    else
    {
        testFail(TRUE, "");
    }

    return OK;
}
Exemple #5
0
process test_semaphore(bool8 verbose)
{
    pid32 apid;
    bool8 passed = TRUE;
    sid32 s;
    byte testResult = 0;
    char msg[50];

    /* Single semaphore tests */
    testPrint(verbose, "Semaphore creation: ");
    s = semcreate(0);
    if (isbadsem(s))
    {
        passed = FALSE;
        sprintf(msg, "%d", s);
        testFail(verbose, msg);
    }
    else if (test_checkSemCount(s, 0, verbose))
    {
        testPass(verbose, "");
    }
    else
    {
        passed = FALSE;
    }

    testPrint(verbose, "Wait on semaphore: ");
    if ((SYSERR != resume(apid =
          create((void *)test_semWaiter, INITSTK, 31,
                 "SEMAPHORE-A", 3, s, 1, &testResult)))
    	&& test_checkProcState(apid, PR_WAIT, verbose)
        && test_checkSemCount(s, -1, verbose)
        && test_checkResult(testResult, 0, verbose))
    {
        testPass(verbose, "");
    }
    else
    {
        passed = FALSE;
    }


    testPrint(verbose, "Signal semaphore: ");
    if ((OK == signal(s))
    	&& test_checkProcState(apid, PR_FREE, verbose)
        && test_checkSemCount(s, 0, verbose)
        && test_checkResult(testResult, 1, verbose))
    {
        testPass(verbose, "");
    }
    else
    {
        passed = FALSE;
    }

    testPrint(verbose, "Signaln semaphore (valid count): ");
    if ((OK == signaln(s, 5))
    	&& test_checkProcState(apid, PR_FREE, verbose)
        && test_checkSemCount(s, 5, verbose)
        && test_checkResult(testResult, 1, verbose))
    {
        testPass(verbose, "");
    }
    else
    {
        passed = FALSE;
    }

    testPrint(verbose, "Signaln semaphore (invalid count): ");
    if (SYSERR == signaln(s, -5))
    {
        testPass(verbose, "");
    }
    else
    {
        passed = FALSE;
    }


    /* Free semaphore, single semaphore tests */
    testPrint(verbose, "Delete valid semaphore: ");
    if ((OK == semdelete(s))
	&& (semtab[s].sstate == S_FREE)
	&& isempty(semtab[s].squeue))
    {
	testPass(verbose, "");
    }
    else
    {
        passed = FALSE;
    }

    testPrint(verbose, "Delete invalid semaphore: ");
    if (SYSERR == semdelete(-1))
    {   
        testPass(verbose, "");
    }
    else
    {
        passed = FALSE;
    }
 
    testPrint(verbose, "Delete free semaphore: ");
    if (SYSERR == semdelete(s))
    {   
        testPass(verbose, "");
    }
    else
    {
        passed = FALSE;
    }
 
    testPrint(verbose, "Signal bad semaphore id: ");
    if (SYSERR == signal(-1))
    {
        testPass(verbose, "");
    }
    else
    {
        passed = FALSE;
    }

    testPrint(verbose, "Signal free semaphore: ");
    if (SYSERR == signal(s))
    {
        testPass(verbose, "");
    }
    else
    {
        passed = FALSE;
    }


    testPrint(verbose, "Signaln bad semaphore id: ");
    if (SYSERR == signaln(-1, 4))
    {
        testPass(verbose, "");
    }
    else
    {
        passed = FALSE;
    }

    testPrint(verbose, "Signaln free semaphore: ");
    if (SYSERR == signaln(s, 4))
    {
        testPass(verbose, "");
    }
    else
    {
        passed = FALSE;
    }

    testPrint(verbose, "Wait on bad semaphore id: ");
    if (SYSERR == wait(-1))
    {
        testPass(verbose, "");
    }
    else
    {
        passed = FALSE;
    }
 
    testPrint(verbose, "Wait on free semaphore: ");
    if (SYSERR == wait(s))
    {
        testPass(verbose, "");
    }
    else
    {
        passed = FALSE;
    }



    /* Process A should be dead, but in case the test failed. */
    kill(apid);

    /* General semaphore pass/fail */
    if (TRUE == passed)
    {
        testPass(TRUE, "");
    }
    else
    {
        testFail(TRUE, "");
    }

    return OK;
}
/*------------------------------------------------------------------------
 * 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);
	}

}
Exemple #7
0
process test_semaphore2(bool8 verbose)
{
    pid32 apid, bpid;
    bool8 passed = TRUE;
    sid32 s;
    byte testResult = 0;
    char msg[50];

    testPrint(verbose, "Semaphore creation: ");
    s = semcreate(0);
    if (isbadsem(s))
    {
        passed = FALSE;
        sprintf(msg, "%d", s);
        testFail(verbose, msg);
    }
    else if (test_checkSemCount(s, 0))
    {
        testPass(verbose, "");
    }
    else
    {
        passed = FALSE;
    }

    ready(apid =
          create((void *)test_semWaiter, INITSTK, 31,
                 "SEMAPHORE-A", 3, s, 1, &testResult), RESCHED_NO);
    ready(bpid =
          create((void *)test_semWaiter, INITSTK, 31,
                 "SEMAPHORE-B", 3, s, 1, &testResult), RESCHED_YES);

    /* Both processes should run and immediately wait. */
    testPrint(verbose, "Wait on semaphore: ");
    if (test_checkProcState(apid, PR_WAIT)
        && test_checkProcState(bpid, PR_WAIT)
        && test_checkSemCount(s, -2) && test_checkResult(testResult, 0))
    {
        testPass(verbose, "");
    }
    else
    {
        passed = FALSE;
    }

    /* Process A waited first, so a signal should release it. */
    testPrint(verbose, "Signal first semaphore: ");
    if ((OK == signal(s))
    	&& test_checkProcState(apid, PR_FREE)
        && test_checkProcState(bpid, PR_WAIT)
        && test_checkSemCount(s, -1) && test_checkResult(testResult, 1))
    {
        testPass(verbose, "");
    }
    else
    {
        passed = FALSE;
    }

    /* Process B waited second, so another signal should release it. */
    testPrint(verbose, "Signal second semaphore: ");
    if ((OK == signal(s))
    	&& test_checkProcState(bpid, PR_FREE)
        && test_checkSemCount(s, 0) && test_checkResult(testResult, 2))
    {
        testPass(verbose, "");
    }
    else
    {
        passed = FALSE;
    }

    if (TRUE == passed)
    {
        testPass(TRUE, "");
    }
    else
    {
        testFail(TRUE, "");
    }

    /* Processes should be dead, but in case the test failed. */
    kill(apid);
    kill(bpid);
    semdelete(s);

    return OK;
}