Example #1
0
int main (int argc, char **argv)
{
	struct sembuf buf = {0, 0, 0}; //declaration buf structure
	
	int semid = createSem(PATH, PROJECTID, SEMVALUE, IPC_CREAT | WRITEREADATT); //create semaphore

	switch(fork())
	{
		case -1: //if fork error
			perror(FORKERROR);
			exit(1);
			break;
		case 0: 
			printData(semid); //print process id and sem data
			buf.sem_op = CLOSE; // close semaphore
			if(semop(semid, &buf, 1) == -1) // send cmd
			{
				perror(SEMOPERROR);
				exit(EXIT_FAILURE);
			}
			criticalSection(); // do sth
			printData(semid); //print process id and sem data
			buf.sem_op = OPEN; // open semaphore
			if(semop(semid, &buf, 1) == -1) // send cmd
			{
				perror(SEMOPERROR);
				exit(EXIT_FAILURE);
			}
			break;
		default:
			printData(semid); //print process id and sem data
			buf.sem_op = CLOSE; // close semaphore
			if(semop(semid, &buf, 1) == -1) // send cmd
			{
				perror(SEMOPERROR);
				exit(EXIT_FAILURE);
			}
			criticalSection();  // do sth
			printData(semid); //print process id and sem data
			buf.sem_op = OPEN; // open semaphore
			if(semop(semid, &buf, 1) == -1) // send cmd
			{
				perror(SEMOPERROR);
				exit(EXIT_FAILURE);
			}
			printf(PARENTEND); //end of parent
			printData(semid); //print process id and sem data
			if(wait(NULL) == -1) // wait for children
			{
				perror(WAITERROR);
				exit(EXIT_FAILURE);
			}
			printf(SEMDELETE, semid); 
			deleteSem(semid); // delete sem
			
			break;
	}
	
	return 0;
}
Example #2
0
int bumount() {
    if (close(fichero) < 0) {
        printf("bloques.c: > bumount \n");
        return -1;
    }
    deleteSem();
    return 0;
}