-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsemaphores.c
More file actions
executable file
·57 lines (48 loc) · 1.69 KB
/
semaphores.c
File metadata and controls
executable file
·57 lines (48 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include "semaphores.h"
void seminit(int *idsem, int val) {
// Obtenemos un semáforo con permisos de lectura y escritura
int rc = semget(IPC_PRIVATE, 1, IPC_CREAT | 0666);
// Comprobamos si se creó correctamente
if (rc == -1) {
printf("No se pudo obtener el semáforo de parte del sistema\n");
exit(-1);
}
*idsem = rc;
// Le asignamos el valor inicial al semáforo
rc = semctl(*idsem, 0, SETVAL, val);
// Comprobamos si se asigno el valor inicial correctamente
if (rc == -1) {
printf("No se pudo asignar el valor inicial al semáforo\n");
exit(-1);
}
}
void semwait(int idsem) {
// Creamos la estructura que nos permitirá realizar operaciones atómicas
struct sembuf *sops = (struct sembuf *) malloc(sizeof(struct sembuf));
// Se refiere al índice del arreglo de semáforos que será afectado
sops[0].sem_num = 0;
// Se refiere a que le restará uno
sops[0].sem_op = -1;
// Se refiere a que debe esperar a que se realice la operación
sops[0].sem_flg = 0;
// Comprobamos si se realizaron correctamente las operaciones
// especificadas por la estructura
if (semop(idsem, sops, 1) == -1) {
printf("No se pudo decrementar el semáforo\n");
exit(-1);
}
}
void semsignal(int idsem) {
// Creamos la estructura que nos permitirá realizar operaciones atómicas
struct sembuf *sops = (struct sembuf *) malloc(sizeof(struct sembuf));
sops[0].sem_num = 0;
// Se refiere a que le sumará uno
sops[0].sem_op = 1;
sops[0].sem_flg = 0;
// Comprobamos si se realizaron correctamente las operaciones
// especificadas por la estructura
if (semop(idsem, sops, 1) == -1) {
printf("No se pudo incrementar el semáforo\n");
exit(-1);
}
}