-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxerrori.c
367 lines (315 loc) · 9.64 KB
/
xerrori.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
#include "xerrori.h"
// collezione di chiamate a funzioni di sistema con controllo output
// i prototipi sono in xerrori.h
// termina un processo con eventuale messaggio d'errore
void termina(const char *messaggio) {
if(errno==0) fprintf(stderr,"== %d == %s\n",getpid(), messaggio);
else fprintf(stderr,"== %d == %s: %s\n",getpid(), messaggio,
strerror(errno));
exit(1);
}
// termina un processo con eventuale messaggio d'errore + linea e file
void xtermina(const char *messaggio, int linea, char *file) {
if(errno==0) fprintf(stderr,"== %d == %s\n",getpid(), messaggio);
else fprintf(stderr,"== %d == %s: %s\n",getpid(), messaggio,
strerror(errno));
fprintf(stderr,"== %d == Linea: %d, File: %s\n",getpid(),linea,file);
exit(1);
}
// ---------- operazioni su FILE *
FILE *xfopen(const char *path, const char *mode, int linea, char *file) {
FILE *f = fopen(path,mode);
if(f==NULL) {
perror("Errore apertura file");
fprintf(stderr,"== %d == Linea: %d, File: %s\n",getpid(),linea,file);
exit(1);
}
return f;
}
// ----------- operazioni su file descriptors
void xclose(int fd, int linea, char *file) {
int e = close(fd);
if(e!=0) {
perror("Errore chiusura file descriptor");
fprintf(stderr,"== %d == Linea: %d, File: %s\n",getpid(),linea,file);
exit(1);
}
return;
}
// -------------- operazioni su processi
pid_t xfork(int linea, char *file)
{
pid_t p = fork();
if(p<0) {
perror("Errore fork");
fprintf(stderr,"== %d == Linea: %d, File: %s\n",getpid(),linea,file);
exit(1);
}
return p;
}
pid_t xwait(int *status, int linea, char *file)
{
pid_t p = wait(status);
if(p<0) {
perror("Errore wait");
fprintf(stderr,"== %d == Linea: %d, File: %s\n",getpid(),linea,file);
exit(1);
}
return p;
}
int xpipe(int pipefd[2], int linea, char *file) {
int e = pipe(pipefd);
if(e!=0) {
perror("Errore creazione pipe");
fprintf(stderr,"== %d == Linea: %d, File: %s\n",getpid(),linea,file);
exit(1);
}
return e;
}
// ---------------- memoria condivisa POSIX
int xshm_open(const char *name, int oflag, mode_t mode, int linea, char *file)
{
int e = shm_open(name, oflag, mode);
if(e== -1) {
perror("Errore shm_open");
fprintf(stderr,"== %d == Linea: %d, File: %s\n",getpid(),linea,file);
exit(1);
}
return e;
}
int xshm_unlink(const char *name, int linea, char *file)
{
int e = shm_unlink(name);
if(e== -1) {
perror("Errore shm_unlink");
fprintf(stderr,"== %d == Linea: %d, File: %s\n",getpid(),linea,file);
exit(1);
}
return e;
}
int xftruncate(int fd, off_t length, int linea, char *file)
{
int e = ftruncate(fd,length);
if(e== -1) {
perror("Errore ftruncate");
fprintf(stderr,"== %d == Linea: %d, File: %s\n",getpid(),linea,file);
exit(1);
}
return e;
}
void *simple_mmap(size_t length, int fd, int linea, char *file)
{
void *a = mmap(NULL, length ,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0);
if(a == (void *) -1) {
perror("Errore mmap");
fprintf(stderr,"== %d == Linea: %d, File: %s\n",getpid(),linea,file);
exit(1);
}
return a;
}
int xmunmap(void *addr, size_t length, int linea, char *file)
{
int e = munmap(addr, length);
if(e== -1) {
perror("Errore munmap");
fprintf(stderr,"== %d == Linea: %d, File: %s\n",getpid(),linea,file);
exit(1);
}
return e;
}
// ---- semafori POSIX
// IMPORTANTE: i semafori posix sono usati sia da processi che da threads
// Nel caso dei threads, non è opportuno eseguire in caso di errore exit(1)
// in quanto questo fa terminare tutti i thread del processo: bisognerebbe
// chiamare pthread_exit() che fa terminare solo il thread corrente
// (usare pthread_exit per i processi ugualmente non è accettabile
// perché poi invoca exit(0)).
// Si potrebbe distinguere thread da processi con gettid(2)
// semafori NAMED
sem_t *xsem_open(const char *name, int oflag, mode_t mode,
unsigned int value, int linea, char *file) {
sem_t *s = sem_open(name,oflag,mode,value);
if (s==SEM_FAILED) {
perror("Errore sem_open");
fprintf(stderr,"== %d == Linea: %d, File: %s\n",getpid(),linea,file);
exit(1);
}
return s;
}
int xsem_close(sem_t *s, int linea, char *file)
{
int e = sem_close(s);
if(e!=0) {
perror("Errore sem_close");
fprintf(stderr,"== %d == Linea: %d, File: %s\n",getpid(),linea,file);
exit(1);
}
return e;
}
int xsem_unlink(const char *name, int linea, char *file)
{
int e = sem_unlink(name);
if(e!=0) {
perror("Errore sem_unlink");
fprintf(stderr,"== %d == Linea: %d, File: %s\n",getpid(),linea,file);
exit(1);
}
return e;
}
// semafori UNNAMED
int xsem_init(sem_t *sem, int pshared, unsigned int value, int linea, char *file) {
int e = sem_init(sem,pshared,value);
if(e !=0) {
perror("Errore sem_init");
fprintf(stderr,"== %d == Linea: %d, File: %s\n",getpid(),linea,file);
exit(1);
}
return e;
}
int xsem_destroy(sem_t *sem, int linea, char *file) {
int e = sem_destroy(sem);
if(e !=0) {
perror("Errore sem_destroy");
fprintf(stderr,"== %d == Linea: %d, File: %s\n",getpid(),linea,file);
exit(1);
}
return e;
}
// comuni NAMED e UNNAMED
int xsem_post(sem_t *sem, int linea, char *file) {
int e = sem_post(sem);
if(e !=0) {
perror("Errore sem_post");
fprintf(stderr,"== %d == Linea: %d, File: %s\n",getpid(),linea,file);
exit(1);
}
return e;
}
int xsem_wait(sem_t *sem, int linea, char *file) {
int e = sem_wait(sem);
if(e !=0) {
perror("Errore sem_wait");
fprintf(stderr,"== %d == Linea: %d, File: %s\n",getpid(),linea,file);
exit(1);
}
return e;
}
// ----- funzioni per thread: in caso di errore non scrivono
// il codice d'errore in errno ma lo restituiscono
// come return value. Un return value==0 indica nessun errore
// errno viene evitato perché in certe implementazioni
// non è thread-safe (nei linux recenti lo è)
// stampa il messaggio d'errore associato al codice en
// in maniera simile a perror
#define Buflen 100
void xperror(int en, char *msg) {
char buf[Buflen];
char *errmsg = strerror_r(en, buf, Buflen);
if(msg!=NULL)
fprintf(stderr,"%s: %s\n",msg, errmsg);
else
fprintf(stderr,"%s\n",errmsg);
}
// threads: creazione e join
int xpthread_create(pthread_t *thread, const pthread_attr_t *attr,
void *(*start_routine) (void *), void *arg, int linea, char *file) {
int e = pthread_create(thread, attr, start_routine, arg);
if (e!=0) {
xperror(e, "Errore pthread_create");
fprintf(stderr,"== %d == Linea: %d, File: %s\n",getpid(),linea,file);
pthread_exit(NULL);
}
return e;
}
int xpthread_join(pthread_t thread, void **retval, int linea, char *file) {
int e = pthread_join(thread, retval);
if (e!=0) {
xperror(e, "Errore pthread_join");
fprintf(stderr,"== %d == Linea: %d, File: %s\n",getpid(),linea,file);
pthread_exit(NULL);
}
return e;
}
// mutex
int xpthread_mutex_init(pthread_mutex_t *restrict mutex, const pthread_mutexattr_t *restrict attr, int linea, char *file) {
int e = pthread_mutex_init(mutex, attr);
if (e!=0) {
xperror(e, "Errore pthread_mutex_init");
fprintf(stderr,"== %d == Linea: %d, File: %s\n",getpid(),linea,file);
pthread_exit(NULL);
}
return e;
}
int xpthread_mutex_destroy(pthread_mutex_t *mutex, int linea, char *file) {
int e = pthread_mutex_destroy(mutex);
if (e!=0) {
xperror(e, "Errore pthread_mutex_destroy");
fprintf(stderr,"== %d == Linea: %d, File: %s\n",getpid(),linea,file);
pthread_exit(NULL);
}
return e;
}
int xpthread_mutex_lock(pthread_mutex_t *mutex, int linea, char *file) {
int e = pthread_mutex_lock(mutex);
if (e!=0) {
xperror(e, "Errore pthread_mutex_lock");
fprintf(stderr,"== %d == Linea: %d, File: %s\n",getpid(),linea,file);
pthread_exit(NULL);
}
return e;
}
int xpthread_mutex_unlock(pthread_mutex_t *mutex, int linea, char *file) {
int e = pthread_mutex_unlock(mutex);
if (e!=0) {
xperror(e, "Errore pthread_mutex_unlock");
fprintf(stderr,"== %d == Linea: %d, File: %s\n",getpid(),linea,file);
pthread_exit(NULL);
}
return e;
}
// condition variables
int xpthread_cond_init(pthread_cond_t *restrict cond, const pthread_condattr_t *restrict attr, int linea, char *file) {
int e = pthread_cond_init(cond,attr);
if (e!=0) {
xperror(e, "Errore pthread_cond_init");
fprintf(stderr,"== %d == Linea: %d, File: %s\n",getpid(),linea,file);
pthread_exit(NULL);
}
return e;
}
int xpthread_cond_destroy(pthread_cond_t *cond, int linea, char *file) {
int e = pthread_cond_destroy(cond);
if (e!=0) {
xperror(e, "Errore pthread_cond_destroy");
fprintf(stderr,"== %d == Linea: %d, File: %s\n",getpid(),linea,file);
pthread_exit(NULL);
}
return e;
}
int xpthread_cond_wait(pthread_cond_t *restrict cond, pthread_mutex_t *restrict mutex, int linea, char *file) {
int e = pthread_cond_wait(cond,mutex);
if (e!=0) {
xperror(e, "Errore pthread_cond_wait");
fprintf(stderr,"== %d == Linea: %d, File: %s\n",getpid(),linea,file);
pthread_exit(NULL);
}
return e;
}
int xpthread_cond_signal(pthread_cond_t *cond, int linea, char *file) {
int e = pthread_cond_signal(cond);
if (e!=0) {
xperror(e, "Errore pthread_cond_signal");
fprintf(stderr,"== %d == Linea: %d, File: %s\n",getpid(),linea,file);
pthread_exit(NULL);
}
return e;
}
int xpthread_cond_broadcast(pthread_cond_t *cond, int linea, char *file) {
int e = pthread_cond_broadcast(cond);
if (e!=0) {
xperror(e, "Errore pthread_cond_broadcast");
fprintf(stderr,"== %d == Linea: %d, File: %s\n",getpid(),linea,file);
pthread_exit(NULL);
}
return e;
}