Professional Documents
Culture Documents
Q. Task 1
int main(void)
{
cout<<"\nI am process\n"<<getpid();
cout<<endl;
}
int main()
{
int pid;
Page |1
pid = fork();
cout<<"\nAfter Forking.";
if (pid == 0)
cout<<"\nChild process executing";
else
{
cout<<"\nParent process executing with pid = "<<getpid();
cout<<"\nChild process pid = "<<getpid()+1;
}
cout<<endl;
return 0;
}
int main()
{
int pid;
pid = fork();
cout<<"\nAfter Forking.";
if (pid == 0)
cout<<"\nChild process executing with pid = "<<getpid();
cout<<endl;
return 0;
}
Page |2
Task 2
Create a process and make it an orphan.
Ans:
#include <iostream>
#include <unistd.h>
using namespace std;
int main()
{
int pid;
pid = fork();
if(pid == 0)
{
cout<<"\nChild process ID = "<<getpid();
cout<<"\nExecuting Child. Parent's process ID = "<<getppid();
sleep(5);
cout<<"\nChild sleeping for 5 sec..\nChild process ID = "<<getpid();
cout<<"\nParent's process ID = "<<getppid();
}
else
{
sleep(3);
cout<<"Parent's parent process ID = "<<getppid();
}
cout<<endl;
return 0;
}
Page |3
Task 3:
Create a process and make it a Zombie.
Ans:
#include <stdlib.h>
#include <iostream>
#include <sys/types.h>
#include <unistd.h>
int main()
{
int pid = fork();
if (pid > 0)
{
sleep(100);
else
{
exit(0);
}
return 0;
Page |4
Task 4:
Write a C++ program in which a parent process creates a child process using a
fork() system call. The child process takes your age as input and parent process
prints the age.
Ans:
#include <iostream>
#include <unistd.h>
#include <sys/wait.h>
#include <stdlib.h>
using namespace std;
int age;
int main()
{
int pid;
pid = fork();
if(pid == 0)
{
cout<<"\nEnter your age = ";
cin>>age;
exit(0);
}
else
Page |5
{
wait(6);
cout<<"Your age is "<<age;
}
cout<<endl;
return 0;
}
By Manual Program
Page |6
<***************************END**************************>
Page |7