-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOSoving1.c
226 lines (196 loc) · 5.17 KB
/
OSoving1.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
//Unix based alarm clock
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<unistd.h>
#include<signal.h>
#include<sys/wait.h>
//alarm struct som skal brukes i array
struct alarm {
int alarmId;
pid_t PID;
time_t ringTime;
} newAlarm;
//alarm ringing
void ringAlarm() {
printf("RING! \n");
#ifdef __APPLE__
execlp("afplay", "afplay", "./nuke.mp3", NULL);
#elif __unix__
execlp("mpg123", "mpg123", "-q", "./nuke.mp3", NULL);
#elif _WIN32
printf("RING! \n");
#endif
}
//alarms
struct alarm alarms[10];
//methods
void cancelAlarm(int index);
int getNumAlarms() {
time_t currentTime;
time(¤tTime);
int num = 0;
for (int i = 0; i<10; i++){
time_t alarmTime = alarms[i].ringTime;
if (alarms[i].ringTime - currentTime > 0) {
num++;
}
}
return num;
}
void actionS() {
time_t currentTime;
time(¤tTime);
struct tm alarm;
char date[250];
int year;
int month;
int day;
printf("enter scheduled date (YYYY-MM-DD): ");
scanf("%s", date);
sscanf(date, "%4d-%2d-%2d", &year, &month, &day);
alarm.tm_year = year - 1900;
alarm.tm_mon = month - 1;
alarm.tm_mday = day;
char timeOfDay[250];
int hour;
int minute;
int second;
printf("enter scheduled time (hh:mm:ss): ");
scanf("%s", timeOfDay);
sscanf(timeOfDay, "%2d:%2d:%2d", &hour, &minute, &second);
alarm.tm_hour = hour;
alarm.tm_min = minute;
alarm.tm_sec = second;
time(¤tTime);
time_t alarmTime = mktime(&alarm);
if (alarmTime - currentTime > 0) {
printf("Scheduling alarm for: %s\n", ctime(&alarmTime));
time(¤tTime); //updates the time
time_t secondsLeft = alarmTime - currentTime;
printf("Scheduling alarm in %ld seconds\n", secondsLeft);
int index = 0;
for (int i = 0; i < 10; i++) {
if (alarms[i].alarmId == 0) {
alarms[i].ringTime = alarmTime;
alarms[i].alarmId = i + 1;
index = i + 1;
int forked = fork();
if (forked == 0) { //fork child
alarms[i].PID = forked;
sleep(secondsLeft);
ringAlarm();
cancelAlarm(index);
printf("next input: ");
exit(EXIT_SUCCESS);
} else {
alarms[i].PID = forked;
}
break;
}
}
} else {
if (getNumAlarms() == 10) {
printf("Too many alarms, remove one to add another");
} else {
printf("Invalid input, try again. \n");
}
}
}
void actionL() {
time_t currentTime;
time(¤tTime);
printf("Your alarms: \n\n");
int num = 0;
for (int i = 0; i<10; i++){
time_t alarmTime = alarms[i].ringTime;
if (alarms[i].ringTime - currentTime > 0) {
printf("%i %s \n",alarms[i].alarmId ,ctime(&alarmTime));
num++;
}
}
if (num == 0) {
printf("You currently don't have any alarms set: \n\n");
}
}
void cancelAlarm(int index) {
for (int i = 0; i < 10; i++) {
if (alarms[i].alarmId == index) {
alarms[i].alarmId = 0;
alarms[i].ringTime = time(NULL);
}
}
}
void actionC() {
int index;
printf("PID of first alarm: %i\n", alarms[0].PID);
printf("enter the number of the alarm you wish to cancel: \n");
scanf("%i", &index);
int deleted = -1;
for (int i = 0; i < 10; i++) {
if (alarms[i].alarmId == index) {
alarms[i].alarmId = 0;
alarms[i].ringTime = time(NULL);
deleted = 1;
kill(alarms[i].PID, SIGKILL);
}
}
if (deleted == -1) {
printf("Alarm does not exist. \n");
} else {
printf("Alarm %i is deleted. \n", index);
}
}
void catchZombies() {
pid_t PID = waitpid(-1, NULL, WNOHANG);
if (PID == 0) {
return;
}
int index = -1;
for (int i = 0; i < getNumAlarms(); i++) {
if (alarms[i].PID == PID) {
index = i;
break;
}
}
if (index==-1) {
return;
}
alarms[index].alarmId = 0;
alarms[index].ringTime = time(NULL);
kill(alarms[index].PID, SIGKILL);
}
void menu() {
time_t rawTime;
struct tm *timeinfo;
time (&rawTime);
timeinfo = localtime (&rawTime);
char action;
printf("Welcome to the alarm clock! It is currently %s . Please enter 's' (schedule), 'l' (list), 'c' (cancel), 'x' (exit) \n", asctime (timeinfo));
while (action != 'x') {
catchZombies(); //makes sure there are no zombies before new input.
scanf(" %c", &action);
if (action == 's') {
printf("your input = %c \nyou can schedule an alarm \n", action);
actionS();
printf("%s", "next input: ");
} else if (action == 'l') {
printf("your input = %c \nyou can display alarms \n" , action);
actionL();
printf("%s", "next input: ");
} else if (action == 'c') {
printf("your input = %c \nyou can cancel alarms \n" , action);
actionC();
printf("%s", "next input: ");
} else {
if (action != 'x') {
printf("wrong input \n");
}
}
}
}
int main(){
menu();
printf("Compiled\n");
return 0;
}