Vous êtes sur la page 1sur 34

Operating Systems Lab (CSX-325)

141030008

OPERATING SYSTEMS
LAB

(CSX-325)

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


Dr. B R AMBEDKAR NATIONAL INSTITUTE OF TECHNOLOGY
JALANDHAR 144011, PUNJAB (INDIA)
JULY-DECEMBER, 2016

Submitted To:

Submitted By:

Mrs. Taranpreet kaur


Assistant Professor
Dept. of CSE

Ishan Bansal
14103008
Dept. of CSE

Operating Systems Lab (CSX-325)

141030008

INDEX
Sr. No.

Title

Page No.

1.)

Installation of Linux OS on system.

1-6

2.)

Write basic commands of Linux

7-10

3.)

Write a program to create a process


using fork system call
To print 1 to 100 numbers by
creating K processes to print the
numbers sequentially
Write a program to terminate a
parent process and make the child
process orphan
Write a program to implement signal
handling (Event Handling)
To simulate CPU scheduling
algorithms
7.1) Non Preemptive scheduling
techniques
(a) FCFS
(b) SRN
(c) HRN
7.2) Preemptive Scheduling
techniques
(a) Roundrobin
(b) LCN
(c) STG

11-11

4.)
5.)
6.)
7.)

12-12
13-13
14-15

16-17
18-19
20-21
22-25
26-29
31-34

Remarks

Operating Systems Lab (CSX-325)

1.Installation of linux OS on system.

141030008

Step 1: Create a live USB or disk


Download and create a live USB or DVD. If you are using Windows, we can use
a live usb creator tool. In Ubuntu, we can use Startup Disk Creator (if it works).

Step 2: Boot in to live USB


Plug the live USB or disk in to the computer and restart the computer. While
booting the computer press F10 or F12 function key (defers from computer to
computer) to go to the boot menu. Now, choose the option to boot from USB or
Removable Media.

Step 3: Start the installation


It takes some time to boot from the live USB or disk. Have some patience. Once
it boots in to live disk, youll be provided to Try Linux Mint or Install Linux Mint.
Even if we choose to try it, we can find the install option on the desktop:

and next few screens, well be


asked to choose the language of
the operating system. It will then
do some checks on available
space, battery and Internet
connection.

Step 4: Prepare the partition


This is the most important part of the whole installation. Where to install Linux
Mint? As mentioned before, I prefer separate partitions for Windows and Linux.
3

Operating Systems Lab (CSX-325)

141030008

Windows is already installed here, well prepare a new partition for Linux Mint.
In the Installation Type window, choose Something Else:

Now, I have 3 NTFS and some ext4 partitions. If we dont have ext4 partition,
dont worry, we dont need that. As we can see in the picture below, one of the
NTFS partition consists of Windows installation. This should be untouched if we
want to keep your Windows installation safe.
I hope we have more than one NTFS (or FAT 32) partitions (i.e. Drives in
Windows term) on our hard disk, with one of them consisting of Windows
installation (usually C drive). What we need to do here is to delete a NTFS or
existing ext4 partition and create some free space. This will delete all the data
in that partition and this is why I asked we to verify if you have Windows
installed in a different partition.Click on the desired partition and press the to
delete the partition

Step 5: Create root, swap


and home
Once we created free space on our
hard drive, its time to install Linux
Mint on it. Now, there are several
ways to do it. But here, Ill show you
my favorite way and that is to have
a Root, a swap and a Home.
Create a root partition first. Choose
the free space available and click on
+.

Operating Systems Lab (CSX-325)

141030008

Here,
choose
the size of root
(10
GB
is
enough but I
chose to have 20 here), choose ext4 file system, and mount point as / (i.e.
root):

Operating Systems Lab (CSX-325)

141030008

Now, next is to create swap


partition. It is advised by many
that swap should be double of
our RAM. we can choose the
swap size accordingly.

The next step is to create Home. Try to allocate the maximum size to Home
because this is where well be downloading and keeping the files.

Once we have created Root, Swap and Home partitions, click on Install Now
button.

Operating Systems Lab (CSX-325)

141030008

Step 6: Follow the trivial instructions


Technically, we have crossed the main hurdle if we react till this point
successfully. Now we will be taken through a number of screens to select
options like keyboard layout, login credentials etc. You dont need to be a
genius to figure out what to do here afterwards. I have attached screenshots for
reference purpose here.

Operating Systems Lab (CSX-325)

141030008

Once the installation is over, we will be presented with the option to keep
trying live version or to restart the system.

Operating Systems Lab (CSX-325)

141030008

2.Write basic commands of linux.


a. ps :- It is used to generate snapshot of current process.

b. ls :- List information about the FILEs (the current directory by


default).

c. ps -aef:- It is used to find out the process whose scripts are


located are in current directory.

d. top:- top provides dynamic real time view of a running system.


9

Operating Systems Lab (CSX-325)

141030008

e. ifconfig:- It is used to configure the real time view of a running


system.

f. cat :- It is used to concatanate files or Std input to output.

g. free :- It is used to display total unused memory.

h. df :- Display the amount of disk space available on the file


system containing by each file.

10

Operating Systems Lab (CSX-325)

141030008

i. mount :- All files which are accessible in a unix system are


arranged in a tree .mount command is used to attach file system.

j. grep :- It is used to find out a pattern like a character or a


sentence of the concatanated file.

11

Operating Systems Lab (CSX-325)

141030008

3.Write a program to create a process using fork


system call.

#include<stdio.h>
#include<unistd.h>
int main()
{
int k=fork();
if(k==0)
{
printf("Child process with pid = %d and ppid =
%d\n",getpid(),getppid());
}
else
{
printf("Parent process with pid = %d and child id =
%d\n",getpid(),k);
}
}

12

Operating Systems Lab (CSX-325)

141030008

4.Write a program to print 1 to 100 numbers by


creating k processes using execlp.

#include<stdio.h>
#include<unistd.h>
int main()
{
int n,i;
int k;
printf("Enter the number of processes to be started\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
k= fork();
if(k==0)
{
execle("/home/Ishan/Desktop/os/fj","f.c",(char *)NULL,(char *)NULL);
execlp("/home/Ishan/Desktop/os/fj","f.c",(char *)NULL);
}
}
}
Program print.c:// to print 100 numbers using 10 processes
#include<stdio.h>
#include<stdlib.h>
int main(int argc,char *argv[])
{
int k=atoi(argv[0]);
int j=atoi(argv[1]);
int i=0;
for(i=k+1;i<=j;i++)
printf("%d ",i);
printf("\n");
}

13

Operating Systems Lab (CSX-325)

141030008

5.Write a program to terminate a parent process


and make the child process orphan.

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
int main()
{
signal
int k=fork();
if(k==0)
{
printf("Child id= %d parent id= %d\n",getpid(),getppid());
sleep(15);
printf("Parent Dead\n");
printf("Parent is dead. New parent id=%d\n",getppid());
}
else
{
printf("Parent process going to sleep\n");
sleep(5);
printf("Parent about to die with id:%d with child id
%d\n",getpid(),k);
}
}

14

Operating Systems Lab (CSX-325)

141030008

6.Write a program to implement signal handling.


#include<stdio.h>
#include<sys/types.h>
#include<sys/wait.h>
#include<stdlib.h>
#include<signal.h>
int main(){
int k=fork();
if(k==0){
printf("Parent: %d GroupID: %d\n",getppid(),getpgrp());
//
raise(SIGINT);
//
sleep(2);
printf("Help\n");
exit(20);
} else {
printf("I am Parent of Child: %d\n",k);
int status;
int d=wait(&status);
printf("Dead Child: %d, Value from it: %d Signaled:
%d\n",d,WEXITSTATUS(status),WIFSIGNALED(status));
}
return 0;
}

Tag
WIFEXITED( status)

WEXITSTATUS( status)

WIFSIGNALED( status)
WTERMSIG( status)
WCOREDUMP( status)

Description
returns true if the child terminated normally, that is,
by calling exit(3) or _exit(2), or by returning from
main().
returns the exit status of the child. This consists of
the least significant 16-8 bits of the status
argument that the child specified in a call to exit()
or _exit() or as the argument for a return statement
in main(). This macro should only be employed
ifWIFEXITED returned true.
returns true if the child process was terminated by a
signal.
returns the number of the signal that caused the
child process to terminate. This macro should only
be employed if WIFSIGNALED returned true.
returns true if the child produced a core dump. This
15

Operating Systems Lab (CSX-325)

WIFSTOPPED( status)

WSTOPSIG( status)
WIFCONTINUED( status)
Tag
WNOHANG
WUNTRACED

WCONTINUED

141030008

macro should only be employed if WIFSIGNALED


returned true. This macro is not specified in
POSIX.1-2001 and is not available on some Unix
implementations (e.g., AIX, SunOS). Only use this
enclosed in #ifdef WCOREDUMP ... #endif.
returns true if the child process was stopped by
delivery of a signal; this is only possible if the call
was done using WUNTRACED or when the child is
being traced (see ptrace(2)).
returns the number of the signal which caused the
child to stop. This macro should only be employed if
WIFSTOPPED returned true.
(Since Linux 2.6.10) returns true if the child process
was resumed by delivery ofSIGCONT.
Description
return immediately if no child has exited.
also return if a child has stopped(but not traced via
ptrace(2)). Status for tracedchildren which have
stopped is provided even if this option is not
specified.
(Since Linux 2.6.10) also return if a stopped child has
been resumed by delivery ofSIGCONT.

16

Operating Systems Lab (CSX-325)

141030008

7.1 To implement FCFS non-preemptive


scheduling algorithm.

#include<stdio.h>
struct pair
{
double a,b;
};
int main()
{
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
int n;
scanf("%d",&n);
struct pair ar[n];
double waiting[n];
for(int i=0;i<n;i++)
{
scanf("%lf%lf",&ar[i].a,&ar[i].b);
}
waiting[0]=0;
double current=ar[0].b;
for(int i=1;i<n;i++)
{
if(ar[i].a<current)
waiting[i]=current-ar[i].a;
else
waiting[i]=0;
current=current+ar[i].b;
}
double totalwaiting=0;
for(int i=0;i<n;i++)
{
totalwaiting+=waiting[i];
}
double avg=totalwaiting/n;
printf(" Pid | Turn Around time | Waited Turn Around Time
\n");
for(int i=0;i<55;i++)
printf("-");
printf("\n");
double avg1=0;
double avg2=0;
17

Operating Systems Lab (CSX-325)

141030008

for(int i=0;i<n;i++)
{
avg1+=waiting[i]+ar[i].b;
avg2+=(waiting[i]+ar[i].b)/ar[i].b;
printf(" P%d | %lf\t
|
%lf\n",i+1,waiting[i]
+ar[i].b,(waiting[i]+ar[i].b)/ar[i].b);
}
for(int i=0;i<55;i++)
printf("-");
printf("\n");
printf(" Average| %lf\t
|
%lf\n",avg1/n,avg2/n);
}

18

Operating Systems Lab (CSX-325)

141030008

7.2 To implement SRN non-preemptive


scheduling algorithm.

#include<bits/stdc++.h>
using namespace std;
int main()
{
freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
int glo=0;
int n;
scanf("%d",&n);
double counter=0.0;
pair<int,double> completed[n+1];
pair<pair<double,double> ,int > se[n+1];
pair<pair<double,double> ,int > oe[n+1];
for(int i=0;i<n;i++)
{
scanf("%lf%lf",&se[i].first.second,&se[i].first.first);
se[i].second=i;
oe[i].first.second=se[i].first.second;
oe[i].first.first=se[i].first.first;
}
double timer=0.0;
int front=0;
int ind;
for(int k=0;k<n;k++)
{
ind=n;
for(int i=front;i<n;i++)
{
if(se[i].first.second>timer)
{
ind=i;
break;
}
}
sort(se+front,se+ind);
timer+=se[front].first.first;
completed[glo].first=se[front].second;
completed[glo].second=timer;
front++;
glo++;
}
printf(" Pid | Turn Around time | Waited Turn Around Time \n");
19

Operating Systems Lab (CSX-325)

141030008

for(int i=0;i<55;i++)
printf("-");
printf("\n");
double avg1=0;
double avg2=0;
for(int i=0;i<n;i++)
{
int k=completed[i].first;
avg1+=(completed[i].second-oe[k].first.second);
double tr=(completed[i].second-oe[k].first.second);
double service=((completed[i].secondoe[k].first.second)/oe[k].first.first);
avg2+=service;
printf(" P%d | %lf\t
|
%lf\n",k+1,tr,service);
}
for(int i=0;i<55;i++)
printf("-");
printf("\n");
printf(" Average| %lf\t
|
%lf\n",avg1/n,avg2/n);
}

20

Operating Systems Lab (CSX-325)

141030008

7.3 To implement Highest Response Ratio nonpreemptive scheduling.

#include<bits/stdc++.h>
using namespace std;
int main()
{
freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
ios::sync_with_stdio(false);
int n;
cin>>n;
pair<double,double> ar[n];
pair<int,pair<double,double> > br[n];
int has[n]={0};
int glo=0;
for(int i=0;i<n;i++)
{
cin>>ar[i].first>>ar[i].second;
}
double timer=0;
while(1)
{
double max=-10,k;
int ind=-1;
for(int i=0;i<n;i++)
{
if((has[i]==0)&&(ar[i].first<=timer))
{
k=(timer-ar[i].first)+ar[i].second;
k=k/ar[i].second;
if(k>max)
{
max=k;
ind=i;
}
}
}
if(ind==-1)
break;
else
{
has[ind]=1;
timer=timer+ar[ind].second;
br[glo].first=ind;
double temp=timer-ar[ind].first;
21

Operating Systems Lab (CSX-325)


}

br[glo].second.first=temp;
br[glo++].second.second=temp/ar[ind].second;

141030008

printf(" Pid | Turn Around time | Waited Turn Around Time \n");
for(int i=0;i<55;i++)
printf("-");
printf("\n");
double avg1=0;
double avg2=0;
for(int i=0;i<n;i++)
{
avg1+=br[i].second.first;
avg2+=br[i].second.second;
printf(" P%d | %lf\t
|
%lf\n",br[i].first+1,br[i].second.first,br[i].second.second);
}
for(int i=0;i<55;i++)
printf("-");
printf("\n");
printf(" Average| %lf\t
|
%lf\n",avg1/n,avg2/n);
}

22

Operating Systems Lab (CSX-325)

141030008

7.4 To implement Round Robin preemptive


scheduling.

#include<bits/stdc++.h>
using namespace std;
#define fast_io ios::sync_with_stdio(false)
int main()
{
freopen("input.txt","r",stdin);
//freopen("output1.txt","w",stdout);
fast_io;
int n;
queue<int> q;
cin>>n;
double timer=0.0;
double time[n];
int has[n]={0};
pair<double,double> ar[n+1];
for(int i=1;i<=n;i++)
{
cin>>ar[i].first>>ar[i].second;
time[i]=ar[i].second;
}
double completed[n+1]={0.0};
double timeslice=1.0;
int mkg=0;
int pointer=2;
printf("Gantt Chart for the algorithm is \n");
23

Operating Systems Lab (CSX-325)

141030008

q.push(1);
while(1)
{

printf("%d-%d--->P%d \n",mkg,mkg+1,q.front());
int kl=q.front();
time[q.front()]--;
q.pop();
timer+=timeslice;
while(1)
{
if(pointer==n+1)
break;
if(ar[pointer].first<=timer)
{
q.push(pointer);
pointer++;
}
else
break;
}
if(time[kl]!=0)
{
q.push(kl);
}
else
{
completed[kl]=timer;
24

Operating Systems Lab (CSX-325)

141030008

if(q.size()==0)
break;
mkg++;
}
cout<<endl;
double tat[n+1],wtat[n+1];
double avg1=0;
double avg2=0;
for(int i=1;i<=n;i++)
{
tat[i]=completed[i]-ar[i].first;
avg1+=tat[i];
wtat[i]=tat[i]/ar[i].second;
avg2+=wtat[i];
}
printf(" Pid | Turn Around time | Waited Turn Around Time
\n");
for(int i=0;i<55;i++)
printf("-");
printf("\n");
for(int i=1;i<=n;i++)
{
printf(" P%d |

%lf\t

}
for(int i=0;i<55;i++)
25

%lf\n",i,tat[i],wtat[i]);

Operating Systems Lab (CSX-325)

141030008

printf("-");

printf("\n");
printf(" Average|

%lf\t

%lf\n",avg1/n,avg2/n);

26

Operating Systems Lab (CSX-325)

141030008

7.5 To implement Least Completed Next


preemptive scheduling.

#include<bits/stdc++.h>
using namespace std;
#define fast_io ios::sync_with_stdio(false)
int main()
{
freopen("input.txt","r",stdin);
//freopen("output1.txt","w",stdout);
fast_io;
int n;
cin>>n;
double timer=0.0;
int mkg=0;
int time[n];
pair<double,double> ar[n+1];
for(int i=1;i<=n;i++)
{
cin>>ar[i].first>>ar[i].second;
time[i]=0;
}
double completed[n+1]={0.0};
int has[n+1]={0};
int ind,min;
while(1)
{
ind=-1,min=INT_MAX;
27

Operating Systems Lab (CSX-325)

141030008

for(int i=1;i<=n;i++)
{
if(has[i]==0&&ar[i].first<=timer)
{
if(time[i]<min)
{
min=time[i];
ind = i;
}

}
}
if(ind==-1)
break;
printf("%d-%d--->P%d\n",mkg,mkg+1,ind);
time[ind]++;
if(time[ind]==ar[ind].second)
{
completed[ind]=timer+1.0;
has[ind]=1;
}
timer+=1.0;
mkg++;
}
cout<<endl;
double tat[n+1],wtat[n+1];
double avg1=0;
28

Operating Systems Lab (CSX-325)

141030008

double avg2=0;

for(int i=1;i<=n;i++)
{
tat[i]=completed[i]-ar[i].first;
avg1+=tat[i];
wtat[i]=tat[i]/ar[i].second;
avg2+=wtat[i];
}
printf(" Pid | Turn Around time | Waited Turn Around Time
\n");
for(int i=0;i<55;i++)
printf("-");
printf("\n");
for(int i=1;i<=n;i++)
{
printf(" P%d |

%lf\t

%lf\n",i,tat[i],wtat[i]);

}
for(int i=0;i<55;i++)
printf("-");
printf("\n");
printf(" Average|

%lf\t

%lf\n",avg1/n,avg2/n);

29

Operating Systems Lab (CSX-325)

141030008

30

Operating Systems Lab (CSX-325)

141030008

7.6 To implement Shortest Time to Go


preemptive scheduling.

#include<bits/stdc++.h>
using namespace std;
#define fast_io ios::sync_with_stdio(false)
int main()
{
freopen("input.txt","r",stdin);
//freopen("output1.txt","w",stdout);
fast_io;
int n;
cin>>n;
double timer=0.0;
int mkg=0;
int time[n];
pair<double,double> ar[n+1];
for(int i=1;i<=n;i++)
{
cin>>ar[i].first>>ar[i].second;
time[i]=ar[i].second;
}
double completed[n+1]={0.0};
int has[n+1]={0};
int ind,min;
while(1)
{
ind=-1,min=INT_MAX;
31

Operating Systems Lab (CSX-325)

141030008

for(int i=n;i>=1;i--)
{

if(has[i]==0&&ar[i].first<=timer)
{
if(time[i]<min)
{
min=time[i];
ind = i;
}

}
}
if(ind==-1)
break;
printf("%d-%d--->P%d\n",mkg,mkg+1,ind);
time[ind]--;
if(time[ind]==0)
{
completed[ind]=timer+1.0;
has[ind]=1;
}
timer+=1.0;
mkg++;
}
cout<<endl;
double tat[n+1],wtat[n+1];
double avg1=0;
32

Operating Systems Lab (CSX-325)

141030008

double avg2=0;

for(int i=1;i<=n;i++)
{
tat[i]=completed[i]-ar[i].first;
avg1+=tat[i];
wtat[i]=tat[i]/ar[i].second;
avg2+=wtat[i];
}
printf(" Pid | Turn Around time | Waited Turn Around Time
\n");
for(int i=0;i<55;i++)
printf("-");
printf("\n");
for(int i=1;i<=n;i++)
{
printf(" P%d |

%lf\t

%lf\n",i,tat[i],wtat[i]);

}
for(int i=0;i<55;i++)
printf("-");
printf("\n");
printf(" Average|

%lf\t

%lf\n",avg1/n,avg2/n);

33

Operating Systems Lab (CSX-325)

141030008

34

Vous aimerez peut-être aussi