示例#1
0
文件: semtest1.c 项目: thlmille/asg2
int main (int argc, char **argv) {
    int foo = seminit(1,0);
    printf ("semval = %d\n", foo);
    int bar = seminit(1,0);
    if (bar != 1) printf("bar rejected\n");
    semfree(1);
    return 0;
}
示例#2
0
void lock_init (struct lock *l)
{
   assert (l != NULL);

   l->mutex = seminit(0,1);
   l->next = seminit(0,0);
   l->nextCount = 0;
}
示例#3
0
文件: semtest9.c 项目: thlmille/asg2
int main (int argc, char **argv) {
  seminit(1, 5);
  semfree (1);
  seminit(1, 5);
  semfree (1);
  seminit(1, 5);
  semfree (1);
  seminit(1, 5);
  semfree (1);
}
示例#4
0
int main(){
    srand(time(NULL));
    int gen1sem = seminit(1,1);
    int gen2sem = seminit(2,1);
    int gen3sem = seminit(3,1);
    int nursery = seminit(4,2);
    int randARR[7] = {1,2,2,2,3,2,1};
    int v = 0;
    for (; v < 7; v++){
        genNUMB(randARR[v]);
    }
}
示例#5
0
/* what does this program do?
   First we make a semaphore
   check what its value is
   do semdown on it
   start sleep 10 seconds
	-> at this point, another program would try to semdown the semaphore we created
   end sleep
   now do a semup, program B should be able to proceed.
   
	-> program C should now free the semaphore, and try a semvalue on it.
   */
int main() {
	int result;
	int e;
	message m;
	e = errno;
	
	printf("+-----------------+\n");
	printf("| Program A Start |\n");	
	printf("+-----------------+\n");
	result = seminit(2000, 1);
	printf("<1> Create semaphore named 2000, with value 1: result=%d & e=%d\n", result, errno);
	
	result = semvalue(2000);
	printf("<2> Look at value of semaphore[index for indentifier(2000)]: result=%d & e=%d\n", result, errno);
	
	result = semdown(2000);
	printf("<3> Do a sem down: result=%d & e=%d\n", result, errno);
	
	printf("<4> I am to do nothing for 10 seconds.\n");
	int wait_unit;
	for (wait_unit = 1; wait_unit < 11; wait_unit++) {
		sleep(1);
		printf("   Waiting %d\n", wait_unit);
	}
	printf("<4> I done doing nothing.\n");

	result = semup(2000);
	printf("<5> Sem Up: result=%d & e=%d\n", result, errno);
	
	printf("<6> Program A has no more to do.\n", result, errno);
	
	return 0;
}
示例#6
0
void cond_init(struct cond *cnd, struct lock *l)
{
   assert (cnd != NULL && l != NULL);
   cnd->lock = l;
   cnd->condSem = seminit(0,0);
   cnd->semCount = 0;

}
示例#7
0
int amservicestart() 
{

	pthread_mutex_init(&net_mutex, NULL);
	seminit();

	sockfd = dtcmr_client_creat("127.0.0.1",12580, IPPROTO_TCP);
	if (sockfd<0){
		printf("dtcmr_client_creat is error\n");	
		return -1;
	}
	memset(sddata,2,1024);

	send_pthread_init();

	while(1)
	{
		pthread_mutex_lock(&net_mutex);
		printf("while recv_pack\n");
		recv_pack();
		pthread_mutex_unlock(&net_mutex);
		usleep(5000);
	}
}
示例#8
0
int main()
{
	int semid;
	int semval;
	
	seminit(&semid, 0);
	
	semval = semvalue(semid);
	printf("semval: %d\n", semval);
	
	semv(semid);
	
	semval = semvalue(semid);
	printf("semval: %d\n", semval);

	semp(semid);
	
#if 0
#endif

	semdistr(semid);

	return 0;
}
示例#9
0
文件: lockcond.c 项目: thlmille/asg2
void lock_init (lock_ptr l) {
  l->mutex = seminit (0, 1);
  l->next = seminit (0, 0);
  l->next_count = 0;
}
示例#10
0
文件: lockcond.c 项目: thlmille/asg2
void cond_init (cond_ptr cnd, lock_ptr l) {
  cnd->the_lock = l;
  cnd->cond_sem = seminit (0, 0);
  cnd->sem_count = 0;
}
示例#11
0
int main (int argc, char **argv) {
  printf ("%d\n", seminit (1, 4000));

}
示例#12
0
int main (int argc, char **argv) 
{
    FILE *fp, *fp2;
    int mutex = 0;
    int writing = 0;
    int priority = 0;

    // check if any semaphore intializations returned an error
    mutex = seminit(0,1);
    if (mutex < 0)
    {
    switch (errno) {
        case EEXIST:
            fprintf(stderr, "%s: mutex semaphore already exists\n", argv[0]);
            return -1;
            break;
        case EAGAIN:
            fprintf(stderr, "%s: cannot create mutex semaphore due to max amount\n", argv[0]);
            return -1;
            break;
        case EINVAL:
            fprintf(stderr, "%s: mutex id is negative or value not in range (should not reach here!)\n", argv[0]);
            return -1;
            break;
        case -EEXIST:
            fprintf(stderr, "%s: mutex semaphore already exists\n", argv[0]);
            return -1;
            break;
        case -EAGAIN:
            fprintf(stderr, "%s: cannot create mutex semaphore due to max amount\n", argv[0]);
            return -1;
            break;
        case -EINVAL:
            fprintf(stderr, "%s: mutex id is negative or value not in range (should not reach here!)\n", argv[0]);
            return -1;
            break;
        default:
            fprintf(stderr, "%s: Unknown error returned on creation of mutex semaphore\n", argv[0]);
            return -1;
            break;
        }
    }
    writing = seminit(0,1);
    if (writing < 0)
    {
    switch (errno) {
        case EEXIST:
            fprintf(stderr, "%s: writing semaphore already exists\n", argv[0]);
            return -1;
            break;
        case EAGAIN:
            fprintf(stderr, "%s: cannot create writing semaphore due to max amount\n", argv[0]);
            return -1;
            break;
        case EINVAL:
            fprintf(stderr, "%s: writing id is negative or value not in range (should not reach here!)\n", argv[0]);
            return -1;
            break;
        case -EEXIST:
            fprintf(stderr, "%s: writing semaphore already exists\n", argv[0]);
            return -1;
            break;
        case -EAGAIN:
            fprintf(stderr, "%s: cannot create writing semaphore due to max amount\n", argv[0]);
            return -1;
            break;
        case -EINVAL:
            fprintf(stderr, "%s: writing id is negative or value not in range (should not reach here!)\n", argv[0]);
            return -1;
            break;
        default:
            fprintf(stderr, "%s: Unknown error returned on creation of writing semaphore\n", argv[0]);
            return -1;
            break;
        }
    }
    priority = seminit(0,1);
    if (priority < 0)
    {
    switch (errno) {
        case EEXIST:
            fprintf(stderr, "%s: priority semaphore already exists\n", argv[0]);
            return -1;
            break;
        case EAGAIN:
            fprintf(stderr, "%s: cannot create priority semaphore due to max amount\n", argv[0]);
            return -1;
            break;
        case EINVAL:
            fprintf(stderr, "%s: priority id is negative or value not in range (should not reach here!)\n", argv[0]);
            return -1;
            break;
        case -EEXIST:
            fprintf(stderr, "%s: priority semaphore already exists\n", argv[0]);
            return -1;
            break;
        case -EAGAIN:
            fprintf(stderr, "%s: cannot create priority semaphore due to max amount\n", argv[0]);
            return -1;
            break;
        case -EINVAL:
            fprintf(stderr, "%s: priority id is negative or value not in range (should not reach here!)\n", argv[0]);
            return -1;
            break;
        default:
            fprintf(stderr, "%s: Unknown error returned on creation of priority semaphore\n", argv[0]);
            return -1;
            break;
        }
    }
    printf("%s mutex id    : %d\n", argv[0], mutex);
    printf("%s writing id  : %d\n", argv[0], writing);
    printf("%s priority id : %d\n", argv[0], priority);

    // write semaphore ids to a file named 'tempfile'
    fp = fopen("semfile", "w");
    fprintf(fp, "%d ", mutex);
    fprintf(fp, "%d ", writing);
    fprintf(fp, "%d\n", priority);
    fclose(fp);

    fp2 = fopen("numreaders", "w");
    fprintf(fp2, "0\n");
    fclose(fp2);

    return 1;
}
示例#13
0
int main(){
    int retINIT = seminit(-5,3);
    printf("retINIT: %d\n", retINIT);
    int retINIT2 = seminit(1001,3);
    printf("retINIT2: %d\n", retINIT2);
}
示例#14
0
/** Initialize all variables needed for ipc */
static void ipc_init(void)
{

	/* All communicating processes need to be started from within the ssame working dir in order to make key have the same value */
	key_t key = ftok(".", 'x');

	if(key == -1) {
		bail_out(EXIT_FAILURE, "Couldn't create ipc key via ftok");
	}

	/* cnd is a two field semaphore (0 = write, 1 = read)*/
	if((cnd = mseminit(key, (0600), 2, 1, 0)) == -1) {
		if((cnd = msemgrab(key, 2)) == -1) {
			bail_out(EXIT_FAILURE, "Error aquiring conditional semephores");
		}
	}

	/* mtx is a mutex semaphore to protect access to shm */
	if((mtx = seminit(key + 1, (0600), 0)) == -1) {
		if((mtx = semgrab(key + 1)) == -1) {
			bail_out(EXIT_FAILURE, "Error aquiring mutual exclusive semephore");
		}
	}
	

	/* Create or fetch shm segment */
	if((shm = shmget(key, sizeof(ipc_data_t), IPC_CREAT | IPC_EXCL | (0600))) == -1) {

		if((shm = shmget(key, sizeof(ipc_data_t), 0)) == -1) {
			bail_out(EXIT_FAILURE, "Error aquiring shared mem");
		}

		/* The process that creates the shm segment will up the mtx, after it's initialized */
		if(semdown(mtx) == -1) {
			bail_out(EXIT_FAILURE, "Error downing mutex");

		}

		/* Mount shm */
		if((shared = shmat(shm, NULL, 0)) == (void *) -1) {
			bail_out(EXIT_FAILURE, "Error attaching shm segment");
		}

#ifndef _BUILD_READIN
		/* Signal listening reader */
		shared->flag |= READER_F;	
#endif

		/* Let other processes proceed */
		if(semup(mtx) == -1) {
			bail_out(EXIT_FAILURE, "Error upping mutex");
		}

	} else {

		/* Mount shm */
		if((shared = shmat(shm, NULL, 0)) == (void *) -1) {
			bail_out(EXIT_FAILURE, "Error attaching shm segment (creator)");
		}

		/* If we created the segment, we'll initialize it */
		(void) memset(shared, 0, sizeof(ipc_data_t));

#ifndef _BUILD_READIN
		/* Signal listening reader */
		shared->flag |= READER_F;	
#endif

		/* And signal other processes to proceed */
		if(semup(mtx) == -1) {
			bail_out(EXIT_FAILURE, "Error opening mutex");
		}
		
	} 

}