wait() command for synchronization
Aim:
To write a program that creates a new process using the fork() system call.
The child process should print the string "PCCSL407", and the parent process should print "Operating Systems Lab".
The wait() system call is used to ensure the output is displayed in the order:
Theory:
In UNIX/Linux operating systems, the fork() system call is used to create a new process (child process) from the existing process (parent process).
After the call, both parent and child execute the same program code independently.
-
fork()returns:-
0to the child process -
Child’s PID (a positive integer) to the parent process
-
-1on error
-
The wait() system call is used by a parent process to wait for the completion of its child process.
It helps to achieve process synchronization, ensuring that the parent executes its statements only after the child has finished execution.
By combining fork() and wait(), we can control the order in which output appears from both processes.
Algorithm:
-
Start the program.
-
Call the
fork()system call to create a new process. -
If
fork()returns a negative value, display an error message. -
If the return value is zero, it indicates the child process.
-
Print
"PCCSL407".
-
-
Otherwise, it is the parent process.
-
Call
wait(NULL)to wait until the child process finishes. -
Print
"Operating Systems Lab".
-
-
End the program.
Program:
Output:
Result:
The program was successfully executed and verified.
The fork() system call created a new process, and the wait() system call ensured that the parent process executed only after the child completed, producing the output in the desired order.
Comments
Post a Comment