KEMBAR78
Os Lab Assignment | PDF | Computer File | Filename
0% found this document useful (0 votes)
33 views47 pages

Os Lab Assignment

The document provides a comprehensive overview of basic Linux commands, including their syntax and descriptions, as well as various shell scripting examples for common programming tasks such as calculating factorials, checking for prime numbers, and generating Fibonacci series. It includes source code snippets and expected outputs for each example, demonstrating practical applications of the commands and scripts. Additionally, it covers topics like file permissions, simple interest calculations, and sorting algorithms.

Uploaded by

sayanmetya222
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)
33 views47 pages

Os Lab Assignment

The document provides a comprehensive overview of basic Linux commands, including their syntax and descriptions, as well as various shell scripting examples for common programming tasks such as calculating factorials, checking for prime numbers, and generating Fibonacci series. It includes source code snippets and expected outputs for each example, demonstrating practical applications of the commands and scripts. Additionally, it covers topics like file permissions, simple interest calculations, and sorting algorithms.

Uploaded by

sayanmetya222
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/ 47

BASIC LINUX COMMANDS

COMMAND EXAMPLE DESCRIPTION


DATE $ date Displays the current
Date.
MAN $ man Displays the manual
Page of our terminal.
Ls $ ls List the number of files and
directories.
Ls-al $ ls-al List all the files in the
current working directory
showing permissions,
ownership, size, time and
date.

Cal $ Cal Displays the current


Month calendar.
Cal 2008 $ Cal 2008 Displays the calendar of the
year 2008.
Cal 5 2008 $ Cal 5 2008 Displays the calendar for
fifth month of 2008.
Who $ Who Displays the number of
users logged on particular
system.
Who am I $ Who am I Current login user name
will be displayed.
More $ More It allows file contents or
piped output to be sent to
the screen page by page.
file $ file Displays the type of file.
rm $ rm Remove the file or
directory.
mk dir $ mkdir hai Create a directory.
Cat filename $ cat prog 1 To open a file.
Echo $ Echo SMEC Display the string.

clear $ clear Clear the screen.


cd Change directory.
$ cd/Home Change the current
directory to /Home.
$ cd httpd Change the current working
directory to httpd.
$ cd.. Move to parent directory of
the current directory.
$ cd~ Move to user’s home
directory which is
“/home/username”.
cp Copy files.
$ cp myfile yourfile Copy the files “myfile” to
the file “yourfile” in the
current working directory.
$ cp-i/data/myfile Copy the file “/data/myfile”
to the current working
directory.
$ cp – dpr Copy all files from the
srcdir dest dir directory “srcdir” to
“destdir”.
cmp $ cmpf1f2 Compare two files.
Mv Move or rename file.
$mv-i myfile Move the file from
yourfile “myfile” to “yourfile”.
$ mv-i/data/myfile Move the file from myfile
from the directory “/data”
to the current working
directory.
Pwd $ pwd Show the name of the
current working directory.
sort $ sort filename Sort the files content in
ascending order.
$ sort-r filename Sort the file content in
descending order.
Grep $ grep-pattern filename Searches the regular
expressions in string or
files.
Head $ head-op filename Access the lines from the
top.

Tail $ tail-op filename Access the lines to bottom.


uniq $ uniq filename I fetches one copy of
redundant record.
Shutdown Shuts the system down.
$ shutdown-h now Shuts the system down to
halt immediately.
$ shut down-r now Shuts the system down
immediately and the system
reboots.
SIMPLE FUNCTION USING SHELL
SOURCE CODE:
program()
{
echo “ 1 is $1 ”
echo “ 2 is $2 ”
a= “ goodbye ”
}
a=hello
b=world
program $a $b
echo “ a is $a ”
echo “ b is $b ”

OUTPUT:
[examuser35@localhost Jebastin]$ sh function.sh
1 is hello
2 is world
a is goodbye
b is world
LEAP YEAR
SOURCE CODE:
echo “Enter the year”
read year
chk=`expr $year % 4`
if [ $chk –eq 0 ]
then
echo “ $Given year is a leap year ”
else
echo “ $Given year is not a leap year ”
fi

OUTPUT:
[examuser35@localhost Jebastin]$ sh leap.sh
Enter the year
1995
year is not leap year

[examuser35@localhost Jebastin]$ sh leap.sh


Enter the year
1998
Given year is a leap year
ARMSTRONG NUMBER
SOURCE CODE:
echo "Enter the number"
read n
t=$n
s=0
b=0
c=10
while [ $n -gt $b ]
do
r=`expr $n % $c`
i=`expr $r \* $r \* $r`
s=`expr $s + $i`
n=`expr $n / $c`
done
echo $s
if [ $s -eq $t ]
then
echo "Amstrong number"
else
echo "Not an Armstrong number"
fi

OUTPUT:
[examuser35@localhost Jebastin]$ sh amstrong.sh
Enter the number
213
36
Not an Armstrong number
[examuser35@localhost Jebastin]$ sh amstrong.sh
Enter the number
153
153
Armstrong number
FACTORIAL OF GIVEN NUMBER
SOURCE CODE:
echo " Enter the number for which factorial is to be found "
read n
f=1
a=0
i=1
if [ $n -eq $a ]
then
echo $f
else
while [ $i -le $n ]
do
f=`expr $f \* $i`
i=`expr $i + 1`
done
fi
echo " The factorial of the number is "
echo $f

OUTPUT:
[examuser35@localhost Jebastin]$sh fact.sh
Enter the number for which factorial is to be found
5
The factorial of the number is
120
SUM OF FIVE DIGIT NUMBER
SOURCE CODE:
echo "Enter the number"
read num
sum=0
while test $num -gt 0
do
digit=`expr $num % 10`
num=`expr $num / 10`
sum=`expr $sum + $digit`
done
echo "sum of the digits= $sum"

OUTPUT:
[examuser35@localhost Jebastin]$ sh five.sh
Enter the number
12345
sum of the digits= 15
GREATEST AMONG THREE NUMBERS
SOURCE CODE:
echo " Enter A value: "
read a
echo " Enter B value: "
read b
echo " Enter C value: "
read c
if [ $a -gt $b ] && [ $a -gt $c ]
then
echo "A value is greater"
elif [ $b -gt $a ] && [ $b -gt $c ]
then
echo "B value is greatest "
else
echo "C value is greatest "
fi

OUTPUT:
[examuser35@localhost Jebastin]$ sh greatest.sh
Enter A value:
5
Enter B value:
6
Enter C value:
2
B value is greatest
PALINDROME
SOURCE CODE:

echo "Enter the string"


read str
len=`expr length $str`
flag=1
i=1
j=$len
while ["$i" -le `expr $len/2`]
do
c1=`expr substr $i 1`
c2=`expr substr $j 1`
if ["$c1"!="$c2"]
then
flag=0
fi
i=`expr $i + 1`
j=`expr $j + 1`
done
if ["$flag" -ed 1]
then
echo "Given string is a palindrome"
else
echo "Given string is not a palindrome"
fi

OUTPUT:

[examuser35@localhost Jebastin]$ sh pal.sh


Enter the string
mam
Given string is a palindrome

[examuser35@localhost Jebastin]$ sh pal.sh


Enter the string
dam
Given string is not a palindrome
PRIME NUMBER
SOURCE CODE:
echo "Enter the number"
read a
i=2
z=0
while [ $i -lt $a ]
do
s=`expr $a % $i`
if [ $s -eq $z ]
then
echo "Not Prime"
exit
else
i=`expr $i + 1`
fi
done
echo "Prime number"

OUTPUT:
[examuser35@localhost Jebastin]$ sh prime.sh
Enter the number
7
Prime number
[examuser35@localhost Jebastin]$ sh prime.sh
Enter the number
4
Not Prime
FIBONACCI SERIES
SOURCE CODE:
echo "Enter the number"
read n
i=1
a=-1
fib=0
b=1
echo "Fibonacci series is"
while [ $i -lt $n ]
do
fib=`expr $a + $b`
a=$b
b=$fib
echo $fib
i=`expr $i + 1`
done

OUTPUT:
[examuser35@localhost Jebastin]$ sh fib.sh
Enter the number
8
Fibonacci series is
0
1
1
2
3
5
8
ARMSTRONG NUMBER
SOURCE CODE:
echo "Enter the number"
read n
t=$n
s=0
b=0
c=10
while [ $n -gt $b ]
do
r=`expr $n % $c`
i=`expr $r \* $r \* $r`
s=`expr $s + $i`
n=`expr $n / $c`
done
echo $s
if [ $s -eq $t ]
then
echo "Amstrong number"
else
echo "Not an Armstrong number"
fi

OUTPUT:
[examuser35@localhost Jebastin]$ sh amstrong.sh
Enter the number
213
36
Not an Armstrong number
[examuser35@localhost Jebastin]$ sh amstrong.sh
Enter the number
153
153
Armstrong number
ARITHMETIC OPERATION
SOURCE CODE:
echo "Enter the values"
read i
read j
echo "Enter 1 for addition"
echo "Enter 2 for subtraction"
echo "Enter 3 for multiplication"
echo "Enter 4 for division"
echo "enter choice"
read a
x=1
y=2
z=3
m=4
if [ $a -eq $x ]
then
l=`expr $i + $j`
echo “ The addition is ”
echo $l
elif [ $a -eq $y ]
then
k=`expr $i - $j`
echo “ The subtraction is ”
echo $k
elif [ $a -eq $z ]
then
p=`expr $i \* $j`
echo “ The multipliction is ”
echo $p
elif [ $a -eq $m ]
then
s=`expr $i / $j`
echo “ The division is ”
echo $s
fi

OUTPUT:
[examuser35@localhost Jebastin]$ sh arithmetic.sh
Enter the values
3
5
Enter 1 for addition
Enter 2 for subtraction
Enter 3 for multiplication
Enter 4 for division
Enter choice
3
The multiplication is
15
MULTIPLICATION TABLE
SOURCE CODE:
echo “ Enter the number ”
read i
echo “ $i table ”
j=1
while [ $i –le 12 ]
do
l=`expr $j \* $j = $l`
echo "$j * $i = $l"
j=`expr $j + 1`
done

OUTPUT:
[examuser35@localhost Jebastin]$ sh multable.sh
Enter the number
2
2 table
1*2=2
2*2=4
3*2=6
4*2=8
5 * 2 = 10
6 * 2 = 12
7 * 2 = 14
8 * 2 = 16
9 * 2 = 18
10 * 2 = 20
11 * 2 = 22
12 * 2 = 24
EMPLOYEE PAYROLL
SOURCE CODE:
get()
{
echo “ Enter code, allowance, deduction, basic pay ”
read code
read all
read ded
read basic
}
display()
{
echo “ The code is $code ”
echo “ The allowance is $all ”
echo “ The deduction is $ded ”
echo “ The basic pay is $basic ”
}
get $code $all $ded $basic
gross=`expr $bacis + $all`
net=`expr $gross - $ded`
echo “ The gross is $gross ”
echo “ The net is $net ”

OUTPUT:
[examuser35@localhost Jebastin]$sh emp.sh
Enter code, allowance, deduction, basic pay
100
200
300
400
The gross is 600
The net is 300
FILE PERMISSION
SOURCE CODE:
read i
if [ -x $i ]
then
echo “ i has execute permission ”
else
echo “ i does not have execute permission ”
fi
if [ -r $i ]
then
echo “ i has readable permission ”
else
echo “ i does not have readable permission ”
fi
if [ -w $i ]
then
echo “ i has writeable permission ”
else
echo “ i does not have writeable permission ”
fi

OUTPUT:
[examuser35@localhost Jebastin]$sh permission.sh
a
i has execute permission
i has readable permission
i has writeable permission
DISPLAY FILE TYPE
SOURCE CODE:
read i
if [ -f $i ]
then
echo “ It is an ordinary file ”
elif [ -d $i ]
then
echo “ It is a directory file ”
else
echo “ It does not exist ”
fi

OUTPUT:
[examuser35@localhost Jebastin]$ sh type.sh
a
It is a directory file
[examuser35@localhost Jebastin]$ sh big.sh
It is an ordinary file
[examuser35@localhost Jebastin]$ sh i.sh
It does not exist
SIMPLE INTEREST
SOURCE CODE:
echo " Enter the principle value: "
read p
echo " Enter the rate of interest:"
read r
echo " Enter the time period:"
read t
s=`expr $p \* $t \* $r / 100`
echo " The simple interest is "
echo $s

OUTPUT:
[examuser35@localhost Jebastin]$ sh si.sh
Enter the principle value:
2000
Enter the rate of interest:
4
Enter the time period:
10
The simple interest is
800
BUBBLE SORT
SOURCE CODE:
echo “Enter the size of array”
read no
i=0
echo “Enter the numbers”
for i in `seq 0 $[$no – 1]`
do
read arr[i]
done
for i in `seq 0 $[$no – 1]`
do
j=0
while [ $j –lt $[$no - $i – 1]]
do
if [ $[arr[j]] –gt $[arr[$j + 1]]]
then
tmp=$[arr[$j]]
arr[$j]=$[arr[$j + 1]]
arr[$j + 1]=$tmp
fi
j=`expr $j + 1`
done
done
echo “The ordered array is……”
for i in `seq 0 $[$no – 1]`
do
echo “$[arr[$i]]”
done

OUTPUT:
[examuser35@localhost Jebastin]$ sh bubble.sh
Enter the size of array
5
Enter the numbers
100
50
87
14
25
The ordered array is……
14
25
50
87
100
DISPLAY THE PATTERNS
SOURCE CODE:
for a in 1 2 3 4
do
j=1
while [ $j -le $a ]
do
echo -n "*"
j=` expr $j + 1`
done
echo " "
done

OUTPUT:
[examuser35@localhost Jebastin]$ sh display.sh
*
**
***
****
GENERATE THE COMBINATIONS OF
1,2,3
SOURCE CODE:
clear
for i in 1 2 3
do
for j in 1 2 3
do
for k in 1 2 3
do
echo “$i $j $k”
done
done
done

OUTPUT:
[examuser35@localhost Jebastin]$ sh series.sh
111
112
113
121
122
123
131
132
133
211
212
213
221
222
223
231
232
233
311
312
313
321
322
323
331
332
333
AREA AND PERIMETER
SOURCE CODE:
echo "Enter the length of the rectangle"
echo "Enter the length of the rectangle"
read l
echo "Enter the breadth of the rectangle"
read b
echo "Enter the side of the square"
read a
areaR=`expr $l \* $b`
periR=`expr $l \* 2 + $b \* 2`
areaS=`expr $a \* $a`
periS=`expr 4 \* $a`
echo "Area of the rectangle = $areaR"
echo "Perimeter of the rectangle = $periR"
echo "Area of the square = $areaS"
echo "Perimeter of the square =$periS"

OUTPUT:
[examuser35@localhost Jebastin]$ sh area.sh
Enter the length of the rectangle
5
Enter the breadth of the rectangle
6
Enter the side of the square
8
Area of the rectangle = 30
Perimeter of the rectangle = 22
Area of the square = 64
Perimeter of the square =32
SUM OF FIVE DIGIT NUMBER
SOURCE CODE:
echo "Enter the number"
read num
sum=0
while test $num -gt 0
do
digit=`expr $num % 10`
num=`expr $num / 10`
sum=`expr $sum + $digit`
done
echo "sum of the digits= $sum"

OUTPUT:
[examuser35@localhost Jebastin]$ sh five.sh
Enter the number
12345
sum of the digits= 15
PALINDROME
SOURCE CODE:

echo "Enter the string"


read str
len=`expr length $str`
flag=1
i=1
j=$len
while ["$i" -le `expr $len/2`]
do
c1=`expr substr $i 1`
c2=`expr substr $j 1`
if ["$c1"!="$c2"]
then
flag=0
fi
i=`expr $i + 1`
j=`expr $j + 1`
done
if ["$flag" -ed 1]
then
echo "Given string is a palindrome"
else
echo "Given string is not a palindrome"
fi

OUTPUT:

[examuser35@localhost Jebastin]$ sh pal.sh


Enter the string
mam
Given string is a palindrome

[examuser35@localhost Jebastin]$ sh pal.sh


Enter the string
dam
Given string is not a palindrome
SWAPPING
SOURCE CODE:
echo “ First value ”
read a
echo “ Second value ”
read b
temp=$a
a=$b
b=$temp
echo “ The swapped value: ”
echo “ First value = $a ”
echo “ Second value = $b ”

OUTPUT:
[examuser35@localhost Jebastin]$ sh swapping.sh
First value
23
Second value
10
The swapped value is:
First value = 10
Second value = 23
PROCESS CREATION USING
FORK,EXEC COMMANDS
SOURCE CODE:
#include<stdio.h>
int main()
{
int p;
printf(“ Origin Program, pid = %d\n ” , getpid());
p=fork();
if(p==0)
{
printf(“ In child process pid = %d , ppid= %d “, getpid(),getppid());
}
else
{
printf(“ In parent pid = %d, fork returned = %d \n”, getpid(),p);
}
}

OUTPUT:
[examuser35@localhost Jebastin]$ cc pro.c
[examuser35@localhost Jebastin]$ ./a.out
Origin Program, pid = 22386
In child process pid = 22837 , ppid= 22386
In parent pid = 22386 , fork returned = 22387
PROCESS CREATION USING
GETPID,SLEEP,EXEC COMMANDS
SOURCE CODE:
#include<stdio.h>
#include<unistd.h>
main()
{
int pid;
pid=fork();
if(pid<0)
{
printf(stderr, “fork failed”);
exit(-1)
}
else if(pid==0)
{
printf(“child id is%d”,getpid());
execlp(“/bin/ls”,”is”,NULL);
}
else
{
wait(NULL);
printf(“The parent id is %d\n”,getpid());
printf(“The child id is %d\n”,getpid());
printf(“child completed\n”);
exit(0);
}
}

OUTPUT:
[examuser35@localhost Jebastin]$ cc prob.c
[examuser35@localhost Jebastin]$ ./a.out
amrarpl.c ff.c naan.c
arpraj.c ff.c pca.c
arp.c ftp.c pcb.c
bata.c heema.c pcc.c
bgp.c hema.c run.c
bgpro.c heha.c rr.c
cals.c input.c sr.c
chat.c ktec.c ss.c
client.c ktes.c sss.c
data1.c ls.c tam.c
day.c maa.c tcp.c
echo.c man.c udhai.c
eho.c new1.c ven.c
exam.c nehru1.c wa.c
The parent id is 3286
The child is 6526
Child completed.
PROCESS CREATION USING WAIT
COMMAND
SOURCE CODE:
#include<stdio.h>
main()
{
int pid;
pid=fork();
if(!pid)
{
printf(“ \n Child process\n ”);
printf(“ getpid(): %d\n” , getpid() );
printf(“ getppid(): %d\n” , getppid() );
}
else
{
printf(“ \n Parent process\n” );
printf(“ getpid(): %d\n” , getpid() );
printf(“ getppid(): %d\n” , getppid() );
sleep(10);
printf(“\n Parent woke up”);
}
}

OUTPUT:
[examuser35@localhost Jebastin]$ cc pc.c
[examuser35@localhost Jebastin]$ ./a.out
Child Process
getpid(): 5640
getppid(): 5639
Parent process
getpid(): 5639
getppid(): 5639
Parent woke up
FILE MANIPULATION-
OPEN,READ,WRITE
SOURCE CODE:
#include<stdio.h>
#include<unistd.h>
int main()
{
int fd_in,fd_out,n bytes;
char buf[512];
fd_in=open(“output 1”,0);
fd__creat(“output 2”,0644);
n bytes=read(fd_in,buf,512);
while (n bytes>0)
{
write(fd_out,buf,n bytes);
n bytes=read(fd_in,buf,512);
}
close(fd_in);
return 0;
}

OUTPUT:
[examuser35@localhost Jebastin]$ cat>output1
a
b
c
[examuser35@localhost Jebastin]$ cat>output2
[examuser35@localhost Jebastin]$ cat>cc fileman.cc
[examuser35@localhost Jebastin]$ ./a.out
[examuser35@localhost Jebastin]$ cat>output2
a
b
c
DIRECTORY MANIPULATION- READ
DIR,OPEN DIR
SOURCE CODE:
#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<sys/dir.h>
#define MAX CHARS 200
int char read, tmp;
struct direct *DirEntryPtr;
DIR *dirptr;
Struct stat Stat;
char path [MAX_CHARS];
main(argc,argc)
int argc;
char **argv
{
dirptr=opendir(argv[1]);
while(1)
DirEntryPtr=readdir(dirptr);
if(DirEntryPtr==0)
break;
if(strcmp(DirEntryPtr->d_name,”.”)!=0&&
strcmp(DirEntryPtr->d_name,”..”)
{
printf(“%s”, DirEntryPtr->d_name);
tmp=stat(path,&Stat)
if(S_ISDIR (Stat.st mode))
printf(“*”);
printf(“\n”);
}
}
}

OUTPUT:
[examuser35@localhost Jebastin]$ cc dm.c
[examuser35@localhost Jebastin]$ ./a.out
.a.sh.swp b.c r1
sf.sh fm.c dm.c
e.sh c.c r2
sw.sh output 2 .dm.c.swp
fp.sh c .dm.c.swpo
df.sh output1
a.out r1
a.c dm.c
b.c.swp r2
c.sh .dm.c.swp
LS COMMAND
SOURCE CODE:
echo “ TO DISPLAY LIST OF FILE ”
echo “ 1) ls ”
echo “ 2) ls-al ”
read choice
case $choice in
1)la;;
2)la-al;;
*)echo “ invalid ”;;
esac

OUTPUT:
[examuser35@localhost Jebastin]$ sh list.sh
TO DISPLAY LIST OF FILE
1) ls
2) ls-al
1
list.sh palin.sh fib.sh fact.sh rr.c
greatest.sh fcfs.c sjf.mul.sh armstr.ch
GREP COMMAND
SOURCE CODE:
[examuser35@localhost Jebastin]$ cat>god
god is great
god is present everywhere
[2]+ Stopped cat>god
echo “displaying a string from a file”
echo “get file name”
read s
echo “get string”
read i
grep $i $s

OUTPUT:
[examuser35@localhost Jebastin]$ grep.sh
displaying a string from a file
get file name
god
get string
great
god is great
UNIX C Program for First Come First Serve
Scheduling Algorithm
AIM:
To write a LINUX/UNIX C Program for the Implementation of First Come First Serve
Scheduling Algorithm.

SOURCE CODE:
#include<stdio.h>
main()
{
int n,a[10],b[10],t[10],w[10],g[10],i,m;
float att=0,awt=0;
for(i=0;i<0;i++)
{
a[i]=0;b[i]=0;w[i]=0;g[i]=0;
}
printf(“Enter the number of processes:”);
scanf(“%d”,&n);
printf(“Enter the burst time:”);
for(i=0;i<n;i++)
scanf(“%d”,&b[i]);
printf(“Enter the arrival time:”);
for(i=0;i<n;i++)
scanf(“%d”,&a[i]);
g[0]=0;
for(i=0;i<10;i++)
{
w[i]=g[i]-a[i];
t[i]=g[i+1]-a[i];
awt=awt+w[i]
att=att+t[i];
}
awt=awt/n;
att=att/n;
printf(“\n\t Process\t Waiting time \t Turnaround time\n”);
for(i=0;i<n;i++)
{
printf(“\t %d\t\t %d\t\\t %d\n”I,w[i],t[i]);
}
printf(“\n The average waiting time is %t \n”,awt);
printf(“The average turnaround time is %f\n”,att);
OUTPUT:
[examuser35@localhost Jebastin]$ cc fcfs.c
[examuser35@localhost Jebastin]$ ./a.out
Enter the number of processes:3
Enter the burst time:1
2
3
Enter the arrival time:0
1
2

Process Waiting time Turnaround time


1
1 3 2
2 6 4

The average waiting time is 3.3333


The average turnaround time is 2.333
UNIX C Program for Shortest Job First
Scheduling Algorithm
AIM:
To write a LINUX/UNIX C Program for the Implementation of Shortest Job First Scheduling
Algorithm.

SOURCE CODE:
#include<stdio.h>
main()
{
int n,j,temp,temp1,temp2,pr[10],b[10],t[10],w[10],p[10],i;
float att=0;awt=0;
for(i=0;i<10;i++)
{
b[i]=0;w[i]=0;
}
printf(“Enter the number of process:”);
scanf(“%d”,&n);
printf(“\n Enter the burst time”);
for(i=0;i<n;i++)
{
scanf(“%d”,&b[i]);
p[i]=i;
}
for(i=0;i<n;i++)
{
for(j=1;j<n;j++)
{
if(b[i]>b[j]
{
temp=b[i];
temp1=p[i];
b[i]=b[j];
p[i]=p[j];
b[j]=temp;
p[j]=temp1;
}
}
w[0]=0;
for(i=0;i<n;i++)
w[i+1]=w[i]+b[i];
for(i=0;i<n;i++)
{
t[i]=w[i]+b[i];
awt=aw+w[i]:
att=att+t[i];
}
awt=awt/n;
att=att/n;
printf(“\n\t Process \tWaitimgtime \t Turnaroundtime\n);
for(i=0;i<n;i++)
printf(“\t p[%d] \t %d\t\t %d\n”,p[i].w[i],t[i]);
printf(“\n The average waiting time is %t \n”,awt);
printf(“The average turnaround time is %f\n”,att);
return 1;
}

OUTPUT:
[examuser35@localhost Jebastin]$ cc sjf.c
[examuser35@localhost Jebastin]$ ./a.out
Enter the number of process:5
Enter the burst time:5
4
3
2
1

Process Waiting time Turnaround time


P[4] 0 1
P[0] 1 6
P[1] 6 10
P[2] 10 13
P[3] 13 15

The average waiting time is 6.000000


The average turnaround time is 9.000000
UNIX C Program for Priority Scheduling
Algorithm
AIM:
To write a LINUX/UNIX C Program for the Implementation of Priority Scheduling Algorithm .

SOURCE CODE:
#include<stdio.h>
#include>math.h>
#define MAX20
main()
{
int arrival[MAX];
int burst[MAX];
int prior[MAX];
int n,i,t1=0,t2=0,j,temp,temp1,temp2,tot=0;
float avg=0.0,wait=0.0;
printf(“Enter the number of jobs: ”);
scanf(“%d”,&n);
for(i=0;i<=n;i++)
{
printf(“Enter the arrival time: ”,i+1);
scanf(“%d”,&arrival[i]);
printf(“Enter the burst time: ”,i+1);
scanf(“%d”,&arrival[i]);
printf(“Enter the priority time: ”,i+1);
scanf(“%d”,&prior[i]);
}
printf(“Priority \t Arrival \t Burst time \t Turnaround time”);
for(i=1;i<n;i++)
{
for(j=1;j<=n;j++)
{
if(prior[i]>prior[j])
{
temp2=prior[i];
prior[i]=prior[j];
prior[j]=temp2;
temp=arrival[j];
arrival[i]=arrival[j];
arrival[j]=temp;
temp1=burst[i];
burst[i]=burst[j];
burst[j]=temp1;
}
}
tot=burst[i]+tot;
printf(“\n %d \t %d \t %d \t %d”,arrival[i],prior[i],burst[i],tot);
}
for(i=1;i<=n;i++)
{
t1=t2;
t2=t1+burst[i];
printf(“\n Process %d \t Waitingtime is: %d \n”,i,t1);
wait=wait+t1;
}
avg=wait/n;
printf(“The average waiting time is %f \n”,avg);
}

OUTPUT:
[examuser35@localhost Jebastin]$ cc priority.c
[examuser35@localhost Jebastin]$ ./a.out
Enter the number of jobs: 3
Enter the arrival time: 1
Enter the burst time: 3
Enter the priority time: 2
Enter the arrival time: 3
Enter the burst time: 1
Enter the priority time: 2
Enter the arrival time: 5
Enter the burst time: 3
Enter the priority time: 1

Priority Arrival Burst time Turnaroundtime


5 1 3 3
5 1 3 6
5 1 3 9

process 1 waiting time is:0

process 2 waiting time is:0

process 3 waiting time is:1

The average waiting time is 0.333333


UNIX C Program for Round Robin
Scheduling Algorithm
AIM:
To write a LINUX/UNIX C Program for the Implementation of Round Robin Scheduling
Algorithm
SOURCE CODE:
#include<stdio.h>
int ttime,i,j,temp;
main()
{
int pname[10],btime[0],pname2[10],btime2[10];
int n,x,z;
printf("enter the no of process");
scanf("%d",&n);
printf("Enter the process name & burst time for the process\n");
for(i=0;i<n;i++)
{
printf("Enter the process name");
scanf("%d",&pname2[i]);
printf("Enter the burst time for the process %d");
scanf("%d",&btime2[i]);
}
printf("PROCESS NAME\t\tBURST TIME");
for(i=0;i<n;i++)
printf("%d\t\t\t%d\n",pname2[i],btime[i]);
z=1;
while(z==1)
{
ttime=0;
for(i=0;i<n;i++)
{
pname[i]=pname2[i];
btime[i]=btime2[i];
}
printf("\npress 1.roundrobin \n2.exit");
scanf("%d",&x);
switch(x)
{
case 1:
rrobin(pname,btime,n);
break;
case 2:
exit(0);
break;
}
printf("\n\n if you want to continue press1:");
scanf("%d",&z);
}
}
rrobin(int pname[],int btime[],int n)
{
int tslice;
j=0;
printf("\n\t round robin scheduling");
printf("enter the time slice");
scanf("%d",&tslice);
printf("process name\t remaining time \ttotal time");
while(j<n)
{
for(i=0;i<n;i++)
{
if(btime[i]>0)
{
if(btime[i]>=tslice)
{
ttime+=tslice;
btime[i]=btime[i]-tslice;
printf(“\n %d\t\t%d\t\t%d”,pname[i],btime[i],ttime);
if(btime[i]==0)
j++;
}
else
{
ttime+=btime[i];
btime[i]=0;
printf(“\n %d\t\t%d\t\t%d”,pname[i],btime[i],ttime);
}
}
}
}
}
OUTPUT:
[examuser35@localhost Jebastin]$ cc rr.c
[examuser35@localhost Jebastin]$ ./a.out
Enter the number of process: 3
Enter the process name and burst time for the process
Enter the process name 1
Enter the burst time for the process: 1
10
Enter the process name 2
Enter the burst time for the process: 2
10
Enter the process name 3
Enter the burst time for the process: 3
10

Press 1.round robin


2.exit
1

Process name Remaining time Total


1 5 5
2 5 10
3 5 15
1 0 20
2 0 25
3 0 30

If you want to continue press 1


0
UNIX C Program for Semaphore
AIM:
To write a LINUX/UNIX C Program for the Implementation of Semaphore
SOURCE CODE:
#include<stdio.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/sem.h>
main()
{
int semid,pid;
struct sembuf sop;
semid=semget(0*20,1,IPC_CREAT|0666);
semctl(semid,0,SETVAL,1);
pid=fork();
if(pid!=0)
{
printf("\n child before semaphore\n");
sop.sem_num=0;
sop.sem_op=-1;
sop.sem_flg=0;
semop(semid,&sop,1);
printf("\n child in critical section\n");
sleep(2);
printf("\n child in critical section \n");
sop.sem_num=0;
sop.sem_op=1;
sop.sem_flg=0;
semop(semid,&sop,1);
}
else
{
printf("\n parent before semaphore \n");
sop.sem_num=0;
sop.sem_op=-1;
sop.sem_flg=0;
semop(semid,&sop,1);
printf("\n parent in critical section");
sleep(2);
printf("\n parent coming out of critical section");
sop.sem_num=0;
sop.sem_op=1;
sop.sem_flg=0;
semop(semid,&sop,1);
}
}

OUTPUT:
[examuser35@localhost Jebastin]$ cc semaphore.c
[examuser35@localhost Jebastin]$ ./a.out
parent before semaphore
child before semaphore
parent in critical section
parent coming out of critical section
child in critical section
child in critical section

You might also like