Question: Q 1 . Create a parent - child relationship between two processes. The parent should print two statements: A ) Parent ( P ) is
Q Create a parentchild relationship between two processes. The parent should print two statements:
A Parent P is having ID
B ID of Ps Child is
The child should print two statements:
C Child is having ID
D My Parent ID is
Make use of wait in such a manner that the order of the four statements A B C and D is:
A
C
D
B
You are free to use any other relevant statementprintf as you desire and their order of execution does not matter.
Q Create a parentchild relationship between two processes such that the Child process creates a file named Relation.txt and the Parent process write some content into it by taking the input from the user
Q Write a program to create two child process. The parent process should wait for both the child to finish.
Q Create two child process C and C Make sure that only C becomes an orphan process.
give me the correct code with the output. using these concepts.
Wait
When a process creates a child process, sometimes it becomes necessary that the parent process
should execute only after the child has finished. wait system call does exactly this. It makes the
parent process wait for child process to finish and then the parent continues its working from the
statement after the wait
Syntax: pidt waitint wstatus;
Status: A pointer to an integer where the exit status of the child process is stored. This can be used to
determine how the child process terminated
wait vs waitpid: wait waits for any child process to terminate, while waitpidallows more
control, such as waiting for a specific child process or nonblocking waits.
Wait
Synchronizing Processes: Use wait to synchronize the parent process with
its child processes. This is particularly important if the parent needs to
perform actions based on the completion of its child processes.
Avoiding Zombie Processes: When a child process terminates, it still
occupies an entry in the process table until the parent reads its exit status. If
the parent does not call wait these terminated processes become "zombie"
processes. Calling wait ensures that the child processes are properly cleaned
up
Resource Management: By waiting for child processes to terminate, you can
manage system resources more efficiently, as it prevents the accumulation of
zombie processes which can consume system resources and affect
performance.
Wait Program
#include
#include
#include
#include
int main
pidt p;
printfbefore fork
;
pfork;
ifpchild
printfI am child having id
d
getpid;
printfMy parent's id is
d
getppid;
elseparent
waitNULL;
printfMy child's id is
d
p;
printfI am parent having id
d
getpid;
printfCommon
;
Working
The execution begins by printing before fork Then fork system call creates a child
process.
wait system call is added to the parent section of the code. Hence, the moment processor
starts processing the parent, the parent process is suspended because the very first
statement is waitNULL
Thus, first, the child process runs, and the output lines are all corresponding to the child
process. Once the child process finishes, parent resumes and prints all its printf
statements. The NULL inside the wait means that we are not interested to know the
status of change of state of child process
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
