Question: I'm writting using C++ I having hard time to get this part done in my project. Can someone explain to me or give me a

I'm writting using C++ I having hard time to get this part done in my project.

Can someone explain to me or give me a code example to reach the requirement?

Thank you,

C. Fork, wait and system

1. Call fork() to create a new process, which we will call the middle process. Then call fork() again to create two new children.

Each child or middle process will print out the following information, one sentence per line, with a blank line after the full message:

Child: My PID is xxxx.

My parent is yyyy.

Middle: My PID is xxxx.

My parent is yyyy.

My child is wwww.

After printing this information, the child or middle process should sleep for 3 seconds, then print the following message (followed by a blank line) and exit:

Child: I am awake.

Middle: I am awake.

In the initial parent process, print the following message, formatted as above.

Parent: My PID is xxxx.

My parent is yyyy.

My children are wwww and zzzz.

2. Then invoke the command ps f -ppid ... from your program to show all the processes involved. You can use the system() function to do this. After -ppid, you must list the parent process ids of all the processes you want to list. For example:

/bin/ps -f --ppid 26560,26803,26804,26805

See the man entry for ps for more details. Do not use a general ps command here without specifying the parent ids of the processes in question.

Since system() starts another process, you should see 5 processes in the output list (see sample output below).

3. The original parent should wait for all the children to complete, then print the following message:

Parent: All processes finished.

Note that each message is labeled according to the process that printed it: parent, child or middle program

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 Databases Questions!