Beispiel #1
0
void my_lock(int fd)
{
	int oflag, i;
	union semun arg;
	struct semid_ds seminfo;

	if (initflag == 0) {
		oflag = IPC_CREAT | IPC_EXCL | SVSEM_MODE;
		if ((semid = semget(Ftok(LOCK_PATH, 0), 1, oflag)) >= 0) {
			arg.val = 1;
			Semctl(semid, 0, SETVAL, arg);
		} else if (errno == EEXIST) {
			semid = Semget(Ftok(LOCK_PATH, 0), 1, SVSEM_MODE);
			arg.buf = &seminfo;
			for (i = 0; i < MAX_TRIES; i++) {
				Semctl(semid, 0, IPC_STAT, arg);
				if (arg.buf->sem_otime != 0)
					goto init;
				sleep(1);
			}
			err_quit("semget OK, but semaphore not initalized");
		} else
			err_sys("semget error");

init:
		initflag = 1;
		postop.sem_num = 0;
		postop.sem_op = 1;
		postop.sem_flg = SEM_UNDO;
		waitop.semnum = 0;
		waitop.semop = -1;
		waitop.semflg = SEM_UNDO;
	}
	Semop(semid, &waitop, 1);
}
Beispiel #2
0
int
main(int argc, char **argv)
{
	int		i, nthreads;
	pthread_t	tid[MAXNTHREADS];
	union semun	arg;

	if (argc != 3)
		err_quit("usage: incr_svsem2 <#loops> <#threads>");
	nloop = atoi(argv[1]);
	nthreads = min(atoi(argv[2]), MAXNTHREADS);

		/* 4create semaphore and initialize to 0 */
	shared.semid = Semget(IPC_PRIVATE, 1, IPC_CREAT | SVSEM_MODE);
	arg.val = 0;
	Semctl(shared.semid, 0, SETVAL, arg);
	postop.sem_num = 0;		/* and init the two semop() structures */
	postop.sem_op  = 1;
	postop.sem_flg = SEM_UNDO;
	waitop.sem_num = 0;
	waitop.sem_op  = -1;
	waitop.sem_flg = SEM_UNDO;

		/* 4create all the threads */
	Set_concurrency(nthreads);
	for (i = 0; i < nthreads; i++) {
		Pthread_create(&tid[i], NULL, incr, NULL);
	}
		/* 4start the timer and release the semaphore */
	Start_time();
	Semop(shared.semid, &postop, 1);		/* up by 1 */

		/* 4wait for all the threads */
	for (i = 0; i < nthreads; i++) {
		Pthread_join(tid[i], NULL);
	}
	printf("microseconds: %.0f usec\n", Stop_time());
	if (shared.counter != nloop * nthreads)
		printf("error: counter = %ld\n", shared.counter);
	Semctl(shared.semid, 0, IPC_RMID);

	exit(0);
}
Beispiel #3
0
void
my_lock(int fd)
{
	int		oflag, i;
	union semun	arg;
	struct semid_ds	seminfo;

	if (initflag == 0) {
		oflag = IPC_CREAT | IPC_EXCL | SVSEM_MODE;
		if ( (semid = semget(Ftok(LOCK_PATH, 0), 1, oflag)) >= 0) {
				/* 4success, we're the first so initialize */
			arg.val = 1;
			Semctl(semid, 0, SETVAL, arg);

		} else if (errno == EEXIST) {
				/* 4someone else has created; make sure it's initialized */
			semid = Semget(Ftok(LOCK_PATH, 0), 1, SVSEM_MODE);
			arg.buf = &seminfo;
			for (i = 0; i < MAX_TRIES; i++) {
				Semctl(semid, 0, IPC_STAT, arg);
				if (arg.buf->sem_otime != 0)
					goto init;
				Write(pipefd[1], "", 1);	/* tell parent */
				sleep(1);
			}
			err_quit("semget OK, but semaphore not initialized");

		} else
			err_sys("semget error");
init:
		initflag = 1;
		postop.sem_num = 0;		/* and init the two semop() structures */
		postop.sem_op  = 1;
		postop.sem_flg = SEM_UNDO;
		waitop.sem_num = 0;
		waitop.sem_op  = -1;
		waitop.sem_flg = SEM_UNDO;
	}
	Semop(semid, &waitop, 1);		/* down by 1 */
}
Beispiel #4
0
int main(int argc, char **argv)
{
	int semid, nsems, i;
	struct semid_ds seminfo;
	unsigned short *ptr;
	union semun arg;

	if (argc < 2)
		err_quit("Usage: setvalue <pathname> [ value ...]");

	//first get the number of semaphore in the set
	semid = Semget(Ftok(argv[1], 0), 0, 0);
	arg.buf = &seminfo;
	Semctl(semid, 0, IPC_STAT, arg);
	nsems = arg.buf->sem_nsems;

	//now get the values from the command line
	ptr = Calloc(nsems, sizeof(unsigned short));
	arg.array = ptr;
	for (i = 0; i < nsems; i++)
		ptr[i] = atoi(argv[i + 2]);
	Semctl(semid, 0, SETALL, arg);
	exit(0);
}