-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfunc.c
127 lines (97 loc) · 2.29 KB
/
func.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
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <semaphore.h>
#define _GNU_SOURCE
#include <signal.h>
#include <poll.h>
#include "defs.h"
#include "server.h"
#include "curses-utils.h"
volatile sig_atomic_t flag_alarm;
volatile sig_atomic_t flag_race_stop;
int try_sys_call_int(int syscall_ret, char* msg_on_fail) {
if (syscall_ret != -1) return syscall_ret;
perror(msg_on_fail);
exit(EXIT_FAILURE);
}
void* try_sys_call_ptr(void* syscall_ret, char* msg_on_fail) {
if (syscall_ret != NULL) return syscall_ret;
perror(msg_on_fail);
exit(EXIT_FAILURE);
}
sem_t* sem;
void init_semaphore(){
sem_unlink ("pSem");
sem = sem_open ("pSem", O_CREAT | O_EXCL, 0644, 1);
// 1 = Only 1 process at a time doing action on shm
}
/**
* Flush a pipe and returns data availabilty
* @param pipe the pipe descriptor
* @return 0 if no data were available, anything else if there was something on the pipe
*/
int flush_pipe(int pipe){
char buffer[256];
if (poll(&(struct pollfd){ .fd = pipe, .events = POLLIN }, 1, 0)==1) {
while(1){
if (poll(&(struct pollfd){ .fd = pipe, .events = POLLIN }, 1, 0)==1) {
read(pipe, &buffer, sizeof(buffer));
}else{
break;
}
}
return 1;
}else{
return 0;
}
}
int compare_rank_item(const void* a, const void* b){
rank_item* ra = (rank_item*) a;
rank_item* rb = (rank_item*) b;
return ra->bestlap - rb->bestlap;
}
int compare_pilotes(const void* a, const void* b){
pilote* ra = (pilote*) a;
pilote* rb = (pilote*) b;
if(ra->lap_cnt == rb->lap_cnt){
if(ra->sector == rb->sector){
return rb->time - ra->time;
}else{
return rb->sector - ra->sector;
}
}else{
return rb->lap_cnt - ra->lap_cnt;
}
}
int compare_pilote_position(const void* a, const void* b){
pilote* ra = (pilote*) a;
pilote* rb = (pilote*) b;
return ra->position - rb->position;
}
void sighandler(int sig) {
switch (sig) {
case SIG_RACE_STOP:
flag_race_stop = 1;
break;
case SIGALRM:
flag_alarm = 1;
break;
}
}
float doSector(pilote* p, float time, int pipe){
p->time = time;
if(p->status != pitstop){
if(++(p->sector) > 3){
p->sector = 0;
p->lap_cnt++;
}
}
p->has_changed = 1;
char status[] = "driving";
write(pipe, status, sizeof(status));
return p->time;
}