0% found this document useful (0 votes)
16 views

Lab4 Performance - 201-16-516

The code demonstrates the fork() system call by creating a parent and child process. The fork() call returns 0 to the child process and the child's PID to the parent. getpid() is used to print the PID of the current process, while getppid() prints the parent's PID. A total of two processes are created. The parent uses wait(NULL) to pause its execution and wait for the child process to finish before continuing.

Uploaded by

xekiwi1002
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Lab4 Performance - 201-16-516

The code demonstrates the fork() system call by creating a parent and child process. The fork() call returns 0 to the child process and the child's PID to the parent. getpid() is used to print the PID of the current process, while getppid() prints the parent's PID. A total of two processes are created. The parent uses wait(NULL) to pause its execution and wait for the child process to finish before continuing.

Uploaded by

xekiwi1002
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Lab4 performance

Operating System
Course Code: CIS323L

Submitted To:
Teacher Name: Dr. Sheikh Abdullah
Designation: Assistant Professor

Department of Computing Information System


Daffodil International University

Submitted By:
Name:Md Masum Rana
ID:201-16-516

Department of Computing Information System


Daffodil International University

Date of Submission: 02/02/24


Experiment 8

#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:

bash Copy the code


I have a child's ID, 28.
The child's process ID in this instance is 28. Every time you run the software, the real value
could change.

Which function is used to get the PID of a process?

Getpid() is the function that is used to obtain a process's process ID (PID). Within the code:

To find the PID of the active process, use getpid().


To obtain the PID of the parent process, use getppid().

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.

Program for wait() system call which makes the parent


process wait for the child to finish.
#include <unistd.h>
#include <sys/types.h>
#include <stdio.h>
#include <sys/wait.h>

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.

2. What does the wait() system call return on


success?

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.

3. What is the return value of wait()?

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

pid_t wait(int *status);

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.

5. What does wait(NULL) mean?


When the parent process passes NULL as an argument to wait(), it indicates that it has no
interest in finding out the child process's exit status. It does nothing but wait for any of its
offspring processes to finish. The parent is not gathering data regarding the child's termination
status, as the NULL parameter suggests.

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.

You might also like