Skip to content

Instantly share code, notes, and snippets.

@diogocapela

diogocapela/main.c

Created Apr 17, 2019
Embed
What would you like to do?
Error: No such file or directory
#include <errno.h>
#include <fcntl.h>
#include <semaphore.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <time.h>
#include <unistd.h>
#define SEMAFORO_FILE "/EX05"
int main(int argc, char *agrv[]) {
// Open new semaforo
sem_t *semaforo;
if ((semaforo = sem_open(SEMAFORO_FILE, O_CREAT | O_EXCL, 0644, 0)) == SEM_FAILED) {
perror("Error at sem_open()!\n");
exit(EXIT_FAILURE);
}
// For a new process
pid_t pid = fork();
// If we are at the father process
if (pid > 0) {
if (sem_wait(semaforo) == -1) {
perror("Error at sem_wait()!");
exit(EXIT_FAILURE);
}
printf("I'm the father!\n");
}
// If we are at the child process
if (pid == 0) {
printf("I'm the child!\n");
if (sem_post(semaforo) == -1) {
perror("Error at sem_post()!");
exit(EXIT_FAILURE);
}
}
// Close semaforo
if (sem_close(semaforo) == -1) {
perror("Error at sem_close()!\n");
exit(EXIT_FAILURE);
}
// Remove semaforo from system
if (sem_unlink(SEMAFORO_FILE) == -1) {
perror("Error at sem_unlink()!\n");
printf("Error: %s\n", strerror(errno));
exit(EXIT_FAILURE);
}
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment