
Julie D. answered 04/10/25
Foundations of Computer Networking
Download the Dataset: Get the text files from the specified Google Drive URL.
create a c program
Spawn two child processes.
Use shared memory to communicate between processes
Each process reads one file and computes average ratings.
download the files using a browser, or you can use command-line tools like wget or curl.
For example, using wget:
wget --no-check-certificate 'https://drive.google.com/uc?export=download&id=1fgJqOeWbJC4ghMKHkuxfIP6dh2F911-E' -O ratings.txt
Include Required Libraries:
<stdio.h>, <stdlib.h>, <string.h> for standard functions.
<unistd.h>, <sys/types.h>, <sys/wait.h> for process control.
<sys/ipc.h>, <sys/shm.h> for shared memory.
Create Shared Memory: Use shmget() to create shared memory space for storing the total ratings and count.
Spawn Processes: Use fork() to create child processes.
Read Files and Compute Averages:
Each process should read its respective file.
Calculate the total rating and the number of ratings.
Communicate Results Using Shared Memory: Store results in shared memory and ensure synchronization using appropriate mechanisms.
Example C Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#define FILE1 "ratings1.txt"
#define FILE2 "ratings2.txt"
#define SHM_KEY 1234
int main() {
// Shared memory to hold total ratings and counts
int shmid;
float *shared_data;
// Allocate shared memory
shmid = shmget(SHM_KEY, 2 * sizeof(float), IPC_CREAT | 0666);
if (shmid < 0) {
perror("shmget");
exit(1);
}
shared_data = (float*)shmat(shmid, NULL, 0);
shared_data[0] = 0; // Total rating for process 1
shared_data[1] = 0; // Count for process 1
// Forking Process
pid_t pid1 = fork();
if (pid1 == 0) {
// Child process 1 reads FILE1
FILE *file1 = fopen(FILE1, "r");
if (!file1) {
perror("Opening file1");
exit(1);
}
float rating;
int count = 0;
while (fscanf(file1, "%*d %*d %f %*s", &rating) != EOF) {
shared_data[0] += rating; // Accumulate total rating
count++;
}
fclose(file1);
shared_data[1] = count; // Set count
exit(0);
}
pid_t pid2 = fork();
if (pid2 == 0) {
// Child process 2 reads FILE2
FILE *file2 = fopen(FILE2, "r");
if (!file2) {
perror("Opening file2");
exit(1);
}
float rating;
int count = 0;
while (fscanf(file2, "%*d %*d %f %*s", &rating) != EOF) {
shared_data[0] += rating; // Accumulate total rating
count++;
}
fclose(file2);
shared_data[1] += count; // Add this process's count
exit(0);
}
// Wait for both child processes to finish
wait(NULL);
wait(NULL);
// Calculate overall average
float average = shared_data[0] / shared_data[1];
printf("Overall Average Rating: %.2f\n", average);
// Detach and remove shared memory
shmdt(shared_data);
shmctl(shmid, IPC_RMID, NULL);
return 0;
}
Code Explained:
Shared Memory: The shared memory segment is created using shmget() and mapped into the process address space with shmat().
Child Processes:
Each child reads its respective file (you'll need two separate files named ratings1.txt and ratings2.txt).
The first value in shared_data stores the cumulative rating, while the second stores the total count.
Synchronization: The main process waits for both child processes to complete.
Average Calculation: After the processes finish, the main process calculates and prints the overall average rating.
Clean-Up: Shared memory is detached and removed after use.