-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmotor2.c
More file actions
115 lines (87 loc) · 3.33 KB
/
Copy pathmotor2.c
File metadata and controls
115 lines (87 loc) · 3.33 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
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
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <signal.h>
#include <time.h>
char* format_time(){
time_t rawtime; //Unix and POSIX-compliant systems implement the time_t type as a signed integer (typically 32 or 64 bits wide) which represents the number of seconds since the start
// of the Unix epoch
struct tm * timeinfo; // Structure containing a calendar date and time broken down into its components: tm_sec, tm_min, tm_hour, tm_mday (day pf the month), tm_month, tm_year
char* timedate;
time ( &rawtime ); //time_t time( time_t *second ) This function returns the time since 00:00:00 UTC, January 1, 1970 (Unix timestamp) in seconds.
// If second is not a null pointer, the returned value is also stored in the object pointed to by second.
timeinfo = localtime ( &rawtime ); //struct tm *localtime(const time_t *timer) function that uses the time pointed by timer to fill a tm structure with the values that represent the corresponding local time.
// The value of timer is broken up into the structure tm and expressed in the local time zone.
// AKA Convert time_t to tm as local time (function ) -> the inverse function is mktime
timedate = asctime(timeinfo); //char *asctime(const struct tm *timeptr); The asctime function returns a pointer to a null-terminated string that is of the form:
// Fri Feb 15 14:45:01 2013\n
return timedate;
}
void sig_handler(int sig_number)
{
if (sig_number == SIGUSR1){
printf("\nMOTOR2: received SIGUSR1 from the watchdog -> RESET\n");
}
}
int main(int argc, char * argv[])
{
int num;
//REGISTER THE SIGUSR1 SIGNAL
if (signal(SIGUSR1, sig_handler)== SIG_ERR){
printf("\nError in catching the signal\n");
}
//VARIABLES TO PRINT THE PID ON THE pids.txt FILE
pid_t pid;
FILE * fp1;
//LOOP VARIABLES
int n_iter1=0;
int n_iter2=0;
//VARIABLES TO PRINT THE ACTION ON THE LOG.txt FILE
char *action_time;
FILE * fp2;
//PRINT THE NAME OF THE KONSOLE
num=atoi(argv[1]);
printf("motor %d\n", num);
fflush(stdout);
//RETRIEVE THE PID OF THE PROCESS
pid=getpid();
printf("my pid is:%d\n\n", pid);
fflush(stdout);
sleep(1);
//PRINT ON pids.txt THE PID OF THIS PROCESS
fp1 = fopen("pids.txt", "a");
fprintf(fp1, "%d\n", pid);
fclose(fp1);
//ACTION OF THE MOTOR AND PRINT OF THE LOG
while(1){
n_iter1 = n_iter1+1;
printf("MOTOR2: %d-th trip\n", n_iter1);
fflush(stdout);
if(n_iter1<=10){
printf("mamm't\n");
fflush(stdout);
action_time = format_time();
printf("MOTOR2: Action current local time and date: %s\n", action_time); //action time already contains a \n, so here there are two \n
fflush(stdout);
n_iter2 = n_iter2+1;
if(n_iter2==5){
fp2 = fopen("LOG.txt", "a");
fprintf(fp2, "MOTOR2: doing nothing as always at %s",action_time);
fclose(fp2);
n_iter2=0;
}
sleep(3);
}
else{
printf("MOTOR2: goin' to sleep\n\n");
fflush(stdout);
sleep(60);
n_iter1=0;
}
}
exit (0);
}