Ex02-Linux Process API-fork(), wait(), exec()
Operating systems Lab exercise
To write C Program that uses Linux Process API - fork(), wait(), exec()
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.
C Program to create new process using Linux API system calls fork() and getpid() , getppid() and to print process ID and parent Process ID using Linux API system calls
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main(void)
{
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; }
C Program to execute Linux system commands using Linux API system calls exec() , exit() , wait() family
#include <stdlib.h>
#include <sys/wait.h>
#include<stdio.h>
#include<unistd.h>
#include <sys/types.h>
int main()
{ int pid;
pid=fork();
if(pid == 0)
{ printf("Iam child my pid is %d\n",getpid());
printf("My parent pid is:%d\n",getppid());
exit(0); }
else{
printf("I am parent, my pid is %d\n",getpid());
sleep(100);
exit(0);}
The programs are executed successfully.