Пример #1
0
/* include incr */
void *
incr(void *arg)
{
	int		i;

	for (i = 0; i < nloop; i++) {
		Semop(shared.semid, &waitop, 1);
		shared.counter++;
		Semop(shared.semid, &postop, 1);
	}
	return(NULL);
}
Пример #2
0
int busy()
{
	int i = 0;
	for(i =0; i<100; i++)
	{
		Semop((semid, &waitsem[0], 1));
		access_shm();
		Semop((semid, &notifysem[0], 1));
	}
	shmdt(shm_data);

	return 0;

}
Пример #3
0
void p(int semid, int semnum) {
	struct sembuf task;
	task.sem_num = semnum;
	task.sem_op = -1;
	task.sem_flg = 0;
	Semop(semid, &task, 1);
}
Пример #4
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);
}
Пример #5
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);
}
Пример #6
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 */
}
Пример #7
0
int
main(int argc, char **argv)
{
	int		c, i, flag, semid, nops;
	struct sembuf	*ptr;

	flag = 0;
	while ( (c = Getopt(argc, argv, "nu")) != -1) {
		switch (c) {
		case 'n':
			flag |= IPC_NOWAIT;		/* for each operation */
			break;

		case 'u':
			flag |= SEM_UNDO;		/* for each operation */
			break;
		}
	}
	if (argc - optind < 2)			/* argc - optind = #args remaining */
		err_quit("usage: semops [ -n ] [ -u ] <id> operation ...");

	semid = atol(argv[optind]);
	optind++;
	nops = argc - optind;

		/* 4allocate memory to hold operations, store, and perform */
	ptr = Calloc(nops, sizeof(struct sembuf));
	for (i = 0; i < nops; i++) {
		ptr[i].sem_num = i;
		ptr[i].sem_op = atoi(argv[optind + i]);	/* <0, 0, or >0 */
		ptr[i].sem_flg = flag;
	}
	Semop(semid, ptr, nops);

	exit(0);
}
Пример #8
0
void
my_unlock(int fd)
{
	Semop(semid, &postop, 1);		/* up by 1 */
}
Пример #9
0
void my_unlock(int fd)
{
	Semop(semid, &postop, 1);
}