OS-7 Final
OS-7 Final
- 07
THEORY:
a) fork() Function:
● The fork() system call is used to create a new process (child process) that is a duplicate
of the calling process (parent process).
● After a successful fork, two processes run concurrently, and they share the same code,
data, and file descriptors, but they have different process IDs.
● The child process receives a return value of 0 from fork(), while the parent process
receives the child's process ID.
b) exec() Functions:
● The exec() functions are used to replace the current process image with a new one.
● Common variants include execv(), execl(), execvp(), and more.
● These functions load a new program into the current process, and control never returns
to the original program unless there's an error.
c) execlp() Function:
● The execlp() function is a variant of exec() that searches for the program in directories
listed in the PATH environment variable.
● It allows you to execute programs by specifying their name only, without the need for
a full path.
d) wait() Function:
● The wait() system call is used by a parent process to wait for the termination of a child
process.
● It allows the parent to obtain the exit status of the child process.
e) sleep() Function:
● The sleep() function is used to make a process sleep (pause execution) for a specified
number of seconds.
Code:
Program to Implement fork() Function:
#include <stdio.h>
#include <unistd.h>
int main() {
pid_t child_pid; child_pid
= fork();
if (child_pid == 0) {
printf("Child process (PID: %d)\n", getpid());
} else if (child_pid > 0) { printf("Parent process (PID: %d), Child PID:
%d\n", getpid(), child_pid);
} else { fprintf(stderr, "Fork
failed\n"); return 1;
}
return 0;
}
Program to Implement execv() Function:
#include <stdio.h>
#include <unistd.h>
int main() {
char *args[] = {"/bin/ls", "-l", NULL}; execv("/bin/ls",
args);
return 0;
}
Program to Implement execlp() Function:
#include <stdio.h>
#include <unistd.h>
int main() {
execlp("ls", "ls", "-l", NULL);
// Code after execlp will not be executed if successful printf("This line
will not be reached.\n");
return 0;
}
Program to Implement wait() Function:
#include <stdio.h> #include
<stdlib.h> #include
<sys/types.h> #include
<sys/wait.h> #include
<unistd.h>
return 0;
}
Program to Implement sleep() Function:
#include <stdio.h> #include
<unistd.h>
0;
Output:
fork()
execv()
execlp()
wait()
sleep()
VIVA QUESTIONS:
Answer: The fork() function is used to create a new process, known as the child process, which is a duplicate
of the calling process (parent process).
Q2) How do you pass arguments to a new program when using the execv() function?
Answer: Arguments are passed as an array of strings, with the first element being the program name and the
subsequent elements being the program's arguments, terminated with a NULL pointer.
Q3) What is the primary difference between the execlp() and execv() functions in C?
Answer: The primary difference is in how program arguments are specified. execlp() allows you to specify
program arguments directly as function parameters, while execv() requires you to provide the arguments as
an array.
Q4) Why is the wait() function used in a parent process after forking a child process?
Answer: The wait() function is used by the parent process to wait for the termination of the child process and
obtain its exit status. It ensures proper synchronization between parent and child processes.
Q5) What is the purpose of the sleep() function in C, and how is it used?
Answer: The sleep() function is used to make a process pause (sleep) for a specified number of seconds. It
introduces a time delay in the program's execution, allowing you to control timing or create time intervals
between actions.