Question: Challenge Task: Collaborative Prime Number Finder Objective Implement a collaborative prime number finder using shared memory and message queues. This task will demonstrate your understanding

Challenge Task: Collaborative Prime Number Finder
Objective
Implement a collaborative prime number finder using shared memory and message queues. This task will demonstrate your understanding of both IPC mechanisms covered in this lab.
Requirements
1. Create two separate programs: prime_finder.c and message_reporter.c.
2. Use shared memory to store the list of prime numbers found.
3. Use a message queue for notifying the message reporter about newly found primes.
4. Implement proper error handling for all IPC operations.
5. Focus on correctly implementing the IPC mechanisms rather than optimizing the primefinding algorithm. Use the following prime-checking function in your implementation:
```
#include
bool is_prime(int n){
if (n =1) return false;
for (int i =2; i * i = n; i++){
if (n % i ==0) return false;
}
return true;
}
```
Detailed Implementation Guide
Prime Finder Process (prime_finder. c)
1. Set up shared memory:
- Use shmget () to create a shared memory segment (suggest 1024 bytes).- Use shmat () to attach to the shared memory.
2. Set up message queue:
- Use msgget () to create a message queue.
3. Define a structure for the shared memory:
```
#define MAX_PRIMES 20// Assuming we'lL store up to 20 prime numbers
struct shared_data {
int count;
int primes[MAX_PRIMES];
};
```
4. Define a structure for the message queue:
```
struct msg_buffer {
long mitype;
int prime;
};
```
5. Main loop:
- Iterate through numbers from 2 to 20.
- For each number, check if it's prime using is_prime ().
- If it's prime:
- Add it to the shared memory.
- Send a message to the message queue containing the prime number found.
6. Cleanup:
- Use shmolt () to detach from shared memory.
Message Reporter Process (message_reporter.c)
1. Set up shared memory:
- Use shmget () to access the existing shared memory segment.
- Use shmat () to attach to the shared memory.
2. Set up message queue:
- Use msgget () to access the existing message queue.
3. Main loop:
- Use msgrev () to receive messages from the queue.
- After receiving a message:
- Print the received prime number.
- Read and print all primes from shared memory.
4. Cleanup:
- Use shmod () to detach from shared memory.
- Use shmetl () with IPC_RMID to remove the shared memory segment.
- Use msgctl() with IPC_RMID to remove the message queue.
Input and Output - Input: None (the program generates its own input by checking numbers from 2 to 20).
- Output:
- Prime numbers received via the message queue.
- All prime numbers stored in shared memory after each message.
Example Output
```
Message received: Prime found: 2
Primes in shared memory: 2
Message received: Prime found: 3
Primes in shared memory: 2,3
Message received: Prime found: 5
Primes in shared memory: 2,3,5
Message received: Prime found: 7
Primes in shared memory: 2,3,5,7
Message received: Prime found: 11
Primes in shared memory: 2,3,5,7,11
```
Message received: Prime found: 13
Primes in shared memory: \(2,3,5,7,11,13\)
Message received: Prime found: 17
Primes in shared memory: \(2,3,5,7,11,13,17\)
Message received: Prime found: 19
Primes in shared memory: \(2,3,5,7,11,13,17,19\)
Hints and Tips
1. Use the same key for both shared memory and message queue in both processes.
2. Ensure proper error handling for all system calls.
3. The message reporter should only read from shared memory after receiving a message, ensuring the prime finder has written something.
4. Remember to compile each program separately and run them in separate terminal windows.
Evaluation Criteria
1. Correct implementation of shared memory for storing prime numbers.
2. Proper use of message queue for notifying about new primes.
3. Correct reading and displaying of primes from both message queue and shared memory.
4. Proper cleanup of IPC resources.
5. Appropriate error handling.
6. Clear and organized code structure.
Challenge Task: Collaborative Prime Number Finder

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!