Question: Q 1 . Create a parent - child relationship between two processes. The parent should print two statements: A ) Parent ( P ) is

Q1. Create a parent-child 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 statement/printf as you desire and their order of execution does not matter.
Q2. Create a parent-child 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
Q3. Write a program to create two child process. The parent process should wait for both the child to finish.
Q4. Create two child process C1 and C2. Make sure that only C2 becomes an orphan process.
11
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: pid_t wait(int *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 waitpid()-allows more
control, such as waiting for a specific child process or non-blocking waits.
8
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.
9
Wait() Program-1
#include
#include
#include
#include
int main()
{
pid_t p;
printf("before fork
");
p=fork();
if(p==0)//child
{
printf("I am child having id
%d
",getpid());
printf("My parent's id is
%d
",getppid());
}
else//parent
{
wait(NULL);
printf("My child's id is
%d
",p);
printf("I am parent having id
%d
",getpid());
}
printf("Common
");
}10
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 wait(NULL).
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 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!