0% found this document useful (0 votes)
28 views8 pages

Ansar - F18605005 Inlab + Post Lab No 06 OS

The document discusses four tasks related to processes and process management in operating systems. It provides code snippets in C++ for creating child processes, making a process an orphan or zombie, and having a child process take input and a parent process print the output.

Uploaded by

Ansar Iqbal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views8 pages

Ansar - F18605005 Inlab + Post Lab No 06 OS

The document discusses four tasks related to processes and process management in operating systems. It provides code snippets in C++ for creating child processes, making a process an orphan or zombie, and having a child process take input and a parent process print the output.

Uploaded by

Ansar Iqbal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

National University of Technology

Department of Computer Science


CS4012 Operating System

Instructor Name: Madam Tabinda Grade:


Batch: 2018 Session: Spring 2021
Assessment Type: Postlab Assessment No. 06
Student Name: Ansar Iqbal Registration No. F18605005

Q. Task 1

1. Print something and Check id of the parent process


Ans:
#include <iostream>
#include <unistd.h>
using namespace std;

int main(void)
{
cout<<"\nI am process\n"<<getpid();
cout<<endl;
}

2. Create a child process and print child process id in parent process


Ans:
#include <iostream>
#include <unistd.h>
using namespace std;

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;
}

3. Create a child process and print child process id in child process


Ans:
#include <iostream>
#include <unistd.h>
using namespace std;

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();

Page |5
if(pid == 0)
{
cout<<"\nEnter your age = ";
cin>>age;
exit(0);
}
else
{
wait(6);
cout<<"Your age is "<<age;
}

cout<<endl;
return 0;
}

By Manual Program

Page |6
Page |7
<***************************END**************************>

Page |8

You might also like