Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hi, I have a question regarding operating systems. I would appreciate any help with this. Thank you so much! You had the child process generate

Hi, I have a question regarding operating systems. I would appreciate any help with this. Thank you so much!

You had the child process generate and output the sequence in the below code because the parent and child did not share any memory. Modify this program so that the child process writes the Collatz sequence to the shared memory segment. The parent process will invoke wait() to wait for the child to exit, and the parent will then output the sequence from the shared memory segment. After the parent outputs the sequence, have it remove the shared memory segment using the shm_unlink() function.

The parent program will still be invoked as follows (assuming the executable is lab2):

./lab2 

If you think of this in terms of the producer/consumer: the child is the producer process; the parent the consumer.

I suggest the following steps:

The Parent (Consumer) Process:

  • Establish the shared memory object using shm_open(), ftruncate(), and mmap().

  • Name the shared memory segment COLLATZ

  • Create the child process using fork() and wait for it to terminate.

  • Output the contents of the shared memory object after the child has terminated.

  • Remove the shared memory object using shm_unlink().

The Child (Producer) Process:

The shared memory segment is being established in the parent process. Since the child is a copy of the parent, it will have a reference to this shared memory segment. Once the shared memory segment is established, the child process (identified with pid == 0) will write the Collatz sequence to the shared memory object. Once the child process finishes, the parent will read and output the sequence from the shared memory segment.

There are a few suggestions for the child process to write to the shared memory segment:

Create a character array you will write to:

const int STR_SIZE = 128; char str[STR_SIZE];

Assume

int n;

where n represents the current value of the Collatz sequence. To write the value n to the shared memory segment, you would first use

sprintf(str," %d ",n);

which writes the integer value of n to the char array str. You would then write str to the shared memory segment. (Don't forget to increment the pointer to the shared memory segment after each write.)

Debugging

If you find the parent is not outputting the sequence from the shared memory segment, look at the contents of the segment with the command

cat /dev/shm/COLLATZ

which should output the sequence. (***Just be sure to first comment out the call to shm_unlink() which removes the shared memory segment)

STARTING CODE TO WORK WITH:

#include #include #include #include int main(int argc, char *argv[]) { int n = atoi(argv[1]); //converts command line argument from string to an intenger if (fork() == 0) { //fork to create child processes printf("%d ", n); //print Collatz sequence in child process while(n != 1) { n = n%2 ? (3*n+1) : n/2; printf("%d ", n); } printf(" "); } return 0; }

--------------------------------------------------------------------------------------------------------------

#include #include #include #include #include

int main(int argc, char *argv[]) { // Doing fork() for creating child process. if(fork() == 0) { char *args[] = {"./Collatz", argv[1], NULL}; execvp(args[0], args); } else { wait(NULL); } return 0; }

--------------------------------------------------------------------------------------------------------------

#include #include int main(int argc, char *argv[]) {//converts command line argument fron string to integer int n = atoi(argv[1]); //prints Collatz sequence in child process printf("%d ", n); while(n != 1) { n = n%2 ? (3*n+1) : n/2; printf("%d ", n); } printf(" "); 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_2

Step: 3

blur-text-image_3

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

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2014 Nancy France September 15 19 2014 Proceedings Part I Lnai 8724

Authors: Toon Calders ,Floriana Esposito ,Eyke Hullermeier ,Rosa Meo

2014th Edition

3662448475, 978-3662448472

More Books

Students also viewed these Databases questions