Created
April 17, 2019 17:51
-
-
Save diogocapela/fd974e65219aadcffc5bcd93a2145286 to your computer and use it in GitHub Desktop.
Error: No such file or directory
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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