0% found this document useful (0 votes)
6 views5 pages

OS-7 Final

The document outlines various process management functions in C, including fork(), exec(), execlp(), wait(), and sleep(). It provides theoretical explanations and sample code implementations for each function, demonstrating their usage in creating and managing processes. Additionally, it includes a set of viva questions and answers related to these functions to enhance understanding.

Uploaded by

aakshargarg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views5 pages

OS-7 Final

The document outlines various process management functions in C, including fork(), exec(), execlp(), wait(), and sleep(). It provides theoretical explanations and sample code implementations for each function, demonstrating their usage in creating and managing processes. Additionally, it includes a set of viva questions and answers related to these functions to enhance understanding.

Uploaded by

aakshargarg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

PROGRAM NO.

- 07

AIM: Process Management a) fork) b) exec() c) execlp() d) wait() and e) sleep() A.


Program to implement the fork function using C.

B. Program to implement execv function using C.


C. Program to implement execlp function.
D. Program to implement wait function using C.
E. Program to implement sleep function using C.

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);

// Code after execv will not be executed if successful printf("This


line will not be reached.\n");

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>

int main() { pid_t


child_pid; int status;
child_pid = fork(); if

(child_pid == 0) { printf("Child process (PID:


%d)\n", getpid()); exit(0);
} else if (child_pid > 0) { printf("Parent process (PID: %d), Child PID: %d\n", getpid(),
child_pid); wait(&status); if (WIFEXITED(status)) { printf("Child exited with status:
%d\n", WEXITSTATUS(status)); }
} else { fprintf(stderr, "Fork failed\n");
return
1;
}

return 0;
}
Program to Implement sleep() Function:
#include <stdio.h> #include
<unistd.h>

int main() { printf("Sleeping for 3


seconds...\n"); sleep(3); printf("Awake!\n");
return

0;
Output:

fork()
execv()

execlp()

wait()

sleep()
VIVA QUESTIONS:

Q1) What is the purpose of the fork() function in C?

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.

You might also like