Lab 3
Lab 3
Lab 3
Question 1
/*
C Program to block a parent process, until
the child completes using a wait system call
*/
// imports
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
int main() {
// creating child and parent process
// storing process id in `r_val`
printf("[*] Starting parent process\n");
int r_val = fork();
// Sleeping...
for(int i = 0; i < 10; i++) {
sleep(1);
printf("[**] Sleeping child for %d seconds...\n", i + 1);
}
Question 2
/*
Program to load binary executables of the previous program,
in a child process using `exec` system call
*/
// imports
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
int main() {
// creating child and parent process
// storing process id in `r_val`
printf("[*] Starting parent process\n");
int r_val = fork();
/*
Program to create a child process.
Display the process IDs of the process,
parent and child(s) in both the
parent and child process.
*/
// imports
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
int main() {
// creating child and parent process
// storing process id in `r_val`
printf("[*] Starting parent process\n");
int r_val = fork();
Question 4
/*
Create a zombie hild process,
and allow the init process to adopt it.
Run the process as a background process
and run the "ps" command
*/
// imports
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
int main() {
// Sleeping...
for(int i = 0; i < 10; i++) {
sleep(1);
printf("[**] Sleeping child for %d seconds...\n", i + 1);
}