$29
You have 3 processes running that are producing data by writing that data to 3 separate files. In our case, the source for the test processes are provided below. Note that each process produces data at a different rate.
/************************************************/
/* Produces an even value once every 2 seconds. */
/************************************************/
program 1 #include <stdio.h #include <stdlib.h #include <string.h #include <unistd.h int main(void) { int i;
char number; FILE *output;
while (1) {
for (i = 0; i < 10; i+=2) {
semaphore
output = fopen("./output1.txt", "w");
fprintf(output, "%d\n", i);
fclose(output);
semaphore sleep(2);
}
}
return 1;
}
/************************************************/
/* Produces an odd value once every 30 seconds. */
/************************************************/
program 2 #include <stdio.h #include <stdlib.h #include <string.h #include <unistd.h int main(void) { int i;
char number; FILE *output;
while (1) {
for (i = 1; i < 10; i+=2) {
semaphore
output = fopen("./output2.txt", "w");
fprintf(output, "%d\n", i);
fclose(output);
semaphore sleep(30);
}
}
return 1;
}
/************************************************/
/* Produces a character once every 120 seconds. */
/************************************************/
program 3 #include <stdio.h
h
#include <stdlib.h
#include <string.h
#include <unistd.h
int main(void) {
int i;
char number;
FILE *output;
while (1) {
for (i = 58; i < 127; i++) {
// semaphore
output = fopen("./output3.txt", "w");
fprintf(output, "%c\n", i);
fclose(output);
semaphore sleep(120);
}
}
return 1;
}
Your job is to create a fourth program that samples the 3 output files at 0.5 second intervals (the Nyquist sample rate � ie twice the highest production rate) and produces an new output (output4.txt) that is a combination of the 3 data streams. There are several problems, however:
1. Program 4 MUST use three threads to monitor the 3 producers'
output files and you MUST assume that on occasion any output file may be unreadable as it is being written to by the producer (PS This is why we sample at twice the production rate).
You must compile the resulting data such that it appears in order (data from output1.txt, data from output2.txt, and data from output3.txt) and write that data to output4.txt such that a history of all data generated is provided in output4.txt (i.e. output4.txt cannot just contain the most recent dataset). In addition, output4.txt cannot contain more than two consecutive duplicated data (the Nyquist sampling could produce two consecutive duplicated data).
Obviously, we have a synchronization problem and a critical section problem. Your program should only add to output4.txt when it has the most recent data. You can use any critical section management algorithm you want to synchronize the three inputs with the output. You must use an array of Unix semaphores as you must be able to synchronize the 4 processes with a single atomic semaphore (thus the array of semaphores ‐ I STRONGLY suggest you READ the man page on semaphores). You can ADAPT anyone of the 2‐process critical section methods to this problem as long you understand that you must manage 4 processes.
A script will be used that started the programs in reverse order. 4, 3, 2, 1.
Notes:
‐‐‐‐‐‐
Only the contents of the three producers will change when we test your code. The data generation rate and output filenames will remain the same.
Once you start your threads you cannot control them with pthread_join.
You MUST use Unix semaphores (see semget, etc)!
REQUIREMENTS:
‐‐‐‐‐‐‐‐‐‐‐‐‐
1. Your program must run in Streibel 109.
Your full name must appear as a comment at the beginning of your program.
Your source code must be named hw8‐yourname.1.c, hw8‐yourname.2.c, etc.
Email your source (subject hw8‐yourname) to rmarsh@cs.und.edu