-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsemaphore.c
executable file
·88 lines (71 loc) · 2.63 KB
/
semaphore.c
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <stdio.h>
#include <stdlib.h>
#include <sem182.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <limits.h>
#include <sys/shm.h>
#include "semallg.h"
#define KEY_SEM_0 1
#define KEY_SEM_1 2
#define KEY_MEM 3
#define ANZ_SEM 2
#define START 0
#define END 1
#define SEM_TRUE 1
#define SEM_FALSE 0
typedef struct
{
int sem_art;
int ring_id;
int* p_ring_buffer;
int ring_zeiger;
int semaphore [ANZ_SEM];
char * p_name;
} SHARED_STRUCT;
/* ______________________________TODO_________________________*/
/* die Hauptfunktion */
int sema_function (int argc, char * const argv[],int art);
/* ______________________________TODO_________________________*/
/* aufräumen und frei machen */
static void clean_setfree (SHARED_STRUCT * p_shared_struct);
static int create_sema (SHARED_STRUCT * p_shared_struct, key_t key, long mem_size);
static void error_function (SHARED_STRUCT * p_shared_struct, const char * p_error_message);
/* semaphore erstellen */
static int create_sema (SHARED_STRUCT * p_shared_struct, key_t key, long mem_size){
int value = 0;
value = seminit(key, 0660, mem_size);
/* Fehlerfall..*/
if (value == -1){
/* ..und es gibt eine Fehlernummer..*/
if (errno == EEXIST){
value = semgrab(key);
if (value == -1) {
error_function (p_shared_struct, "semgrab");
}
}
else {
error_function (p_shared_struct, "seminit");
}
}
return value;
}
/* Fehlerbehandlung*/
static void error_function (SHARED_STRUCT* p_shared_struct, const char * p_error_message) {
/* wenn es einen Fehler gibt.. */
if ((p_shared_struct != NULL) && (p_error_message != NULL))
{
/* ..und es eine errno dazu auch gibt */
if (errno != 0) {
fprintf(stderr, "Usage: %s: error %s - %s \n", p_shared_struct->p_name, p_error_message , strerror(errno));
}
else
{
fprintf(stderr, "Usage: %s: error %s \n", p_shared_struct->p_name, p_error_message);
}
/* aufräumen und freigeben*/
clean_setfree (p_shared_struct);
}
exit(EXIT_FAILURE);
}