You are on page 1of 34

A

PRACTICAL FILE
ON
OPERATING SYSTEM

SUBMITTED TO: SUBMITTED BY:


INDEX
Sr. No. Contents Page No. Remarks
01 Unix General Concepts
i) Kernel
ii) File System
iii) Commands
iv) vi Commands
02 Bankers Algorithm
03 Producer-Consumer Problem (Semaphore)
04 Reader Writer Problem
05 Page Replacement
i) First In First Out
ii) Least Recently Used

06 I/O System Calls


i) Reading from a file
ii) Writing into a file

07 CPU Scheduling
i) First come first served
ii) Shortest job first
iii) Priority
iv) Round robin

08 Memory Management – I
i) Simulation using Paging
ii) Memory management strategies:
a) First fit
b) Best fit

UNIX COMMANDS
To study and excute the commands in unix.
2
COMMANDS:

1.Date Command :
This command is used to display the current data and time.

Syntax :
$date
$date +%ch
Options : -
a = Abbrevated weekday.
A = Full weekday.
b = Abbrevated month.
B = Full month.
c = Current day and time.
C = Display the century as a decimal number.
d = Day of the month.
D = Day in ‘mm/dd/yy’ format
h = Abbrevated month day.
H = Display the hour.
L = Day of the year.
m = Month of the year.
M = Minute.
P = Display AM or PM
S = Seconds
T = HH:MM:SS format
u = Week of the year.
y = Display the year in 2 digit.
Y = Display the full year.
Z = Time zone .
To change the format :
Syntax :
$date ‘+%H-%M-%S’
2.Calender Command :
This command is used to display the calendar of the year or the particular month of
calendar year.
Syntax :
a.$cal <year>
b.$cal <month> <year>

Here the first syntax gives the entire calendar for given year & the second Syntax gives the calendar
of reserved month of that year.

3.Echo Command :This command is used to print the arguments on the screen .

Syntax :

3
$echo <text>
Multi line echo command :
To have the output in the same line , the following commands can be used.
Syntax :
$echo <text\>text
To have the output in different line, the following command can be used.
Syntax :
$echo “text
>line2
>line3”
4.Banner Command :
It is used to display the arguments in ‘#’ symbol .
Syntax :
$banner <arguments>
5.’who’ Command :
It is used to display who are the users connected to our computer currently.
Syntax :
$who – option’s
Options : -
H – Display the output with headers.
b – Display the last booting date or time or when the system was lastely rebooted.
6.’who am i’ Command :
Display the details of the current working directory.
Syntax :
$who am i
7.’tty’ Command :
It will display the terminal name.
Syntax :
$tty
8.’Binary’ Calculator Command :
It will change the ‘$’ mode and in the new mode, arithematic operations such as +,-
,*,/,%,n,sqrt(),length(),=, etc can be performed . This command is used to go to the binary
calculus mode.
Syntax :
$bc
operations
^d
$
1 base –inputbase
0 base – outputbase are used for base conversions.
Base :
Decimal = 1 Binary = 2 Octal =8 Hexa = 16
9.’CLEAR’ Command :
It is used to clear the screen.
Syntax :

4
$clear
10.’MAN’ Command :
It help us to know about the particular command and its options & working. It is like ‘help’
command in windows .
Syntax :
$man <command name>
11.MANIPULATION Command :
It is used to manipulate the screen.
Syntax :
$tput <argument>
Arguments :
1.Clear – to clear the screen.
2.Longname – Display the complete name of the terminal.
3.SMSO – background become white and foreground become black color.
4.rmso – background become black and foreground becomes white color.
5.Cop R C – Move to the cursor position to the specified location.
6.Cols – Display the number of columns in our terminals.
12.LIST Command :
It is used to list all the contents in the current working directory.
Syntax :
$ ls – options <arguments>
If the command does not contain any argument means it is working in the
Current directory.
Options :
a– used to list all the files including the hidden files.
c– list all the files columnwise.
d- list all the directories.
m- list the files separated by commas.
p- list files include ‘/’ to all the directories.
r- list the files in reverse alphabetical order.
f- list the files based on the list modification date.
x-list in column wise sorted order.

5
DIRECTORY RELATED COMMANDS :
1.Present Working Directory Command :
To print the complete path of the current working directory.
Syntax :
$pwd
2.MKDIR Command :
To create or make a new directory in a current directory .
Syntax :
$mkdir <directory name>
3.CD Command :
To change or move the directory to the mentioned directory .
Syntax :
$cd <directory name.
4.RMDIR Command :
To remove a directory in the current directory & not the current directory itself.
Syntax :
$rmdir <directory name>
FILE RELATED COMMANDS :
1.CREATE A FILE :
To create a new file in the current directory we use CAT command.
Syntax :
$cat > <filename.
The > symbol is redirectory we use cat command.
2.DISPLAY A FILE :
To display the content of file mentioned we use CAT command without ‘>’ operator.
Syntax :
$cat <filename.
Options –s = to neglect the warning /error message.
3.COPYING CONTENTS :
To copy the content of one file with another. If file doesnot exist, a new file is created and
if the file exists with some data then it is overwritten.
Syntax :
$ cat <filename source> >> <destination filename>
$ cat <source filename> >> <destination filename> it is avoid
overwriting.
Options : -
-n content of file with numbers included with blank lines.
Syntax :
$cat –n <filename>
4.SORTING A FILE :
To sort the contents in alphabetical order in reverse order.
Syntax :
$sort <filename >
Option : $ sort –r <filename>
5.COPYING CONTENTS FROM ONE FILE TO ANOTHER :

6
To copy the contents from source to destination file . so that both contents are same.
Syntax :
$cp <source filename> <destination filename>
$cp <source filename path > <destination filename path>
6.MOVE Command :
To completely move the contents from source file to destination file and to remove the
source file.
Syntax :
$ mv <source filename> <destination filename>
7.REMOVE Command :
To permanently remove the file we use this command .
Syntax :
$rm <filename>
8.WORD Command :
To list the content count of no of lines , words, characters .
Syntax :
$wc<filename>
Options :
-c – to display no of characters.
-l – to display only the lines.
-w – to display the no of words.
9.LINE PRINTER :
To print the line through the printer, we use lp command.
Syntax :
$lp <filename>
10.PAGE Command :
This command is used to display the contents of the file page wise & next page can be viewed
by pressing the enter key.
Syntax :
$pg <filename>

7
vi EDITOR COMMANDS
To study the various commands operated in vi editor in UNIX.
Starting Vi :
The Vi editor is invoked by giving the following commands in UNIX prompt.
Syntax : $vi <filename> (or)
$vi
This command would open a display screen with 25 lines and with tilt (~) symbol at the start
of each line. The first syntax would save the file in the filename mentioned and for the next the
filename must be mentioned at the end.
Options :
1.vi +n <filename> - this would point at the nth line (cursor pos).
2.vi –n <filename> - This command is to make the file to read only to change from one
mode to another press escape key.
INSERTING AND REPLACING COMMANDS :
To move editor from command node to edit mode, you have to press the <ESC> key.
For inserting and replacing the following commands are used.
1. ESC a Command :
This command is used to move the edit mode and start to append after the current character.
Syntax : <ESC> a
2. ESC A COMMAND :
This command is also used to append the file , but this command append at the end of
current line.
Syntax : <ESC> A
3. ESC i Command :
This command is used to insert the text before the current cursor position.
Syntax : <ESC> i
4. ESC I Command :
This command is used to insert at the beginning of the current line.
Syntax : <ESC> I
5. ESC o Command :
This command is insert a blank line below the current line & allow insertion of contents.
Syntax : <ESC> o
6. ESC O Command :
This command is used to insert a blank line above & allow insertion of contents.
Syntax : <ESC> O
7. ESC r Command :
This command is to replace the particular character with the given characters.
Syntax : <ESC> rx Where x is the new character.

8. ESC R Command :
This command is used to replace the particular text with a given text.

8
Syntax : <ESC> R text
9. <ESC> s Command :
This command replaces a single character with a group of character .
Syntax : <ESC> s
10. <ESC> S Command :
This command is used to replace a current line with group of characters.
Syntax : <ESC> S
CURSOR MOVEMENT IN vi :
1.<ESC> h :
This command is used to move to the previous character typed. It is used to move to
left of the text . It can also used to move character by character (or) a number of characters.
Syntax :
<ESC> h – to move one character to left.
<ESC> nh – to move ‘n’ character to left.
2.<ESC> l :
This command is used to move to the right of the cursor (ie) to the next character. It
can also be used to move the cursor for a number of character.
Syntax :
<ESC> l – single character to right.
<ESC> nl - ‘n’ characters to right.
3.<ESC> j :
This command is used to move down a single line or a number of lines.
Syntax :
<ESC> j – single down movement.
<ESC> nj – ‘n’ times down movement.
4.<ESC> k :
This command is used to move up a single line or a number of lines.
Syntax :
<ESC> k – single line above.
<ESC> nk – ‘n’ lines above.
5.ENTER (OR) N ENTER :
This command will move the cursor to the starting of next lines or a group of lines mentioned.
Syntax :
<ESC> enter <ESC> n enter.
6.<ESC> + Command :
This command is used to move to the beginning of the next line.
Syntax :
<ESC> + <ESC> n+
7.<ESC> - Command :
This command is used to move to the beginning of the previous line.
Syntax: <ESC> - <ESC> n-

8.<ESC> 0 :
This command will bring the cursor to the beginning of the same current line.
Syntax :
<ESC> 0

9
9.<ESC> $ :
This command will bring the cursor to the end of the current line.
Syntax :
<ESC> $
10.<ESC> ^ :
This command is used to move to first character of first lines.
Syntax :
<ESC> ^
11.<ESC> b Command :
This command is used to move back to the previous word (or) a number of words.
Syntax :
<ESC> b <ESC>nb
12.<ESC> e Command :
This command is used to move towards and replace the cursor at last character of the
word (or) no of words.
Syntax :
<ESC> e <ESC>ne
13.<ESC> w Command :
This command is used to move forward by a single word or a group of words.
Syntax :
<ESC> w <ESC> nw
DELETING THE TEXT FROM Vi :
1.<ESC> x Command :
To delete a character to right of current cursor positions , this command is used.
Syntax :
<ESC> x <ESC> nx
2.<ESC> X Command :
To delete a character to left of current cursor positions , this command is used.
Syntax :
<ESC> X <ESC> nX
3.<ESC> dw Command :
This command is to delete a single word or number of words to right of current cursor
position.
Syntax :
<ESC> dw <ESC> ndw
4.db Command :
This command is to delete a single word to the left of the current cursor position.
Syntax :
<ESC> db <ESC> ndb
5.<ESC> dd Command :
This command is used to delete the current line (or) a number of line below the current
line.
Syntax :
<ESC> dd <ESC> ndd
6.<ESC> d$ Command :

10
This command is used to delete the text from current cursor position to last character
of current line.
Syntax :
<ESC> d$
SAVING AND QUITING FROM Vi :-
1.<ESC> w Command :
To save the given text present in the file.
Syntax : <ESC> : w
2.<ESC> q! Command :
To quit the given text without saving.
Syntax : <ESC> :q!
3.<ESC> wq Command :
This command quits the vi editor after saving the text in the mentioned file.
Syntax : <ESC> :wq
4.<ESC> x Command :
This command is same as ‘wq’ command it saves and quit.
Syntax : <ESC> :x
5.<ESC> q Command :
This command would quit the window but it would ask for again to save the file.
Syntax : <ESC> : q

WAP FOR BANKER’S ALGORITHM.


#include<stdio.h>
#include<conio.h>

11
main()
{
int
i,j,a=0,b=0,c=0,f[10],t[10][10],al[10][10],ta[10][10];
int a1[10][10], max[10][10], n[10][10], n1,p,k=0;
printf(“\n enter no.of resources”);
scanf(“%d”,n1);
printf(“\nenter the max no .of resources for each type”);
for(i=0;i<n1;i++)
scanf(“%d”,&t[b][i]);
printf(“\nenter no .of process”);
scanf(“%d”,&p);
printf(“\nenter allocation resources”);
for(i=0;i<p;i++)
{
f[i]=0;
for(j=0;j<n1;j++)
scanf(“%d”,&a1[i][j]);
}
for(i=0;i<p;i++)
for(j=0;j<n1;j++)
{
if(a1[i][j]<=t[b][j])
{
t[b][j]+=a1[i][j];
continue;
}
else
printf(“\n wrong resourcesallocation”);
printf(“\n chance of deadlock occurrence after
allocation”);
for(j=0;j<n1;j++)
printf(“%d”,a1[b][j]);
printf(“\n enter the max resources for every process”);
for(i=0;i<p;i++) for(j=0;j<n1;j++);
{
scanf(“%d”,&max[i][j]);
n[i][j]=max[i][j]-a1[i][j];
}
j=0;
printf(“\n needed resources for every process to start
execution”);
for(i=0;i<p;i++)
printf(“\n%d %d%d”,n[i][j],n[i][j+1],n[i][j+2]);
printf(“\n safe sequence the sequence of process to
compute their execution”);
for(a=0;a<(p-c);)
for(i=0;i<p;i++)
{
j=0;
b=0;

12
if(f[i]==0)
{
if(n[i][j]<=a1[b][j]&&n[i][j+1]<=a1[b][j+1]&&
n[i][j+2]<=a1[b][j+2])
{
printf(“\n process %d execution started and completed”,i+1);
for(k=0;k<n-1;k++)
a1[b][k]+=a1[i][k];
f[i]=1;
c++;
}
else
f[i]=0;
}
}
getch();
}

Input:
enter no.of resources
3
enter the max no .of resources for each type
10 5 7
enter the no .of process
5
enter allocation of resources
010200302211002
Output:
total available resources after allocation
332
enter the max resources for every process
753322902222433
needed resources for every process to start execution
743
122
600
011
431
Safe sequence ,the sequence of process to complete their
execution
Procee 2 execution started & completed
Procee 4 execution started & completed
Procee 5 execution started & completed
Procee 1 execution started & completed
Procee 3 execution started & completed

WAP TO IMPLEMENT PRODUCER CONSUMER PROBLEM USING SEMAPHORE.

13
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/sem.h>
#include<unistd.h>
#define num_loops 2
int main(int argc,char* argv[])
{
int sem_set_id;
int child_pid,i,sem_val;
struct sembuf sem_op;
int rc;
struct timespec delay;
clrscr();
sem_set_id=semget(ipc_private,2,0600);
if(sem_set_id==-1)
{
perror(“main:semget”);
exit(1);
}
printf(“semaphore set created,semaphore setid‘%d’\n ”,
sem_set_id);
child_pid=fork();
switch(child_pid)
{
case -1:
perror(“fork”);
exit(1);
case 0:
for(i=0;i<num_loops;i++)
{
sem_op.sem_num=0;
sem_op.sem_op=-1;
sem_op.sem_flg=0;
semop(sem_set_id,&sem_op,1);
printf(“producer:’%d’\n”,i);
fflush(stdout);
}
break;
default:
for(i=0;i<num_loops;i++)
{
printf(“consumer:’%d’\n”,i);
fflush(stdout);
sem_op.sem_num=0;
sem_op.sem_op=1;

14
sem_op.sem_flg=0;
semop(sem_set_id,&sem_op,1);
if(rand()>3*(rano_max14));
{
delay.tv_sec=0;
delay.tv_nsec=10;
nanosleep(&delay,null);
}
}
break;
}
return 0;
}

Output:
semaphore set created
semaphore set id ‘327690’
producer: ‘0’
consumer:’0’
producer:’1’

WAP TO IMPLEMENT READER WRITTER PROBLEM.

15
#include<stdio.h>
#include<semaphore.h>
#include<pthread.h>
#include<stdlib.h>
#define buffersize 10 pthread_mutex_t mutex; pthread_t tidP[20],tidC[20];
sem_t full,empty;
int counter;
int buffer[buffersize];
void initialize()
{
pthread_mutex_init(&mutex,NULL);
sem_init(&full,1,0);
sem_init(&empty,1,buffersize);
counter=0;
}
void write(int item)
{
buffer[counter++]=item;
}
int read()
{
return(buffer[--counter]);
}
void * producer (void * param)
{
int waittime,item,i; item=rand()%5;
waittime=rand()%5;
sem_wait(&empty);
pthread_mutex_lock(&mutex);
printf("\nProducer has produced item: % d\n",item);
write(item);
pthread_mutex_unlock(&mutex);
sem_post(&full);
}
void * consumer (void * param)
{
int waittime,item;
waittime=rand()%5;
sem_wait(&full);
pthread_mutex_lock(&mutex);
item=read();
printf("\nConsumer has consumed item: %d\n",item);
pthread_mutex_unlock(&mutex);
sem_post(&empty);
}

16
int main()
{
int n1,n2,i;
initialize();
printf("\nEnter the no of producers: ");
scanf("%d",&n1);
printf("\nEnter the no of consumers: ");
scanf("%d",&n2);
for(i=0;i<n1;i++)
pthread_create(&tidP[i] ,NULL,producer,NULL);
for(i=0;i<n2;i++)
pthread_create(&tidC[i] ,NULL,consumer,NULL);
for(i=0;i<n1;i++)
pthread_join(tidP[i],NULL);
for(i=0;i<n2;i++)
pthread_join(tidC[i],NULL);
//sleep(5);
exit(0);
}
OUTPUT:

Enter theno of producers: 2


Enter the no of consumers: 2
Producer has produced item: 3
Producer has produced item: 2
Consumer has consumed item: 2
Consumer has consumed item: 3

WAP TO IMPLEMENT PAGE REPLACEMENT ALGORITHM

17
1. [FIRST-IN-FIRST-OUT].
#include<stdio.h>
#include<conio.h>
main()
{
int a[10],b[5],n,t,i,c=0,k=0,j=0,m,x;
printf("\nenter no of requests");
scanf("%d",&n);
printf("\nenter no of frames");
scanf("%d",&t);
printf("enter requests one by one");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
if(k<t)
{
for(x=0;x<k;x++)
if(b[x]==a[i])
break;
if(x==k)
{
b[k]=a[i];
k++;
m=i;
}
}
}
for(i=m;i<n;i++)
{
for(x=0;x<k;x++)
if(b[x]==a[i])
break;
if(x==3)
{
c++;
b[j]=a[i];
j++;
if(j==t)
j=0;
}
}
printf("no of pagefaults=%d",c);
getch( );
}

Input:
enter no of requests

18
13
enter no of frames
3
enter requests one by one
2
5
3
2
0
1
3
5
4
2
2
3
4
Output:
no of pagefaults
6

2. LEAST RECENTLY USED


19
#include<stdio.h>
#include<conio.h>
main()
{
int a[50],b[40],c[15],n,f,i,y,s,p=0,k=0,l=0,m,x,g;
printf("\nenter no of requests");
scanf("%d",&n);
printf("\nenter no of frames");
scanf("%d",&f);
printf("enter requests one by one");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
if(k<f)
{
for(x=0;x<k;x++)
if(b[x]==a[i])
break;
if(x==k)
{
b[k]=a[i];
c[k]=a[i];
k++;
m=i;
}
}
}
for(i=m;i<n;i++)
{
printf("%d",a[i]);
for(x=0;x<f;x++)
printf("%4d",b[x]);
printf("---------");
for(x=0;x<f;x++)
if(b[x]==a[i])
break;
if(x==f)
{
p++;
l=0;
for(g=i-1;g>0;g--)
for(x=0;x<f&&l<=f;x++)
if(a[g]==b[x]&&c[x]==b[x])
{
y=c[x];
c[x]=-1;
l++;
}
if(l==f)
{
for(s=0;s<f;s++)

20
if(y==b[s])
b[s]=a[i];
}
if(l<f)
for(s=0;s<f;s++)
if(c[s]!=-1)
{
b[s]=a[i];
break;
}
for(s=0;s<f;s++)
c[s]=b[s];
}
for(s=0;s<f;s++)
printf("%4d",b[s]);
printf("\n");
}
printf("\n no of pagefaults=%d",p);
getch();
}

Input:
enter no of requests
13
enter no of frames
3
enter requests one by one
2532013542234
Output:
3 2 5 3 ------------ 2 5 3
2 2 5 3 ------------ 2 5 3
0 2 5 3 ------------ 2 0 3
1 2 0 3 ------------ 2 0 1
3 2 0 1 ------------ 2 0 1
5 2 0 1 ------------ 2 5 1
4 2 5 1 ------------ 2 5 4
2 2 5 4 ------------ 2 5 4
2 2 5 4 ------------ 2 5 4
3 2 5 4 ------------ 2 3 4
4 2 3 4 ------------ 2 3 4
No of pagefaults=7

WAP TO IMPLEMENT I/O SYSTEM CALLS.

21
1. READING FROM A FILE.

#include<stdio.h>

int main()

char str[100];

FILE *fp;

fp=fopen("file1.dat","r");

while(!feof(fp))

fscanf(fp,"%s",str);

printf(" %s ",str);

fclose(fp);

OUTPUT:
[admin@lab1cab01 admin]$ vi read1.c

[admin@lab1cab01 admin]$ gcc read1.c

[admin@lab1cab01 admin]$ ./a.out

hai this is a program to read the content of the file.

2. WRITTING INTO A FILE.

22
#include<stdio.h>

int main()

char str[100];

FILE *fp;

printf("Enter the string");

gets(str);

fp=fopen("file1.dat","w+");

while(!feof(fp))

fscanf(fp,"%s",str);

fprintf(fp,"%s",str);

OUTPUT:
[admin@lab1cab01 admin]$ gcc write.c

[admin@lab1cab 01 admin]$ ./a.out

Enter the string: os lab

[admin@lab1cab01 admin]$ vi file1.dat

os lab

WAP FOR CPU SCHEDULING.


1. FIRST COME FIRST SERVED(FCFS)

23
#include<stdio.h>
struct process
{
int id,wait,ser,tottime;
}p[20];
main()
{
int i,n,j,totalwait=0,totalser=0,avturn,avwait;
printf("enter number of process");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("enter process_id");
scanf("%d",&p[i].id);
printf("enter process service time");
scanf("%d",&p[i].ser);
}
p[1].wait=0;
p[1].tottime=p[1].ser;
for(i=2;i<=n;i++)
{
for(j=1;j<i;j++)
{
p[i].wait=p[i].wait+p[j].ser;
}
totalwait=totalwait+p[i].wait;
p[i].tottime=p[i].wait+p[i].ser;
totalser=totalser+p[i].tottime;
}
avturn=totalser/n;
avwait=totalwait/n;
printf("Id\tservice\twait\ttotal");
for(i=1;i<=n;i++)
{
printf("\n%d\t%d\t%d\t%d\n",p[i].id,p[i].ser,p[i].wait,p[i].tottime);
}
printf("average waiting time %d\n",avwait);
printf("average turnaround time %d\n",avturn);
}

OUTPUT:
[admin@lab1cab5 admin]$ gcc fcfs.c
[admin@lab1cab5 admin]$ ./a.out
enter number of process4
enter process_id901
enter process service time4
enter process_id902
enter process service time3
enter process_id903
enter process service time5
enter process_id904

24
enter process service time2
Id service wait total
901 4 0 4
902 3 4 7
903 5 7 12
904 2 12 14
average waiting time 5
average turnaround time 8
[admin@lab1cab5 admin]$

2. SHORTEST JOB FIRST(SJF)


#include<stdio.h>
struct ff
{
int pid,ser,wait;
}p[20];
struct ff tmp;
main()
{
int i,n,j,tot=0,avwait,totwait=0,tturn=0,aturn;
printf("enter the number of process");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("enter process id");
scanf("%d",&p[i]);
printf("enter service time");
scanf("%d",&p[i].ser);
p[i].wait=0;
}
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(p[i].ser>p[j].ser)
{
tmp=p[i];
p[i]=p[j];
p[j]=tmp;
}
}
}
printf("PID\tSER\tWAIT\tTOT\n");
for(i=0;i<n;i++)
{
tot=tot+p[i].ser;
tturn=tturn+tot;
p[i+1].wait=tot;
totwait=totwait+p[i].wait;
printf("%d\t%d\t%d\t%d\n",p[i].pid,p[i].ser,p[i].wait,tot);
}

25
avwait=totwait/n;
aturn=tturn/n;
printf("TOTAL WAITING TIME :%d\n",totwait);
printf("AVERAGE WAITING TIME : %d\n",avwait);
printf("TOTAL TURNAROUND TIME :%d\n",tturn);
printf("AVERAGE TURNAROUND TIME:%d\n",aturn);
}

OUTPUT:
[root@lab1cab5 home]# ./a.out
enter the number of process4
enter process id701
enter service time6
enter process id702
enter service time4
enter process id703
enter service time8
enter process id704
enter service time1
PID SER WAIT TOT
704 1 0 1
702 4 1 5
701 6 5 11
703 8 11 19
TOTAL WAITING TIME :17
AVERAGE WAITING TIME : 4
TOTAL TURNAROUND TIME :36
AVERAGE TURNAROUND TIME:9

3. PRIORITY
#include<stdio.h>
main()
{
float avgwt,avgtt;
char pname[10][10],c[10][10];
int wt[10], tt[10], bt[10], pt[10], t, q, i, n, sum=0, sbt=0, ttime, j, ss=10;
printf("\n\n Enter the number of processes : ");
scanf("%d",&n);
printf("\n\n Enter the NAME and BURSTTIME ");
for(i=0;i<n;i++)
{
printf("\n\n NAME : ");
scanf("%s",&pname[i]);
printf("\n\n BURSTTIME : ");
scanf("%d",&bt[i]);
}
printf("\n\n Enter the priorities of the processes ");
for(i=0;i<n;i++)

26
{
printf("\n\n Priority of process%d : ",i+1);
scanf("%d",&pt[i]);
}
for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
{
if(pt[i]>pt[j])
{
t=pt[i];
pt[i]=pt[j];
pt[j]=t;
q=bt[i];
bt[i]=bt[j];
bt[j]=q;
strcpy(c[i],pname[i]);
strcpy(pname[i],pname[j]);
strcpy(pname[j],c[i]);
}
}
wt[0]=0;
for(i=0;i<n;i++)
{
wt[i+1]=wt[i]+bt[i];
sum=sum+wt[i];
sbt=sbt+wt[i+1];
tt[i]=wt[i]+bt[i];
ss=ss+bt[i];
}
printf("\n\n GANTT CHART");
printf("\n---------------------------------\n");
for(i=0;i<n;i++)
printf("|\t%s\t",pname[i]);
printf("\n---------------------------------\n");
for(i=0;i<n;i++)
printf("%d\t\t",wt[i]);
printf("%d\n",ss);
printf("\n---------------------------------\n");
printf("\n\n Total WAITING TIME of the process = %d",sum);
printf("\n\n Total TURNAROUND TIME of the process = %d",sbt); avgwt=(float)sum/n;
avgtt=(float)sbt/n;
printf("\n\n Average WAITING TIME of the process = %f",avgwt);
printf("\n\n Average TURNAROUND TIME of the process = %f",avgtt);
}

OUTPUT:
[root@localhost ~]# ./a.out Enter the number of processes : 4 Enter the NAME and BURSTTIME
NAME : p1
BURSTTIME : 8
NAME : p2
BURSTTIME : 3

27
NAME : p3
BURSTTIME : 6
NAME : p4
BURSTTIME : 1
Enter the priorities of the processes
Priority of process1 : 1
Priority of process2 : 5
Priority of process3 : 2
Priority of process4 : 4

GANTT CHART
------------------------------------------------------
| p1 | p3 | p4 | p2
------------------------------------------------------
0 8 14 15 28
Total WAITING TIME of the process = 37
Total TURNAROUND TIME of the process = 55
Average WAITING TIME of the process = 9.250000
Average TURNAROUND TIME of the process = 13.750000

4. ROUND ROBIN
#include<stdio.h>
main() {
int pt[10][10],a[10][10],at[10],pname[10][10],i,j,n,k=0,q,sum=0;
float avg;
printf("\n\n Enter the number of processes : ");
scanf("%d",&n);
for(i=0;i<10;i++)
{
for(j=0;j<10;j++)
{
pt[i][j]=0; a[i][j]=0;
}
}
for(i=0;i<n;i++)
{
j=0;
printf("\n\n Enter the process time for process %d : ",i+1);
scanf("%d",&pt[i][j]);
}
printf("\n\n Enter the time slice : ");
scanf("%d",&q); printf("\n\n");
for(j=0;j<10;j++)
{
for(i=0;i<n;i++)
{
a[2*j][i]=k;
if((pt[i][j]<=q)&&(pt[i][j]!=0))
{

28
pt[i][j+1]=0;
printf(" %d P%d %d\n",k,i+1,k+pt[i][j]);
k+=pt[i][j];
a[2*j+1][i]=k;
}
else if(pt[i][j]!=0)
{
pt[i][j+1]=pt[i][j]-q;
printf(" %d P%d %d\n",k,i+1,(k+q));
k+=q;
a[2*j+1][i]=k;
}
else
{
a[2*j][i]=0;
a[2*j+1][i]=0;
}
}
}
for(i=0;i<n;i++)
sum+=a[0][i];
for(i=0;i<n;i++) {
for(j=1;j<10;j++)
{
if((a[j][i]!=0)&&(a[j+1][i]!=0)&&((j+1)%2==0))
sum+=((a[j+1][i]-a[j][i]));
}
}
avg=(float)sum/n;
printf("\n\n Average waiting time = %f msec",avg);
sum=avg=0;
for(j=0;j<n;j++)
{
i=1;
while(a[i][j]!=0) i+=1;
sum+=a[i-1][j];
}

OUTPUT:
[root@localhost ~]# ./a.out
Enter the number of processes : 4
Enter the process time for process 1 : 8
Enter the process time for process 2 : 3
Enter the process time for process 3 : 6
Enter the process time for process 4 : 1
Enter the time slice : 2
0 P1 2
2 P2 4
4 P3 6
6 P4 7
7 P1 9

29
9 P2 10
10 P3 12
12 P1 14
14 P3 16
16 P1 18
Average waiting time = 8.250000 msec
Average turnaround time = 12.750000 msec

WAP FOR MEMORY MANAGEMENT


1. PAGING SIMULATION

30
#include<stdio.h>
struct page
{
int pno;
int psize;
int nopages;
int sms;
int mms;
int sme;
int mme;
int residentbit;
} table[30];
void main()
{
int ch, resibit, i=0, ss=4000, se=7000, ms=6000, me=9000, pn,j;
int flag;
printf("\n\n\t\t---------------------------");
printf("\n\n\t\tPAGING OPERATIONS");
printf("\n\n\t\t---------------------------");
printf("\n\nDETAILS OF SECONDARY MEMORY");
printf("\n---------------------------------");
printf("\n\nstarting address of secondary memory=%d", ss);
printf("\n\nending address of secondary memory=%d", se);
printf("\n------------------------------------------------");
printf("\n\nDETAILS OF MAIN MEMORY");
printf("\n---------------------------------");
printf("\n\nstarting address of main memory=%d", ms);
printf("\n\nending address of main memory=%d", me);
printf("\n------------------------------------------------");
printf("\n\nPAGE SIZE=50");
printf("\n----------------------");
do
{
printf("\n\nMENU\n\n");
printf("\n1.LOAD");
printf("\n2.DISPLAY");
printf("\n3.EXECUTE");
printf("\n4.EXIT");
printf("\n\nENTER THE CHOICE");
scanf("%d", &ch);
switch(ch)
{
case 1:
{
printf("\n\n\nLOAD");
printf ("ENter the process number");
scanf("%d", &table[i].pno);
printf("\nENter the process size\t");
scanf("%d", &table[i].psize);

31
table[i].nopages=table[i].psize/50;
if(table[i].psize%50!=0)
table[i].nopages++;
table[i].residentbit=0;
table[i].sms=ss;
table[i].sme=table[i].sms+table[i].nopages*50;
ss=table[i].sme;
table[i].mms=-1;
table[i].mme=-1;
i++;
break;
}
case 2:
{
printf("\n\n\nDISPLAY");
printf("\n------------------------------------------------");
printf("\n PNO PSIZE NOPAGES SMS MMS SME MME RSBIT");
printf("\n-------------------------------------------------");
for (j=0;j<i;j++)
{
printf("\n%d", table[j].pno);
printf("\t %d", table[j].psize);
printf("\t %d", table[j].nopages);
printf("\t %d", table[j].sms);
printf("\t %d", table[j].mms);
printf("\t %d", table[j].sme);
printf("\t %d", table[j].mme);
printf("\t %d", table[j].residentbit);
}
printf("\n ----------------------------------");
break;
}
case 3:
{
flag=0;
printf("\n\n\nEXECUTE");
printf("\nENTER THE PROCESS NUMBER TO BE EXECUTED:");
scanf("%d", &pn);
for(j=0;j<i;j++)
{
if(pn==table[j].pno)
{
flag=1;
if(table[j].residentbit==0);
{
table[j].mms=ms;
table[j].mme=table[j].mms+table[j].nopages*50;
ms=table[j].mme;
table[j].residentbit=1;

32
}}}
if(flag==0)
{
printf("\nPROCESS NOT PRESENT IN THE MAIN MEMORY FOR EXECUTION");
}
else
{
printf("\nPROCESS PRESENT IN THE MAIN MEMORY FOR EXECUTION");
}
break;
}
case 4:
{
exit(0);
break;
}}}
while(ch!=4);
}

2. MEMORY MANAGEMENT STRATEGIES


#include<stdio.h>
#define MAX 5
int spaceleft;
struct{
int startbyte;
int endbyte;
int occupied; // 1 means occupied & 0 means not occupied
}a[MAX];
void initialize();
void input();
void firstfit();
void display();
int main()
{
initialize();
input();
printf("\nBefore first fit allocation-");
display();
firstfit();
printf("\nAfter first fit allocation-");
display();
printf(“The space left is =%d”,spaceleft);
}
void initialize()
{
int i;
for(i=0;i<MAX;i++)
a[i].occupied=0;
}
void input()
{
int i,option,choice;
for(i=0;i<MAX;i++)
{
printf("\n Choose 1 for allocation of element \n Choose 2 for hole");

33
printf("\n Your choice=");
scanf("%d",&choice);
printf("\nEnter the Startbyte and Endbyte address:");
scanf("%d %d",&a[i].startbyte,&a[i].endbyte);
if(choice==1)
a[i].occupied=1;
}
}
void firstfit()
{
int i,space,hole,flag=0;
printf("\nNew entry's space requirement:");
scanf("%d",&space);
for(i=0;i<MAX;i++)
{
if(a[i].occupied==0)
{
hole=a[i].endbyte-a[i].startbyte;
if(space < hole)
{
a[i].endbyte=a[i].startbyte+space;
a[i].occupied=1;
flag=1;
spaceleft=hole-space;
i=MAX; //For exiting from the loop
}
}
}
if(flag==0)
{
printf("\nSpace not available");
exit(0);
}
}
void display()
{
int i;
printf("\n_______________________________");
printf("\nIndex \t Startbyte \t Endbyte");
printf("\n-------------------------------");
for(i=0;i<MAX;i++)
printf("\n%d \t %d \t\t %d",i,a[i].startbyte,a[i].endbyte);
printf("\n");
}

34

You might also like