Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Problem 1: synchronization among processes The codebase consists of two .cpp files: p1.cpp and p1-helper.cpp. We should only work on p1.cpp. The program takes one

Problem 1: synchronization among processes

  1. The codebase consists of two .cpp files: p1.cpp and p1-helper.cpp. We should only work on p1.cpp.

  2. The program takes one command line argument, which is the number of child processes to fork. The input is provided as follows:

    ./p1 -n 5 // launch 5 child processes

  3. Once launched, each child process should use execvp to switch to p1-helper -- which simply prints two lines.

  4. However, the prints should be in a specific order. First the parent process should print the child pids in sorted order. Then the child process should print such that the process with larger pid prints first. Sample ouput for the input above:

 28769 28770 28771 28772 28773 Process 28773: hello Process 28773: exiting Process 28772: hello Process 28772: exiting Process 28771: hello Process 28771: exiting Process 28770: hello Process 28770: exiting Process 28769: hello Process 28769: exiting Parent: exitin 
  1. Hints and restrictions:
    • Take a look at the usage of raise and kill system calls.
    • You should not change the p1-helper; all prints for the child processes should come from p1-helper.
    • You can use std::sort to sort the child process ids.
    • Note that sleep does not guarantee the desired synchronization.

Problem 2:

Goal: bidirectional communication with anonymous pipes

  1. The codebase consists of a single .cpp file: p2.cpp.

  2. The program should have the following functionalities:

    • Maintain two processes (P1 and P2) and communicate between them with anonymous pipes.
    • Send a message from P1 (of upto 256 bytes) to P2 using pipes.
    • Once P2 receives the message, it will use a hash function to compute hash of the message. The seed for the hash function should be the pid of P2.
    • P2 then sends back the computed hash to P1 using pipes.
    • P1 reads the hash from pipe, computes its own version of the hash (using pid of P2 as seed as well), and prints both.
    • Both processes exit.
  3. The program takes one command line argument -- a string to pass from P1 to P2.

    ./p2 "hello world" // "hello world" should be sent from P1 to P2

  4. Sample output should just print the two hash values in hex. All other prints should be removed for the autograder to function.

  5. Hint:

    • Check read/write functions used in FIFORequestChannel.

p1.cpp:

#include
#include
#include
#include
int main(int argc, char** argv) {
int n = 1, opt;
while ((opt = getopt(argc, argv, "n:")) != -1) {
switch (opt) {
case 'n':
n = atoi(optarg);
break;
}
}
/*
1. TODO: fork n child processes and run p1-helper on each using execvp
> note: we need to synchronize the child processes to print in the desired order
> note: sleep(..) does not guarantee desired synchronization
> note: check "raise" system call
> note: check "WUNTRACED" flag for "waitpid"
*/
/*
2. TODO: print children pids
*/
fflush(stdout); // DO NOT REMOVE: ensures the first line prints all pids
/*
3. TODO: signal children with the reverse order of pids
> note: take a look at "kill" system call
*/
printf("Parent: exiting ");
return 0;
}

p2.cpp:

#include
#include
#include
#include
#include
#define MAX_MESSAGE 256
long long unsigned int hash(int seed, char* buf, int nbytes) {
long long unsigned int H = seed;
for (int i = 0; i < nbytes; ++i)
H = H * 2 * buf[i] + 1;
return H;
}
int main(int argc, char** argv) {
// TODO: create pipe
int pid = fork();
if (pid == 0) {
// TODO: read from parent
// TODO: compute hash
long long unsigned int h = hash(/*provide pid here*/, buf, bytes);
// TODO: send hash to parent
}
else {
// TODO: write to child
// TODO: get hash from child
long long unsigned int hrecv;
// TODO: calculate hash on parent side
// print hashes; DO NOT change
printf("%llX ", h);
printf("%llX ", hrecv);
}
return 0;
}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Bond Markets Analysis And Strategies

Authors: Frank J Fabozzi

8th Edition

013274354X, 9780132743549

More Books

Students also viewed these Finance questions