Lab4 Performance - 201-16-516
Lab4 Performance - 201-16-516
Operating System
Course Code: CIS323L
Submitted To:
Teacher Name: Dr. Sheikh Abdullah
Designation: Assistant Professor
Submitted By:
Name:Md Masum Rana
ID:201-16-516
#include<stdio.h>
#include <unistd.h>
#include<sys/types.h>
int main()
-{
pid_t p;
printf("before fork\n");
p=fork();
if(p==0)
{
printf("I am child having id %d\n",getpid());
printf("My parent's id is %d\n",getppid());
}
- else{
printf("My child's id is %d\n",p);
printf("I am parent having id %d\n",getpid());
}
printf("Common\n");
}
What does the fork() system call return on success?
The system call fork() returns 0 to the child process and the child process ID to the parent
process. Thus, upon success, the child process's return value is 0, and the parent process's
return value is the child's process ID.
What is the PID of the child process?
The code provided uses getpid() to print the process ID of the child process. The process ID of
the child will appear in the line:
Getpid() is the function that is used to obtain a process's process ID (PID). Within the code:
How many total processes are created with the below code?
The code creates a child process by involving the fork() system function. Consequently, the
creation of two processes occurs: the parent process and the child process. Thus, a total of two
processes have been generated.
int main() {
pid_t p;
printf("before fork\n");
p = fork();
if (p == 0) {
// Child process
printf("I am child having id %d\n", getpid());
printf("My parent's id is %d\n", getppid());
} else {
// Parent process
wait(NULL);
printf("My child's id is %d\n", p);
printf("I am parent having id %d\n", getpid());
}
// This will be printed by both the parent and the child processes
printf("Common\n");
return 0;
}
1. What does the wait() system call do?
The wait() system can pause its execution until one of its child processes ends by using the
wait() system function. It permits the parent to wait for any child process—or just one particular
child process—to end.
The system call return child process is returned by the wait() system function upon success.
Wait() returns the process ID of the terminated child that left the caller process first if there were
several children.
he return value of wait() child process is the number returned by wait(). It returns -1 in the event
of an error.
4. What is the syntax of the wait system call?
The basic syntax of the wait() system call is
In this case, status is a pointer to an integer that will hold the child process's exit status. NULL
can pass if the exit status is not needed.
The code provided uses wait(NULL) to indicate that the parent is waiting for any of its child
processes to terminate and is not in need of knowing the child's exit status.