Ex02-Linux Process API-fork(), wait(), exec()
Operating systems Lab exercise
To write C Program that uses Linux Process API - fork(), wait(), exec()
DEVELOPED BY:
NAME: MANIKANDAN M
REG NO:212224040184
Navigate to any Linux environment installed on the system or installed inside a virtual environment like virtual box/vmware or online linux JSLinux (https://bellard.org/jslinux/vm.html?url=alpine-x86.cfg&mem=192) or docker.
Write the C Program using Linux Process API - fork(), wait(), exec()
Test the C Program for the desired output.
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main(void)
{ gcc
pid_t process_id;
pid_t p_process_id;
process_id = getpid();
p_process_id = getppid();
printf("The process id: %d\n",process_id);
printf("The process id of parent function: %d\n",p_process_id);
return 0;
}

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
int main() {
pid_t pid = fork();
if (pid < 0) {
perror("Fork failed");
exit(1);
}
if (pid == 0) {
printf("I am child, my PID is %d\n", getpid());
printf("My parent PID is: %d\n", getppid());
sleep(2);
} else {
printf("I am parent, my PID is %d\n", getpid());
wait(NULL);
}
return 0;
}

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
int main() {
int status;
pid_t pid;
printf("Running ps with execl (no path)\n");
pid = fork();
if (pid == 0) {
execl("ps", "ps", "ax", NULL);
perror("execl failed");
exit(1);
} else {
wait(&status);
if (WIFEXITED(status))
printf("Child exited with status of %d\n", WEXITSTATUS(status));
else
puts("Child did not exit successfully");
}
printf("Running ps with execl (with /bin/ps path)\n");
pid = fork();
if (pid == 0) {
execl("/bin/ps", "ps", "ax", NULL);
perror("execl failed");
exit(1);
} else {
wait(&status);
if (WIFEXITED(status))
printf("Child exited with status of %d\n", WEXITSTATUS(status));
else
puts("Child did not exit successfully");
}
printf("Done.\n");
return 0;
}



The programs are executed successfully.