- What is the purpose of the
fork()
system call in UNIX-like operating systems?
- Write a simple C program that uses
fork()
to create a child process. Print the process ID (PID) of both the parent and child.
Hint: Use getpid()
to retrieve the process ID and fork()
to create a new process.
#include <stdio.h>#include <stdlib.h>#include <unistd.h>int main() {
printf("Before fork (pid:%d)\\n", (int) getpid());
int rc = fork();
printf("After fork (pid:%d), rc=%d\\n", (int) getpid(), rc);
return 0;
}
- In the following code, what will be printed, and why?
- M odify your program to check if
fork()
fails and handle it gracefully.
Hint: If fork()
returns a negative value, it indicates failure.
- What is the purpose of the
wait()
system call?
#include <stdio.h>#include <stdlib.h>#include <unistd.h>int main() {
printf("Before fork (pid:%d)\\n", (int) getpid());
int rc = fork();
printf("After fork (pid:%d), rc=%d\\n", (int) getpid(), rc);
return 0;
}
- Modify your previous
fork()
program to include wait()
so that the parent process waits for the child to finish before continuing.
Hint: Call wait(NULL);
in the parent process.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
int main() {
printf("Start (pid:%d)\\n", (int)getpid());
int rc = fork();
if (rc < 0) {
fprintf(stderr, "fork failed\\n");
exit(1);
} else if (rc == 0) {
printf("Child (pid:%d)\\n", (int)getpid());
} else {
wait(NULL);
printf("Parent (pid:%d) - child finished\\n", (int)getpid());
}
return 0;
}
- Given the following code, what will be the output order?
- What does the
exec()
system call do?
- Modify your
fork()
program so that the child process replaces its execution with another program using execvp()
.
Use execvp()
to run the ls
command from the child process.
- What happens if
execvp()
fails?
#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <sys/wait.h>#include <string.h>int main() {
printf("Starting (pid:%d)\\n", (int)getpid());
int rc = fork();
if (rc < 0) {
fprintf(stderr, "fork failed\\n");
exit(1);
} else if (rc == 0) {
char *args[] = {"ls", "-l", NULL};
execvp(args[0], args);
printf("This should not be printed!\\n");
} else {
wait(NULL);
printf("Parent finished (pid:%d)\\n", (int)getpid());
}
return 0;
}
- What output will this code produce?
- Why does
exec()
not create a new process but instead replaces the current one?
- What happens if
fork()
is called multiple times in a loop? Write a program that creates a chain of processes.
Hint: Try a loop with fork()
inside.
- Modify a
fork()
program so that multiple children execute different commands using execvp()
. For example, one child executes ls
, another whoami
, and another date
.
Hint: Use multiple fork()
calls and if-else
branches.
- Write a program that calls fork(). Before calling fork(), have the main process access a variable (e.g., x) and set its value to something (e.g., 100). What value is the variable in the child process?
What happens to the variable when both the child and parent change the value of x?
- Write a program that opens a file (with the open() system call) and then calls fork() to create a new process. Can both the child and parent access the file descriptor returned by open()? What happens when they are writing to the file concurrently, i.e., at the same time?