-
Notifications
You must be signed in to change notification settings - Fork 0
/
pr.cpp
193 lines (154 loc) · 4.5 KB
/
pr.cpp
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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <string.h>
//file location stored on a shareable directory.
const char * chatFileLocation = "/tmp/chat.txt";
//store other process's pid.
int otherPrId = 0;
//a flag variable to check if we received
//other process's pid from the chat file on startup.
bool otherPrIdflag = false;
//an integer variable to store the process type.
//on startup set this to 1 or 2.
int type = 0;
bool readFileByLine(const char *fileLocation);
bool readFileExtractPid(const char *fileLocation);
void writeFile(const char *fileLocation, char text[]);
void writeFile(const char *fileLocation, long pid);
static void sigint(int sig);
static void sigusr(int sig);
void onExit(){
remove(chatFileLocation);
printf("Terminated.\n %s is deleted.", chatFileLocation);
fflush(stdout);
}
int main(int argc, char *argv[])
{
if(argc < 2){
printf("Usage: %s [1 or 2] \n", argv[0]);
return 0;
}
if(atoi(argv[1]) == 1){
type = 1;
if (signal(SIGUSR1, sigusr) == SIG_ERR){
printf("Unable to create handler for SIGUSR1\n");
return 0;
}
}else if(atoi(argv[1]) == 2){
type = 2;
if (signal(SIGUSR2, sigusr) == SIG_ERR){
printf("Unable to create handler for SIGUSR2\n");
return 0;
}
}
//delete chat file if interrupt signal is received
if (signal(SIGINT, sigint) == SIG_ERR){
printf("Unable to create handler for SIGINT\n");
printf("Please manually delete %s upon termination.", chatFileLocation);
}
//ensuring chat file deletion upon exit
atexit(onExit);
//self signaling to initiate pid sharing
sigusr(10);
//infinite while loop to keep the thread active
while(1){
//pause will end when this process receives a signal
pause();
}
return 0;
}
bool readFileByLine(const char *fileLocation){
FILE *fpr;
//printf("readFileByLine");
fpr = fopen(fileLocation, "r");
if (fpr == NULL){
return false;
}
char str[1024];
fgets(str, 1024, fpr);
printf("Message received: %s \n", str);
fflush(stdout);
//reallocate memory space - not necessarily needed but
//it is a good practice
memset(str, 0, sizeof str);
return true;
}
bool readFileExtractPid(const char *fileLocation){
FILE *fpr;
//printf("readFileExtractPid ");
fpr = fopen(chatFileLocation, "r");
if (fpr == NULL){
return false;
}
char str[1024];
fgets(str, 1024, fpr);
//convert string to integer
otherPrId = atoi(str);
return true;
}
void writeFile(const char *fileLocation, char text[]){
FILE *fpw;
//printf("writeFile char text");
fpw = fopen(fileLocation, "w");
fputs(text, fpw);
fclose(fpw);
}
void writeFile(const char *fileLocation, long pid){
FILE *fpw;
//printf("writeFile long pid %ld", pid);
fpw = fopen(chatFileLocation, "w");
fprintf(fpw, "%ld", pid);
fclose(fpw);
}
void sigint(int sig){
remove(chatFileLocation);
printf("Terminated.\n %s is deleted.", chatFileLocation);
fflush(stdout);
exit(0);
}
void sigusr(int sig){
//check if we did not store other process's pid
if(otherPrIdflag == false){
//if file does not exist, just create one and write own pid.
//if file exists assume the file contains other process's pid.
if(!readFileExtractPid(chatFileLocation)){
return writeFile(chatFileLocation, (long)getpid());
}
otherPrIdflag = true;
writeFile(chatFileLocation, (long)getpid());
if(type == 1){
kill(otherPrId, SIGUSR2);
}else if(type == 2){
kill(otherPrId, SIGUSR1);
}else{
printf("Internal error. Empty type variable.");
fflush(stdout);
exit(0);
}
return;
}
if(!readFileByLine(chatFileLocation)){
return;
}
//get input from user
char input[1024];
printf("Enter: ");
fflush(stdout);
fgets(input, sizeof input, stdin);
//write the input recived to the shared file
writeFile(chatFileLocation, input);
//reallocate memory for line array
memset(input, 0, sizeof input);
//signal the other process
if(type == 1){
kill(otherPrId, SIGUSR2);
}else if(type == 2){
kill(otherPrId, SIGUSR1);
}else{
printf("Internal error. Empty type variable.");
fflush(stdout);
exit(0);
}
}