Ejemplo n.º 1
0
bool JdbcUtil::lockSemaphore(unsigned short sem_idx, bool lock)
{
	setError(NULL);

	if (lock && !getSemaphores(false)) return false;

	const char *lock_str;
	if (lock) lock_str = "lock";
	else lock_str = "unlock";
	if (semaphoreId==-1)
	{
		char rc[80];
		sprintf(rc,"Cannot %s semaphore when semaphore not attached",lock_str);
		setError(rc);
		return false;
	}
	struct sembuf sem_op;
	sem_op.sem_num = sem_idx;
	if (lock) sem_op.sem_op = -1;
	else sem_op.sem_op = 1;
	sem_op.sem_flg = 0;
	if (semop(semaphoreId,&sem_op,1)==-1)
	{
		char rc[80];
		sprintf(rc,"Error during %s of semaphore",lock_str);
		setError(rc);
		return false;
	}
	return true;
}
Ejemplo n.º 2
0
int main(int argc, char *argv[])
{
    int i;
    struct sems *s = getSemaphores();
    if (s == NULL) {
        errExit("getSemaphores");
    }

    printf("key\t\tsemid\t\towner\t\tperms\t\tnsems\n");

    for (i = 0; i < s->len; i++) {
        printf(
            "0x%08x\t%d\t\t%s\t\t%o\t\t%d\n",
            s->info[i].key,
            s->info[i].semid,
            userNameFromId(s->info[i].uid),
            s->info[i].perms,
            s->info[i].nsems
        );
    }

    free(s->info);
    free(s);

    exit(EXIT_SUCCESS);
}
Ejemplo n.º 3
0
int main(int argc, char const *argv[])
{
	if (argc != 2)
	{
		printf("usage: %s <data file>\n", argv[0]);
		exit(4);
	}

	int semaphores = getSemaphores();
	semaphoreWait(semaphores, WRITER);

	FILE* outputFile;
	outputFile = fopen(argv[1], "w");
	if (outputFile == 0)
	{
		printf("error: couldn't open output file");
		exit(5);
	}

	StudentInfo* basePointer = getSharedMemoryPointer();
	StudentInfo* currentPointer = basePointer;

	for (int i = 0; i < MAX_STUDENTS; ++i)
	{
		fprintf(outputFile, "%s\n", currentPointer->studentName);
		fprintf(outputFile, "%s\n", currentPointer->studentID);
		fprintf(outputFile, "%s\n", currentPointer->address);
		fprintf(outputFile, "%s\n", currentPointer->telephoneNumber);
		
		currentPointer++;
	}

	fclose(outputFile);

	detachSharedMemoryPointer(basePointer);

	destroySharedMemory();
	destroyReadCount();

	semaphoreSignal(semaphores, WRITER);

	destroySemaphores(semaphores);
	return 0;
}
Ejemplo n.º 4
0
bool JdbcUtil::memoryInit(void)
{
	setError(NULL);
	if (!removeMemory()) return false;
	if (!removeSemaphores()) return false;
	if (!getMemory(true)) return false;
	if (!attachMemory()) return false;
	memset(sharedMemory,0,SHARED_MEMORY_SIZE);
	sharedMemory->active = true;

	if (!getSemaphores(true)) return false;
	if (semctl(semaphoreId, 0, SETVAL, 1) == -1)
	{
		int err = errno;
		removeMemory();
		removeSemaphores();
		setPerror(err,"Cannot initialize semaphore value");
		return false;
	}
	return true;
}
Ejemplo n.º 5
0
int main(void) {
	// create shared memory buffer with rw perms
	int shmid = shmget(1000, 1, IPC_CREAT | PERMS);
	if(shmid < 0) {
		fprintf(stderr, "Unable to create shared buffer.\n");
		exit(EXIT_FAILURE);
	}

	// allocate buffer
	if((buffer = (int*)shmat(shmid, (char*)0, 0)) == (int*)-1) {
		fprintf(stderr, "Unable to allocate.\n");
		exit(EXIT_FAILURE);
	}

	// use smget to get semaphores with read write permissions
	getSemaphores();
	// initialize semaphores
	createSemaphores();

	// create child processes
	int pid_tob = fork();
	if(pid_tob < 0) { perror("pid_tob"); exit(EXIT_FAILURE); }
	if(pid_tob == 0) {
		while(1) {
			// in smoker with tobacco
			P(stob);	// wait for signal
			P(smutex);	// wait to modify buffer
			smoker_t();	// roll up cigarette
			V(sage);	// signal agent
			V(smutex);	// signal that buffer can now be modified again
		}
	}else {
		int pid_pap = fork();
		if(pid_pap < 0) { perror("pid_pap"); exit(EXIT_FAILURE); }
		if(pid_pap == 0) {
			while(1) {
				// in smoker with paper
				P(spap);
				P(smutex);
				smoker_p();
				V(sage);
				V(smutex);
			}

		}else {
			int pid_mat = fork();
			if(pid_mat < 0) { perror("pid_mat"); exit(EXIT_FAILURE); }
			if(pid_mat == 0) {
				while(1) {
					// in smoker with matches
					P(smat);
					P(smutex);
					smoker_m();
					V(sage);
					V(smutex);
				}
			}else {
				// in agent
				int i;
				for(int i = 0; i < N; ++i) {
					P(smutex);	// wait for signal to modify buffer

					agent();	// place items on table
					
					// signal the smoker with the remaining item
					int supply = *buffer;
					if(supply == TOBACCO) V(stob);
					else if(supply == PAPER) V(spap);
					else V(smat);

					V(smutex);	// done modifying buffer
					P(sage);	// now wait while a smoker process loop finishes
					putchar('\n');
				}

				// infinite loop, so kill child processes on exit
				kill(pid_pap, SIGUSR1);
				kill(pid_tob, SIGUSR1);
				kill(pid_mat, SIGUSR1);
				exit(EXIT_SUCCESS);
			}
		}
	}
}