Example #1
0
int main(int argc, const char * argv[]) {

    int key; //unique integer identifier for the shared memory region to be created, attached to, or
             //detached from
    void *address; //pointer to the lowest (virtual) address in the shared memory region
    sem_t semID; //semaphore ID
	int err; //error code from a system call
	int i; //used for a counter
	
	key = 12345;
    
    //create a two-page shared region (each page is 4K bytes)
    err = shmem(key,2, (void **) &address);
	
	if (err != 0) {
	    printf("shmem failed in shmdemo 1; error code = %d\n", err);
	    exit(1);
	}
    
    //fill all except first 4 bytes with random ints (each int is 4 bytes long)
    //--original number is 2048 because each page is 1024 and there are 2 pages--
    for(i=1;i<=2047;i++)
    {
	    address[i] = rand();
    }
    
    //create semophore with initial count of 0
	semID =  newsema(0);
   
   	if (semID < 0) 
   	{
	    printf("newsema failed; error code = %d\n", (int)semID);
	    shmem(key,0,(void **)&address);	/* free the shared memory */
	    exit(1);
	}
    
    //put semID in the first 4 bytes of shared memory region
    address[0] = int(semID);
    
    printf("\nWaiting on the second process....\n");
    
    //do a down operation on a semaphore (5 min)
    //if time runs out, throw an error
    if(down(semID,300000) == TIMEOUT)
	{
		printf("shmdemo 1 timed out.\n");
	}
    else
	{
	    printf("Successfully returned to shmdemo 1.\n");
	}
	
	shmem(key,0,(void **)&address);
	freesema(semID);
    return 0;
}
/*
 * This is called to free all the memory associated with a dquot
 */
void
xfs_qm_dqdestroy(
	xfs_dquot_t	*dqp)
{
	ASSERT(! XFS_DQ_IS_ON_FREELIST(dqp));

	mutex_destroy(&dqp->q_qlock);
	freesema(&dqp->q_flock);
	sv_destroy(&dqp->q_pinwait);

#ifdef XFS_DQUOT_TRACE
	if (dqp->q_trace)
	     ktrace_free(dqp->q_trace);
	dqp->q_trace = NULL;
#endif
	kmem_zone_free(xfs_Gqm->qm_dqzone, dqp);
	atomic_dec(&xfs_Gqm->qm_totaldquots);
}
Example #3
0
int main(int argc, char *argv[])
{
    int which;			/* which way was the program invoked? */
    int err;			/* error code from a system call */
    int *mem;			/* pointer to the shared memory region */
    int i;			/* loop index */
    sem_t s;			/* semaphore ID */

    if (argc == 2)
        which = atoi(argv[1]);
    if (argc != 2 || which < 1 || which > 2) {
        printf("Usage:    shmdemo 1    -or-    shmdemo 2\n");
        printf("Look at the program source code for details on operation.\n");
        return 1;
    }

    if (which == 1) {
        err = shmem(12345,1,(void **)&mem);
        if (err != 0) {
            printf("shmem failed in shmdemo 1; error code = %d\n", err);
            exit(1);
        }
        s = newsema(0);
        if (s < 0) {
            printf("newsema failed; error code = %d\n", (int)s);
            shmem(12345,0,(void **)&mem);	/* free the shared memory */
            exit(1);
        }
        mem[0] = (int)s;
        for(i=1; i<=100; i++)
            mem[i] = i;
        if (down(s,60 * 1000) == TIMEOUT)	/* one minute, for now XXX */
            printf("shmdemo 1 timed out.\n");
        else
            printf("Successfully returned to shmdemo 1.\n");
        shmem(12345,0,(void **)&mem);
        freesema(s);
        return 0;
    }

    if (which == 2) {
        err = shmem(12345,1,(void **)&mem);
        if (err != 0) {
            printf("shmem failed in shmdemo 2; error code = %d\n", err);
            exit(1);
        }
        err = 0;	/* this is 0 anyway, but we'll be explicit */
        for(i=1; i<=100; i++) {
            if (mem[i] != i) {
                err = i;
                break;
            }
        }
        if (err) {
            printf("shmdemo 2 error: value in mem[%d] = %d.\n", err, mem[err]);
            printf("                 It should be %d\n", err);
        } else
            printf("shmdemo 2: all integer values are as expected.\n");
        s = (sem_t)mem[0];
        shmem(12345,0,(void **)&mem);
        up(s);
        return 0;
    }
}