OS Rec
OS Rec
OS Rec
Page No.
1. Calendar
(i) TASK
SYNTAX
-s
the default.)
-m
-j
January 1).
-y
OUTPUT
8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31
(ii) TASK
SYNTAX
SMVEC
Page No.
(iii). TASK
year.
SYNTAX
8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
(iv). TASK
month.
SYNTAX
: cal -3
EXPLANATION : Displays the calendar of the previous, current & next month
on the screen.
OUTPUT
July 2016
Aug 2016
Su Mo Tu We Th Fr Sa
We Th Fr Sa
1
1
5 6 7
10 11 12 13
Su Mo Tu We Th Fr Sa
4
Su Mo Tu
1
6
8
9 10 11
12 13 14 15 16 17 18
16 17 18 19 20
10 11 12 13 14 15 16
14 15
19 20 21 22 23 24 25
24 25 26 27
17 18 19 20 21 22 23
21 22 23
26 27 28 29 30
30 31
24 25 26 27 28 29 30
28 29
31
SMVEC
Page No.
2. Date
(i). TASK
SYNTAX
EXPLANATION : This command displays the current system date and time on
the screen.
OUTPUT
(ii). TASK
SYNTAX
: date +%m
EXPLANATION
screen.
OUTPUT
(iii). TASK
SYNTAX
: date +%h
EXPLANATION
month on the screen
OUTPUT
July
(iv). TASK
SYNTAX
: date +%d
EXPLANATION
the screen.
OUTPUT
SMVEC
Page No.
(v). TASK
SYNTAX
: date +%Y
(vi). TASK
SYNTAX
: date +%H
EXPLANATION
hour) on the screen.
OUTPUT
10
(vii). TASK
SYNTAX
: date +%a
EXPLANATION
: This command displays the current system
weekday (in abbreviated
form) on the screen.
OUTPUT
(viii). TASK
SYNTAX
: date +%S
EXPLANATION
: This command displays the current system
second on the screen.
OUTPUT
SMVEC
Page No.
(ix). TASK
SYNTAX
: date +%y
EXPLANATION
on the screen.
OUTPUT
16
3. List
(i). TASK
SYNTAX
: ls
EXPLANATION
by default) on the
OUTPUT
: [smvec8@localhost aravindan~]$ ls
aravind
abc.txt
line.sh
(ii). TASK
pqr.txt
sort.sh
a.txt
level.txt
b.txt
SYNTAX
: ls - l
EXPLANATION
the screen.
OUTPUT
xyz.txt
Total 28
-rw-rw-r--1 smvec8
smvec8
-rw-rw-r--1 smvec8
f1.sh
SMVEC
Page No.
(iii). TASK
SYNTAX
: ls - r
EXPLANATION
the screen.
OUTPUT
xyz.txt sort.sh
abc.txt b.txt
(iv). TASK
SYNTAX
: ls - s
EXPLANATION
OUTPUT
: [smvec8@localhost aravindan~]$ ls - s
4 abc.txt 8 b.txt
(v). TASK
8 pqr.txt 8 line.sh
SYNTAX
EXPLANATION
screen.
OUTPUT
8 a.txt
: ls - S
: Lists information about the files in sorted order on the
: [smvec8@localhost aravindan~]$ ls - S
SMVEC
Page No.
4. Copy
TASK
SYNTAX
: cp f1 f2
EXPLANATION
: Copies f1 to f2
OUTPUT
cat b.txt
Hello guys!
How do you do?
5. Script/Edit
TASK
file
SYNTAX
: script scriptname
..
..
..
Exit.
/vi filename
EXPLANATION
OUTPUT
SMVEC
Page No.
6. Remove
TASK
: To remove files.
SYNTAX
: rm filename
EXPLANATION
OUTPUT
: [smvec8@localhost aravindan~]$ ls
aravind
abc.txt
line.sh
xyz.txt
pqr.txt
sort.sh
a.txt
level.txt
b.txt
[smvec8@localhost aravindan~]$
rm
level.txt
[smvec8@localhost aravindan~]$ ls
aravind
abc.txt
pqr.txt
xyz.txt
a.txt
sort.sh
line.sh
b.txt
7. Rename
TASK
: To rename files.
SYNTAX
: mv f1 f2
EXPLANATION
OUTPUT
: [smvec8@localhost aravindan~]$ ls
aravind
abc.txt
line.sh
xyz.txt
pqr.txt
sort.sh
a.txt
level.txt
b.txt
[smvec8@localhost aravindan~]$
mv pqr.txt
rqp.txt
[smvec8@localhost aravindan~]$ ls
aravind
abc.txt
rqp.txt
xyz.txt
a.txt
sort.sh
b.txt
line.sh
SMVEC
Page No.
8. Make Directory
TASK
SYNTAX
: mkdir
EXPLANATION
OUTPUT
: [smvec8@localhost aravindan~]$ ls
aravind
abc.txt
line.sh
xyz.txt
pqr.txt
sort.sh
a.txt
level.txt
b.txt
[smvec8@localhost aravindan~]$
mkdir
student
[smvec8@localhost aravindan~]$ ls
aravind
abc.txt
rqp.txt
xyz.txt
a.txt
sort.sh
b.txt
line.sh
student
9. Remove Directory
TASK
SYNTAX
: rmdir
EXPLANATION
OUTPUT
: [smvec8@localhost aravindan~]$ ls
aravind
abc.txt
line.sh
xyz.txt
pqr.txt
sort.sh
a.txt
level.txt
b.txt
[smvec8@localhost aravindan~]$
rmdir
student
[smvec8@localhost aravindan~]$ ls
aravind
abc.txt
rqp.txt
xyz.txt
a.txt
sort.sh
b.txt
line.sh
SMVEC
Page No.
10. Pwd
TASK
SYNTAX
: pwd
EXPLANATION
directory.
OUTPUT
SYNTAX
: cd
EXPLANATION
OUTPUT
SYNTAX
: cat>filename
EXPLANATION
OUTPUT
SMVEC
Page No.
(ii). TASK
SYNTAX
: cat filename
EXPLANATION
OUTPUT
(iii). TASK
SYNTAX
: cat f1 f2>f3
EXPLANATION
OUTPUT
abc.txt >b.txt
(iv). TASK
SYNTAX
: cat n filename
EXPLANATION
OUTPUT
SMVEC
Page No.
(v). TASK
SYNTAX
: cat f1>>f2
EXPLANATION
to f2.
OUTPUT
a.txt>>xyz.txt
13. Whoami
TASK
SYNTAX
: whoami
EXPLANATION
login.
OUTPUT
14. Who
TASK
SYNTAX
: who
EXPLANATION
OUTPUT
in.
smvec8
pts/89
2016-07-18
smvec36
pts/63
2016-07-18
smvec92
pts/103
2016-07-14
16:04
15:28(172.17.10.75)
21:13
SMVEC
Page No.
15. Finger
TASK
users.
SYNTAX
: finger
EXPLANATION
: This command is used to display detailed info
about all the system users.
OUTPUT
Office
Login
Name
OfficePhone
Host
smvec8
Tty
Idle
LoginTime
pts/89
Jul 18 16:04
(172.17.25.58)
smvec36
pts/63
smvec92
pts/103
Jul 18 15:28
(172.17.10.75)
3d
Jul 14 21:13
16. Last
TASK
SYNTAX
: last
EXPLANATION
: This command is used to display the list of last
logged-in users for a
month.
OUTPUT
smvec16
still logged in
pts/49
smvec8
still logged in
pts/89
smvec21
16:04(00:00)
pts/78
172.17.10.69
Mon
smvec75
20:02(00:30)
pts/5
172.17.10.31
Thu
wtmp
begins
Thu
172.17.25.51
Mon
Mon
Jul 18 16:09
Jul 18 16:04
Jul 18 16:04Jul 14 19:31-
SMVEC
Page No.
17. Head
TASK
SYNTAX
: head filename
EXPLANATION
OUTPUT
18. Tail
TASK
SYNTAX
: tail filename
EXPLANATION
OUTPUT
19. Clear
TASK
SYNTAX
: clear
EXPLANATION
OUTPUT
SMVEC
Page No.
20. Sort
(i)
TASK
order.
SYNTAX
: sort filename
EXPLANATION
OUTPUT
(ii)
TASK
order.
SYNTAX
: sort r filename
EXPLANATION
order.
OUTPUT
21. And/Semicolon
TASK
SYNTAX
EXPLANATION
OUTPUT
date (whoami;date)
July
11:05:15
IST
2016
SMVEC
Page No.
22. Or
TASK
SYNTAX
: cmd1 || cmd2
EXPLANATION
OUTPUT
date
smvec8
23. Alias
TASK
commands
SYNTAX
: alias name=value
EXPLANATION
OUTPUT
p=pwd
24. Cut
(i)
TASK
SYNTAX
EXPLANATION
: This command displays the characters of a
particular column in the
specified field.
OUTPUT
abc.txt
SMVEC
Page No.
(ii)
TASK
SYNTAX
: cut -c filename
EXPLANATION
: This command displays the characters of a
particular column in the
specified field.
OUTPUT
abc.txt
25. Touch
TASK
SYNTAX
: touch filename
EXPLANATION
OUTPUT
: [smvec8@localhost aravindan~]$ ls
aravind
abc.txt
line.sh
xyz.txt
pqr.txt
sort.sh
a.txt
level.txt
b.txt
[smvec8@localhost aravindan~]$
touch
hello.txt
[smvec8@localhost aravindan~]$ ls
aravind
abc.txt
rqp.txt
xyz.txt
a.txt
sort.sh
b.txt
line.sh
hello.txt
SMVEC
Page No.
26. Uniq
TASK
SYNTAX
: uniq option f1 f2
EXPLANATION
f2 that are in f1.
OUTPUT
cat
xyz.txt
Welcome
This is OS lab
[smvec8@localhost aravindan~]$ uniq d
abc.txt xyz.txt
[smvec8@localhost aravindan~]$
cat
xyz.txt
This is OS lab
27. Echo
TASK
SYNTAX
: echo
EXPLANATION
OUTPUT
all
Hello all
SMVEC
Page No.
(i)
TASK
: Used to display the number of lines, words and
characters in file.
SYNTAX
: wc filename
EXPLANATION
OUTPUT
file.
2
(ii) TASK
23
a.txt
SYNTAX
: wc l filename
EXPLANATION
OUTPUT
(iii) TASK
SYNTAX
: wc m filename
EXPLANATION
OUTPUT
(iv) TASK
SYNTAX
: wc w filename
EXPLANATION
OUTPUT
SMVEC
Page No.
29. Grep
(i)
TASK
SYNTAX
EXPLANATION
file.
OUTPUT
sub.txt
(ii)
TASK
: Used to search for a pattern in a file and displays the
no of times it
repeated in the file.
SYNTAX
EXPLANATION
in the file.
OUTPUT
sub.txt
(iii) TASK
with line number.
SYNTAX
EXPLANATION
file with line number.
OUTPUT
System sub.txt
SMVEC
Page No.
(iv) TASK
the case.
SYNTAX
EXPLANATION
: Displays the lines that have the given pattern
irrespective of case in the file.
OUTPUT
sub.txt
Ss Sound system
30. Read
TASK
SYNTAX
: read identifier
EXPLANATION
OUTPUT
31. Fgrep
(i) TASK
SYNTAX
: fgrep pattern f1 f2
EXPLANATION
the two files.
OUTPUT
sub.txt lmn.txt
SMVEC
Page No.
(ii) TASK
: Used to search for a pattern in the two files and
displays the no of times
it repeated in the files.
SYNTAX
: fgrep -c pattern f1 f2
EXPLANATION
: Displays the no of times the given pattern is
repeated in the files.
OUTPUT
sub.txt lmn.txt
sub.txt : 2
lmn.txt : 1
(iii) TASK
: Used to search for a pattern in two files and
display along with line no.
SYNTAX
: fgrep -n pattern f1 f2
EXPLANATION
the files with line
OUTPUT
: [smvec8@localhost aravindan~]$ fgrep n
System sub.txt lmn.txt
sub.txt: 1: OS Operating System
sub.txt: 8: SS System Software
: fgrep -i pattern f1 f2
EXPLANATION
: Displays the lines that have the given pattern
irrespective of case in the
file.
OUTPUT
System sub.txt
SMVEC
Page No.
32. Pipe
TASK
command.
SYNTAX
: cmd1|cmd2|cmd3
EXPLANATION
another command
OUTPUT
33. Tee
TASK
: Used to read the standard input and then write to
standard output of file.
SYNTAX
EXPLANATION
specified file
OUTPUT
pqr.txt
34. Write
TASK
SYNTAX
: write login_name
EXPLANATION
logged in
OUTPUT
pqr.txt
SMVEC
Page No.
TASK
SYNTAX
: mail username
EXPLANATION
OUTPUT
user
TASK
SYNTAX
EXPLANATION
OUTPUT
14/651
SMVEC
Page No.
SYNTAX
: tty
EXPLANATION
OUTPUT
37. Expression
TASK
SYNTAX
: expr expression
EXPLANATION
OUTPUT
+ 10
38. Free
TASK
: Used to display the total amount of free and used
physical and swap
memory in the system.
SYNTAX
: free
EXPLANATION
: Displays the total amount of free and used
physical and swap
memory in the system.
OUTPUT
total
used
free
shared
buffers
cached
Mem:
0
232936
2074968
1530188
544780
962024
-/+ buffers/cache:
Swap:
4128760
335228
1739740
80
4128680
SMVEC
Page No.
39. Ping
TASK
to the user.
SYNTAX
: ping Ip - address
EXPLANATION
: Pings the ip address and checks if the system is
connected or not to host.
OUTPUT
172.17.28.123
^Z
[1]+ Stopped
ping 172.17.28.123
40. Password
TASK
SYNTAX
: passwd
EXPLANATION
OUTPUT
SMVEC
Page No.
41. Uname
TASK
SYNTAX
: uname
EXPLANATION
OUTPUT
42. Sequence
TASK
SYNTAX
EXPLANATION
ending value.
OUTPUT
SYNTAX
EXPLANATION
the first line.
OUTPUT
: vi +filename
: Opens an existing file and places the cursor at
: [smvec8@localhost aravindan~]$ vi +a.txt
Welcome
Hello all!
How is it going?
SMVEC
Page No.
44. Compare
TASK
SYNTAX
EXPLANATION
the lines that match
OUTPUT
b.txt
Hello all!
How is it going?
45. Basic Calculator
TASK
calculator
SYNTAX
: bc
EXPLANATION
OUTPUT
: [smvec8@localhost aravindan~]$ bc
10 + 10
20
[smvec8@localhost aravindan~]$ bc
10 \* 10
100
46. Exit
TASK
SYNTAX
: logout
EXPLANATION
OUTPUT
SMVEC
Page No.
47. Man
TASK
SYNTAX
: man command_name
EXPLANATION
OUTPUT
ECHO(1)
User Commands
Name
echo display a line of text
Synopsis
echo [OPTION]. [STRING].
Description
Echo the STRING(s) to standard output.
-n do not output the trailing newline
-e enable interpretation of backslash escapes
-E disable interpretation of backslash escapes (default)
--help display this help and exit
.
.
.
(END)
ECHO(1)
SMVEC
Page No.
ODD OR EVEN
Code:
[smvec8@localhost aravindan]$ vi odev.sh
echo "Enter the number : "
read n
rem=$(($n % 2))
if [ $rem -eq 0 ];
then
echo "even"
else
echo "odd"
fi
Output:
[smvec8@localhost aravindan]$ sh odev.sh
Enter the number :
5
odd
[smvec8@localhost aravindan]$ sh odev.sh
Enter the number :
8
even
SMVEC
Page No.
SMVEC
Page No.
Output:
[smvec8@localhost aravindan]$ sh lecho.sh
1. WHO AM I ?
2. WHO IS LOGGED IN?
3. DATE
1
smvec8
[smvec8@localhost aravindan]$ sh lecho.sh
1. WHO AM I ?
2. WHO IS LOGGED IN?
3. DATE
2
root
:0
2016-08-22 15:24
smvec44 pts/11
smvec8 pts/1
smvec45 pts/22
2016-09-01 20:36
2016-09-06 16:40 (172.17.25.57)
2016-08-25 20:05
SMVEC
Page No.
Output:
[smvec8@localhost aravindan]$ sh prime.sh
Enter the number
45
45 is not a prime number
[smvec8@localhost aravindan]$ sh prime.sh
Enter the number
23
23 is a Prime number
SMVEC
Page No.
STRING COMPARE
Code:
[smvec8@localhost aravindan]$ vi strcp.sh
echo "STRING 1"
read str1
echo "STRING 2"
read str2
if [ $str1 = $str2 ]
then
echo "The Strings are Equal"
else
echo "The Strings are Not Equal"
fi
SMVEC
Page No.
Output:
[smvec8@localhost aravindan]$ sh strcp.sh
STRING 1
Hello
STRING 2
Hello
The Strings are Equal
[smvec8@localhost aravindan]$ sh strcp.sh
STRING 1
Hello
STRING 2
Bye
The Strings are Not Equal
[smvec8@localhost aravindan]$ sh strcp.sh
STRING 1
Hello
STRING 2
hello
The Strings are Not Equal
SMVEC
Page No.
SMVEC
Page No.
Output:
[smvec8@localhost aravindan]$ sh gttn.sh
Enter the values
4
5
9
C is greatest
[smvec8@localhost aravindan]$ sh gttn.sh
Enter the values
74
15
54
A is greatest
[smvec8@localhost aravindan]$ sh gttn.sh
Enter the values
75
98
25
B is greatest
SMVEC
Page No.
SMVEC
Page No.
PASSWORD CHECK
Code:
[smvec8@localhost aravindan]$ vi pass.sh
echo "Password check:"
read pw
clear
echo "Password check:"
echo "Enter the password"
read n
if [ $n = $pw ]
then
echo "Valid password"
else
echo "Invalid password"
fi
SMVEC
Page No.
Output:
[smvec8@localhost aravindan]$ sh pass.sh
Password check:
Password
Password check:
Enter the password
Password
Valid password
[smvec8@localhost aravindan]$ sh pass.sh
Password check:
Password
Password check:
Enter the password
password
Invalid password
SMVEC
Page No.
SMVEC
Page No.
Output:
[smvec8@localhost aravindan]$ cc fork.c
[smvec8@localhost aravindan]$ ./a.out
After Fork Called
Current Process1 id :10538
After Fork Called
Current Process2 id :16520
New fork id 16571
SMVEC
Page No.
SMVEC
Page No.
Output:
[smvec8@localhost aravindan]$ cc stat.c
[smvec8@localhost aravindan]$ ./a.out
INODE NUMBER IS 16254000
IDENTIFIER OF THE DEVICE IS 64768
USER IDENTIFIER OF THE OWNER IS 547
BLOCKSIZE FOR THE SYSTEM IS 10000
LAST FILE MODIFY IS Tue Aug 8 14:30:50 2016
SMVEC
Page No.
SMVEC
Page No.
Output:
[smvec8@localhost aravindan]$ read.c
[smvec8@localhost aravindan]$./a.out <filename.c>
[smvec8@localhost aravindan]$ ./a.out fork.c
#include<sys/types.h>
#include<stdio.h>
int main()
{
int pid, p1,p2;
pid=fork();
printf("\n After Fork Called ");
if(pid > 0)
{
wait(10);
p1=getpid();
printf("\n Current Process1 id : %d ",p1);
printf("\n New fork id %d ",pid);
}
else if(pid==0)
{
p1=getpid();
printf("\n Current Process2 id : %d ",p1);
}
else if(pid==-1)
{
printf("\n Error in Creation ");
}
return 0;
}
SMVEC
Page No.
SMVEC
Page No.
Output:
[smvec8@localhost aravindan]$ cc append.c
[smvec8@localhost aravindan]$ usage : ./a.out<filename.c>
[smvec8@localhost aravindan]$ ./a.out fork.c
#include<sys/types.h>
#include<stdio.h>
int main()
{
int pid, p1,p2;
pid=fork();
printf("\n After Fork Called ");
if(pid > 0)
{
wait(10);
p1=getpid();
printf("\n Current Process1 id : %d ",p1);
printf("\n New fork id %d ",pid);
}
else if(pid==0)
{
p1=getpid();
printf("\n Current Process2 id : %d ",p1);
}
else if(pid==-1)
{
printf("\n Error in Creation ");
}
return 0;
}Appended text
SMVEC
Page No.
dp
great.sh
append.c
evenodd
jj
even.sh
john
[smvec8@localhost aravindan]$
SMVEC
Page No.
SMVEC
Page No.
Output:
[smvec8@localhost aravindan]$ cc dir.c
[smvec8@localhost aravindan]$ ./a.out
d:\
new.c
primeornot.sh
fork.c
third.c
SMVEC
Page No.
Simulation of ls Command
Code:
#include<dirent.h>
// Main Program
int main()
{
struct dirent **namelist;
int status,i;
char pathname[100];
getwd(pathname);
status=scandir(pathname,&namelist,0,alphasort); //to scan a directory
if(status <0)
printf("cannot find the current directory");
else
{
for(i=0;i<status;i++)
{
printf("%s\n",namelist[i]->d_name);
}
}
}
SMVEC
Page No.
Output :
[smvec8@localhost aravindan]$ cc ls.c
[smvec8@localhost aravindan]$ ./a.out
Apple.c
Direct.c
Echo
File1.doc
Ring.h
SMVEC
Page No.
SMVEC
Page No.
while(fgets(fline,buffer,fp)!=NULL)
{
// argv[2] is search string
if(strstr(fline,argv[2])!=NULL)
{
printf("Line number: %d text: %s\n",line,fline);
}
line++;
}
}
SMVEC
Page No.
Output:
SMVEC
Page No.
FCFS
Code:
#include<stdio.h>
#include<conio.h>
struct process
{
int pid;
int bt,wt,tt;
}pr[10];
void main()
{
int i,j,n,prevtt;
double avgwt=0,avgtt=0;
double tottt,totwt;
clrscr();
printf("\n\t\t\t FIRST COME FIRST SERVE ");
printf("\n\n Enter the number of process :");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
pr[i].pid=i;
printf("\n Process no:%d\t Enter Burst time :",pr[i].pid);
scanf("%d",&pr[i].bt);
}
prevtt=0;
for(i=1;i<=n;i++)
{
pr[i].wt=prevtt;
pr[i].tt=pr[i].wt+pr[i].bt;
prevtt=pr[i].tt;
}
printf("\nPROCESS NO \tBURST TIME \tWAIT TIME\tTURN AROUND TIME");
printf("\n~~~~~~~~~~ \t~~~~~~~~~~ \t~~~~~~~~~\t~~~~~~~~~~~~~~~~");
SMVEC
Page No.
for(i=1;i<=n;i++)
{
printf("\n%10d\t%10d\t%9d\t%16d",pr[i].pid,pr[i].bt,pr[i].wt,pr[i].tt);
}
totwt=0;
tottt=0;
for(i=1;i<=n;i++)
{
totwt=totwt+pr[i].wt;
tottt=tottt+pr[i].tt;
}
avgwt = totwt/n;
avgtt = tottt/n;
printf("\n\nAverage Wait Time : %6.2f\nAverage Turn Around Time : %6.2f",avgwt,avgtt);
printf("\n");
for(j=0;j<pr[n].tt;j++)
{
printf("-");
}
printf("\n0");
for(i=1;i<=n;i++)
{
printf("%*d",pr[i].bt,pr[i].tt);
}
printf("\n");
for(j=0;j<pr[n].tt;j++)
{
printf("-");
}
getch();
}
SMVEC
Page No.
Output:
SMVEC
Page No.
SJF
Code:
#include<stdio.h>
#include<conio.h>
struct process
{
int pid;
int bt,wt,tt;
}pr[10];
void main()
{
int i,j,n,prevtt,t;
double avgwt=0,avgtt=0;
double tottt,totwt;
clrscr();
printf("\n\t\t\t FIRST COME FIRST SERVE ");
printf("\n\n Enter the number of process :");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
pr[i].pid=i;
printf("\n Process no:%d\t Enter Burst time :",pr[i].pid);
scanf("%d",&pr[i].bt);
}
for(i=1;i<n;i++)
{
for(j=i+1;j<=n;j++)
{
if(pr[i].bt>pr[j].bt)
{
t=pr[j].pid;
pr[j].pid=pr[i].pid;
pr[i].pid=t;
t=pr[j].bt;
pr[j].bt=pr[i].bt;
pr[i].bt=t;
}
}
}
SMVEC
Page No.
prevtt=0;
for(i=1;i<=n;i++)
{
pr[i].wt=prevtt;
pr[i].tt=pr[i].wt+pr[i].bt;
prevtt=pr[i].tt;
}
printf("\nPROCESS NO \tBURST TIME \tWAIT TIME\tTURN AROUND TIME");
printf("\n~~~~~~~~~~ \t~~~~~~~~~~ \t~~~~~~~~~\t~~~~~~~~~~~~~~~~");
for(i=1;i<=n;i++)
{
printf("\n%10d \t%10d\t%9d\t%16d",pr[i].pid,pr[i].bt,pr[i].wt,pr[i].tt);
}
totwt=0;
tottt=0;
for(i=1;i<=n;i++)
{
totwt=totwt+pr[i].wt;
tottt=tottt+pr[i].tt;
}
avgwt = totwt/n;
avgtt = tottt/n;
printf("\n\nAverage Wait Time : %6.2f\nAverage Turn Around Time : %6.2f" ,avgwt ,avgtt);
printf("\n");
for(j=0;j<pr[n].tt;j++)
{
printf("-");
}
printf("\n0");
for(i=1;i<=n;i++)
{
printf("%*d",pr[i].bt,pr[i].tt);
}
printf("\n");
for(j=0;j<pr[n].tt;j++)
{
printf("-");
}
getch();
}
SMVEC
Page No.
Output:
SMVEC
Page No.
PRIORITY SCHEDULING
Code:
#include<stdio.h>
#include<conio.h>
struct process
{
int pid;
int bt,wt,tt,pt;
}pr[10];
void main()
{
int i,j,n,prevtt,t;
double avgwt=0,avgtt=0;
double tottt,totwt;
clrscr();
printf("\n\t\t\t PRIORITY ");
printf("\n\n Enter the number of process :");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
pr[i].pid=i;
printf("\n Process no:%d\t Enter Burst time & Arrival time:",pr[i].pid);
scanf("%d %d",&pr[i].bt,&pr[i].pt);
}
for(i=1;i<n;i++)
{
for(j=i+1;j<=n;j++)
{
if(pr[i].pt>pr[j].pt)
{
t=pr[j].pid;
pr[j].pid=pr[i].pid;
pr[i].pid=t;
t=pr[j].bt;
pr[j].bt=pr[i].bt;
pr[i].bt=t;
t=pr[j].pt;
pr[j].pt=pr[i].pt;
pr[i].pt=t;
}
}
}
SMVEC
Page No.
prevtt=0;
for(i=1;i<=n;i++)
{
pr[i].wt=prevtt;
pr[i].tt=pr[i].wt+pr[i].bt;
prevtt=pr[i].tt;
}
printf("\nPROCESS NO\tBURST TIME\tPRIORITY\tWAIT TIME\tTURN AROUND
TIME");
printf("\n~~~~~~~~~~\t~~~~~~~~~~\t~~~~~~~~\t~~~~~~~~~\t~~~~~~~~~~~~~~~~");
for(i=1;i<=n;i++)
{
printf("\n%10d\t%10d\t%8d\t%9d\t%16d",pr[i].pid,pr[i].bt,pr[i].pt,pr[i].wt,pr[i].tt);
}
totwt=0;
tottt=0;
for(i=1;i<=n;i++)
{
totwt=totwt+pr[i].wt;
tottt=tottt+pr[i].tt;
}
avgwt = totwt/n;
avgtt = tottt/n;
printf("\n\nAverage Wait Time : %6.2f\nAverage Turn Around Time : %6.2f",avgwt,avgtt);
printf("\n");
for(j=0;j<pr[n].tt;j++)
{
printf("-");
}
printf("\n0");
for(i=1;i<=n;i++)
{
printf("%*d",pr[i].bt,pr[i].tt);
}
printf("\n");
for(j=0;j<pr[n].tt;j++)
{
printf("-");
}
getch();
}
SMVEC
Page No.
Output:
SMVEC
Page No.
ROUND ROBIN
Code:
#include<stdio.h>
#include<conio.h>
struct process
{
int pid;
int bt,wt,tt,pt,rem;
}pr[10];
void main()
{
int i,j,n,ts,t,max;
double avgwt=0,avgtt=0;
double tottt,totwt,totbt;
clrscr();
printf("\n\t\t\t\t ROUND ROBIN ");
printf("\n\n Enter the number of process and time slice : ");
scanf("%d %d",&n,&ts);
for(i=1;i<=n;i++)
{
pr[i].pid=i;
printf("\n Process no:%d\t Enter Burst time :",pr[i].pid);
scanf("%d",&pr[i].bt);
}
totbt=0;
for(i=1;i<=n;i++)
{
pr[i].rem = pr[i].bt;
totbt = totbt+pr[i].bt;
}
while(tottt<totbt)
{
for(i=1;i<=n;i++)
{
if(pr[i].rem==0)
{
continue;
}
else if(pr[i].rem>ts)
{
tottt=tottt+ts;
pr[i].rem=pr[i].rem-ts;
}
SMVEC
Page No.
else
{
tottt=tottt+pr[i].rem;
pr[i].rem=0;
pr[i].wt=tottt-pr[i].bt;
pr[i].tt=tottt;
}
}
};
printf("\nPROCESS NO\tBURST TIME\tWAIT TIME\tTURN AROUND TIME");
printf("\n~~~~~~~~~~\t~~~~~~~~~~\t~~~~~~~~~\t~~~~~~~~~~~~~~~~");
for(i=1;i<=n;i++)
{
printf("\n\n%10d\t%10d\t%9d\t%16d",pr[i].pid,pr[i].bt,pr[i].wt,pr[i].tt);
}
totwt=0;
for(i=1;i<=n;i++)
{
totwt=totwt+pr[i].wt;
}
avgwt = totwt/n;
avgtt = tottt/n;
printf("\n\nAverage Wait Time : %6.2f\nAverage Turn Around Time : %6.2f",avgwt,avgtt);
printf("\n");
max=0;
for(j=0;j<=n;j++)
{
if(max<pr[j].tt)
{
max=pr[j].tt;
}
}
for(j=0;j<=max;j++)
{
printf("-");
}
printf("\n0");
SMVEC
Page No.
for(i=1;i<=n;i++)
{
printf("%*d",pr[i].bt,pr[i].tt);
}
printf("\n");
for(j=0;j<=max;j++)
{
printf("-");
}
getch();
}
SMVEC
Page No.
Output:
SMVEC
Page No.
SMVEC
Page No.
s=total[0][i];
for(j=0;j<p;j++)
{
s=s-all[j][i];
}
avail[0][i]=s;
}
printf("\n\n AVAILABLE MATRIX \n\n");
for(i=0;i<r;i++)
{
printf("%d\t",avail[0][i]);
}
printf("\n\n NEED MATRIX:\n\n");
for(i=0;i<p;i++)
{
f[i]=0;
for(j=0;j<r;j++)
{
need[i][j]=max[i][j]-all[i][j];
if(need[i][j]<0)
{
need[i][j]=0;
}
printf("%d\t",need[i][j]);
}
printf("\n");
}
SMVEC
Page No.
while(ch>0)
{
ch1=ch;
for(i=0;i<p;i++)
{
if(f[i]==0)
{
c=0;
for(j=0;j<r;j++)
{
if(need[i][j]<=avail[0][j])
{
c++;
}
}
if(c==r)
{
f[i]=1;
state[k++]=i;
ch--;
for(j=0;j<r;j++)
{
avail[0][j]=avail[0][j]+all[i][j];
need[i][j]=0;
}
}
}
}
if(ch==p)
{
printf("\n ALREADY DEADLOCK OCCURRED");
exit(0);
}
if(ch1==ch)
{
printf("\n ALLOCATION LEADS TO DEADLOCK");
exit(0);
}
}
SMVEC
Page No.
SMVEC
Page No.
Output:
SMVEC
Page No.
SMVEC
Page No.
SMVEC
Page No.
// Allocate Memory
for(i=0;i<np;i++)
{
for(j=0;j<nm;j++)
{
if(p[i]<=m[j])
{
printf("\nThe Process %d allocated to Block
%d\n",i+1,j+1);
m[j] = m[j] - p[i];
p[i]=10000;
break;
}
}
}
for(i=0;i<np;i++)
{
if(p[i]!=10000)
{
printf("The Process %d is not allocated \n",i+1);
}
}
getch();
}
SMVEC
Page No.
Output:
SMVEC
Page No.
SMVEC
Page No.
if (min != 9999)
{
printf("\nThe Process %d allocated to Block
%d\n",i+1,pos+1);
m[pos] = m[pos] - p[i];
p[i]=10000;
}
}
for(i=0;i<np;i++)
{
if(p[i]!=10000)
{
printf("The Process %d is not allocated \n",i+1);
}
}
getch();
}
SMVEC
Page No.
Output:
SMVEC
Page No.
SMVEC
Page No.
if (max != -1)
{
printf("\nThe Process %d allocated to Block
%d\n",i+1,pos+1);
m[pos] = m[pos] - p[i];
p[i]=10000;
}
}
for(i=0;i<np;i++)
{
if(p[i]!=10000)
{
printf("The Process %d is not allocated \n",i+1);
}
}
getch();
}
SMVEC
Page No.
Output:
SMVEC
Page No.
FIFO Program:
Code:
#include<stdio.h>
#include<conio.h>
int nf;
int ne;
int hit,miss;
int ref[50];
int fr[15];
void getdata();
void fifo();
int large(int []);
void getdata()
{
int i;
printf("\nEnter the number of frames:");
scanf("%d",&nf);
printf("\nEnter the number of elements in the reference string:\n");
scanf("%d",&ne);
printf("\nEnter the reference string:\n");
for(i=0;i<ne;i++)
{
scanf("%d",&ref[i]);
}
}
void fifo()
{
int i,j,found=0,rear=-1;
miss=hit=0;
for(i=0;i<nf;i++)
{
fr[i]=-1;
}
for(i=0;i<ne;i++)
{
printf("\nThe no to be inserted :%d",ref[i]);
found=search(ref[i]);
if(found==0)
{
miss++;
SMVEC
Page No.
if(rear==nf-1)
{
rear=-1;
}
rear++;
fr[rear]=ref[i];
}
else
{
hit++;
}
printf("\n");
for(j=0;j<nf;j++)
printf("%d ",fr[j]);
}
printf("\n\n No. of page faults=%d\n",miss);
printf("\n No. of Hits=%d",hit);
}
int search(int item)
{
int i;
for(i=0;i<nf;i++)
{
if(fr[i]==item)
{
return(1);
}
}
return 0;
}
void main()
{
clrscr();
printf("\n\t\t\t FIFO PAGE REPLACEMENT");
getdata();
printf("\t\t\t FIFO PAGE REPLACEMENT");
fifo();
getch();
}
SMVEC
Page No.
Output:
SMVEC
Page No.
SMVEC
Page No.
LRU program:
Code:
#include<stdio.h>
int nf;
int i,ne;
int miss=0;
int ref[50];
int fr[15];
void getdata();
void lru();
void getdata()
{
printf("\nEnter the number of frames:");
scanf("%d",&nf);
printf("\nEnter the number of elements in the reference string:\n");
scanf("%d",&ne);
printf("\nEnter the reference string:\n");
for(i=0;i<ne;i++)
{
scanf("%d",&ref[i]);
}
}
void lru()
{
int check,j,k;
int count=0,index[5];
for(i=0;i<nf;i++)
{
fr[i]=-1;
}
for(i=0;i<ne;i++)
{
check=0;
printf(The number to be inserted %d,ref[i]);
SMVEC
Page No.
for(j=0;j<nf;j++)
{
if(fr[j]==ref[i])
{
check=1;
}
}
if(check==0)
{
miss+=1;
if(count<nf)
{
fr[count++] = ref[i];
}
else
{
for(j=0;j<i;j++)
{
for(k=0;k<nf;k++)
{
if(ref[j]==fr[k])
{
index[k] = j;
}
}
}
for(j=0;j<nf;j++)
{
if(index[check]>index[j])
{
check=j;
}
}
fr[check]=ref[i];
}
printf("\n");
}
SMVEC
Page No.
for(j=0;j<nf;j++)
{
printf("%d\t",fr[j]);
}
}
printf("\n Number of page faults:%d",miss);
}
void main()
{
clrscr();
printf("\n\t\t\t LRU PAGE REPLACEMENT");
getdata();
printf("\t\t\t LRU PAGE REPLACEMENT");
lru();
getch();
}
SMVEC
Page No.
Output:
SMVEC
Page No.
SMVEC
Page No.
SMVEC
Page No.
Output:
SMVEC
Page No.
C-Scan Program
Code:
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define cymax 199
int i,j,req,tmov=0,cp,np,cpo,npo;
int cyp[20],temp;
void main()
{
clrscr();
printf("\n\t\t\t C-SCAN Algorithm ");
printf("\n ENTER THE NUMBER OF REQUEST : ");
scanf("%d",&req);
printf("\n ENTER THE CURRENT HEAD POSITION : ");
scanf("%d",&cpo);
printf("\n ENTER THE ELEMENTS OF THE REQUEST : ");
cyp[0] = cpo;
for(i=1;i<=req;i++)
{
scanf("%d",&cyp[i]);
}
for(i=0;i<=req;i++)
{
for(j=0;j<req-i;j++)
{
if(cyp[j] > cyp[j+1])
{
temp = cyp[j];
cyp[j] = cyp[j+1];
cyp[j+1] = temp;
}
}
}
SMVEC
Page No.
cp=0;
do
{
if(cyp[cp] == cpo)
break;
cp++;
}while(cp!=req);
i=0,j=cp;
cpo = cyp[cp];
do
{
if(cpo == cyp[req])
{
npo = cymax; cp = -1;
}
else
{
npo = cyp[++cp];
}
printf("Moves from %d to %d with %d\n",cpo,npo,abs(cpo-npo));
tmov += (abs(cpo-npo));
cpo = npo == cymax ? 0 : npo;
}while(npo != cyp[j-1]);
printf(" Total Tracks Displaced : %d",tmov);
getch();
}
SMVEC
Page No.
Output:
SMVEC
Page No.
LOOK ALGORITHM:
Code:
#include<stdio.h>
#include<conio.h>
#include<math.h>
int ind,i,j,req,tmov=0,cp,np,cpo,npo;
int cyp[20],temp,tmp;
void main()
{
clrscr();
printf(\n\t\t\t LOOK ALGORITHM \n);
printf("\n Enter the number of requests : ");
scanf("%d",&req);
printf("\n Enter the current header position : ");
scanf("%d",&cpo);
printf("\n Enter the Requests : ",req);
cyp[0] = cpo;
for(i=1;i<=req;i++)
{
scanf("%d",&cyp[i]);
}
ind = 0;
for(i=0;i<=req;i++)
{
for(j=0;j<req-i;j++)
{
SMVEC
Page No.
SMVEC
Page No.
else
npo = cyp[++cp];
printf(" Moves from %d to %d with %d\n",cpo,npo,abs(cpo-npo));
tmov += (abs(cpo-npo));
cpo = npo;
}while(npo!=cyp[req]);
printf(" Total Tracks Displaced : %d",tmov);
getch();
}
SMVEC
Page No.
Output:
SMVEC
Page No.
C-Look Program
Code:
#include<stdio.h>
#include<conio.h>
#include<math.h>
int i,j,req,tmov=0,cp,np,cpo,npo;
int cyp[20],temp;
void main()
{
clrscr();
printf("\n\t\t\t C-LOOK ");
printf("\n Enter the number of requests : ");
scanf("%d",&req);
do
{
printf("\n Enter the current header position : ");
scanf("%d",&cpo);
}while(cpo>199 || cpo <=0);
printf("\n Enter the Requests : ",req);
cyp[0] = cpo;
for(i=1;i<=req;i++)
{
scanf("%d",&cyp[i]);
}
for(i=0;i<=req;i++)
{
for(j=0;j<req-i;j++)
{
if(cyp[j] > cyp[j+1])
{
temp = cyp[j];
cyp[j] = cyp[j+1];
cyp[j+1] = temp;
}
}
}
SMVEC
Page No.
cp=0;
do
{
if(cyp[cp] == cpo)
break;
cp++;
}while(cp!=req);
i=0,j=cp;
cpo = cyp[cp];
do
{
if(cp == req)
{
npo = cyp[0];
cp = 0;
}
else
{
npo = cyp[++cp];
}
printf("Moves from %d to %d with %d\n",cpo,npo,abs(cpo-npo));
tmov += (abs(cpo-npo));
cpo = npo == cyp[req] ? cyp[0] : npo ;
}while(npo != cyp[j-1]);
printf(" Total Movement : %d",tmov);
getch();
}
SMVEC
Page No.
Output:
SMVEC
Page No.
SMVEC
Page No.
do
{
printf("\n\t\t MENU\n\n");
printf("\n\t1.START EATING\n\n");
printf("\n\t2.STOP EATING\n\n");
printf("\n\t3.EXIT\n\n");
printf("ENTER YOUR CHOICE:");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\n ENTER THE PHILOSOPHER WHO IS GOING TO EAT:");
scanf("%d",&a);
f=take_sticks(a);
if(f==EATING)
{
printf("\n THE PHILOSOPHER HAS STARTED
EATING...\n\n");
}
else
{
printf("\nTHE PHILOSOPHER IS HUNGRY!!..\n\n");
printf("\n THE CHOPSTICK IS ENGAGED\n\n");
}
for(i=0;i<N;i++)
{
if(state[i]==THINKING)
{
printf("\nP%d IS THINKING",i);
}
SMVEC
Page No.
else if(state[i]==HUNGRY)
{
printf("\nP%d IS HUNGRY",i);
}
else
{
printf("\nP%d IS EATING",i);
}
}
break;
case 2:
printf("\n ENTER THE PHILOSOPHER NUMBER TO RELEASE THE CHOPSTICK");
scanf("%d",&b);
if(state[b]==EATING)
{
f=put_sticks(b);
printf("\n THE PHILOSOPHER HAS STOPPED EATING!!..\n\n");
}
else
{
printf("\n THE PHILOSOPHER IS NOT EATING!!\n\n");
}
for(i=0;i<N;i++)
{
if(state[i]==THINKING)
{
printf("\nP%d IS THINKING",i);
}
SMVEC
Page No.
else if(state[i]==HUNGRY)
{
printf("\nP%d IS HUNGRY",i);
}
else
{
printf("\nP%d IS EATING",i);
}
}
break;
case 3:
exit(0);
break;
}
}
while(ch!=3);
getch();
}
int take_sticks(int j)
{
state[j]=HUNGRY;
test(j);
return state[j];
}
SMVEC
Page No.
int put_sticks(int j)
{
state[j]=THINKING;
if(j==0) test(N-1); else test(LEFT);
if(j==N-1) test(0); else test(RIGHT);
return state[j];
}
void test(int j)
{
if(j==0) rt = N-1; else rt = LEFT;
if(j==N-1) lt = 0; else lt = RIGHT;
if(state[j]==HUNGRY && state[lt]!=EATING && state[rt]!=EATING)
{
state[j]=EATING;
}
}
SMVEC
Page No.
Output:
SMVEC
Page No.
SMVEC
Page No.
SMVEC
Page No.
SMVEC
Page No.
SMVEC
Page No.