OS Record

Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

Program to implement file allocation strategies.

a) Sequential b) Linked c) Indexed


a) SEQUENTIAL FILE ALLOCATION
#include<stdio.h>
struct fileTable
{
char name[20]; int
sb, nob;

}ft[30];
int main()
{
int i, j, n; char s[20];
printf("Enter no of files :");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter file name %d:",i+1);
scanf("%s",ft[i].name);
printf("Enter starting block of file %d :",i+1);
scanf("%d",&ft[i].sb);
printf("Enter no of blocks in file %d :",i+1);
scanf("%d",&ft[i].nob);
}
printf("\nEnter the file name to be searched - ");
scanf("%s",s);
for(i=0;i<n;i++)
if(strcmp(s, ft[i].name)==0)
break;
if(i==n)
printf("\nFile Not Found");

else
{
printf("\nFILE NAME START BLOCK NO OF BLOCKS BLOCKS
OCCUPIED\n"); printf("\n%s\t\t%d\t\t%d\t",ft[i].name,ft[i].sb,ft[i].nob);
for(j=0;j<ft[i].nob;j++)
printf("%d, ",ft[i].sb+j);
}
}

INPUT:
Enter no of files :3
Enter file name 1 :A
Enter starting block of file 1 :85
Enter no of blocks in file 1 :6
Enter file name 2 :B
Enter starting block of file 2 :102
Enter no of blocks in file 2 :4
Enter file name 3 :C
Enter starting block of file 3 :60
Enter no of blocks in file 3 :4
Enter the file name to be searched -- B
OUTPUT:

FILE NAME START BLOCK NO OF BLOCKS BLOCKS OCCUPIED


B 102 4 102, 103, 104, 105
B) LINKED FILE ALLOCATION

#include<stdio.h>
struct file
{
char fname[10];
int start,size,block[10];
}f[10];
int main()
{
int i,j,n;
printf("Enter no. of files:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter file name:");
scanf("%s",&f[i].fname);
printf("Enter starting block:");
scanf("%d",&f[i].start);
f[i].block[0]=f[i].start;
printf("Enter no.of blocks:");
scanf("%d",&f[i].size);
printf("Enter block numbers:");
for(j=1;j<=f[i].size;j++)
{
scanf("%d",&f[i].block[j])

printf("File\tstart\tsize\tblock\n");

for(i=0;i<n;i++)
{

printf("%s\t%d\t%d\t",f[i].fname,f[i].start,f[i].size);

for(j=1;j<=f[i].size-1;j++)

printf("%d--->",f[i].block[j]);

printf("%d",f[i].block[j]);

printf("\n");

OUTPUT:

Enter no. of files:2

Enter file name: abc

Enter starting block:20

Enter no.of blocks:6

Enter block numbers: 4

12 15 45 32 25

Enter file name: xyz

Enter starting block:12

Enter no.of blocks:5

Enter block numbers:6 5 4 3 2

File start size block

abc 20 6 4--->12--->15--->45--->32--->25
x
yz 12 5 6--->5--->4--->3--->2
C) INDEXED FILE ALLOCATION
#include<stdio.h>
int main()
{
int n,m[20],i,j,sb[20],s[20],b[20][20],x;

printf("Enter no. of files:");


scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter starting block and size of file%d:",i+1); scanf("%d%d",&sb[i],&s[i]);
printf("Enter blocks occupied by file%d:",i+1);
scanf("%d",&m[i]);
printf("enter blocks of file%d:",i+1);
for(j=0;j<m[i];j++)
scanf("%d",&b[i][j]);
}
printf("\nFile\t index\tlength\n");
for(i=0;i<n;i++)
{
printf("%d\t%d\t%d\n",i+1,sb[i],m[i]);
}
printf("\nEnter file name:");
scanf("%d",&x);
printf("file name is:%d\n",x);
i=x-1;
printf("Index is:%d",sb[i]);
printf("Block occupied are:");
for(j=0;j<m[i];j++)
printf("%d",b[i][j]);
}
OUTPUT:
Enter no. of files:2
Enter starting block and size of file1: 3 2

Enter blocks occupied by file1:3


Enter blocks of file1:2
32
Enter starting block and size of file2: 2 2
Enter blocks occupied by file2:2
enter blocks of file2: 2 2
File index length

1 3 3

2 2 2

Enter file name: 2

file name is:2

Index is:2

Block occupied are: 2 2


SHELL PROGRAMMING

A shell script is a computer program designed to be run by the Unix/Linux shell which could be
one of the following:

 The Bourne Shell


 The C Shell
 The Korn Shell

A shell is a command-line interpreter and typical operations performed by shell scripts include
file manipulation, program execution, and printing text. A Shell provides you with an interface
to the Unix system. It gathers input from you and executes programs based on that input. When
a program finishes executing, it displays that program's output. Shell script will have comments,
preceded by #

A shell script comprises following elements –

 Shell Keywords – if, else, break etc.


 Shell commands – cd, ls, echo, pwd, touch etc.
 Functions
 Control flow – if..then..else, case and shell loops etc.
Before you add anything else to your script, you need to alert the system that a shell script is
being started. This is done using the shebang construct. For example: #!/bin/sh
This tells the system that the commands that follow are to be executed by the Bourne shell. It's
called a shebang because the # symbol is called a hash, and the ! symbol is called a bang.
There are four main conditional statements in shell scripts:
if... ; then...; else...; fi
for ...; do...; done
while...; do...; done

Keyword for accepting input: read

Displaying output: echo


Assigning values to variables: Values can be assigned to variables through read statement or
also by using a simple assignment operator (while assigning variables no space should be given)

Operators in UNIX:

Operator Description

Checks if the value of two operands are equal or not; if yes, then the
-eq
condition becomes true.

Checks if the value of two operands are equal or not; if values are not
-ne
equal, then the condition becomes true.

Checks if the value of left operand is greater than the value of right
-gt
operand; if yes, then the condition becomes true.

Checks if the value of left operand is less than the value of right
-lt
operand; if yes, then the condition becomes true.

Checks if the value of left operand is greater than or equal to the


-ge
value of right operand; if yes, then the condition becomes true.

Checks if the value of left operand is less than or equal to the value
-le
of right operand; if yes, then the condition becomes true.
1. Shell script to perform arithmetic operations

# !/bin/bash

echo -n "enter any number:"

read fno

echo -n"enter any number:"

read sno

echo "Sum of two numbers is:"$[fno+sno]

echo "Diff of two nos is: $[fno-sno]

echo " Product of two nos is: $[fno*sno]

OUTPUT

sh read.sh

Enter any number: 5

Enter any number: 6

Sum of two number is: 11

Diff of two numbers is: -1

Product of two numbers is: 30


2. Program to find whether a number is odd or even

echo -n "Enter number : "

read n

rem=$(( $n % 2 ))

if [ $rem -eq 0 ] # -eq is a symbolof equal and 0 represent rem is zero

then

echo "$no. is even number"

else

echo "$no. is odd number"

fi

Output

sh filename.sh

Enter a number: 20

No. is even

sh filename.sh

Enter a number: 9

No is odd

3. Shell script to find factorial of a number

echo "enter a number"

read num
fact=1

while [ $num -ge 1 ]

do

fact=`expr $fact\* $num`

num=’expr $num – 1’

done

echo "factorial of $n is $fact"

Output

sh fact.sh

enter a number: 4

Factorial of 4 is 24

4. Unix Shell Program to check whether the given Year is Leap year or not.
enter the year
read y
a=`expr $y % 4`
b=`expr $y % 100`
c=`expr $y % 400`
if [ $a -eq 0 -a $b -ne 0 -o $c -eq 0 ]
then
echo $y is leap year
else
echo $y is not leap year
fi
OutPut
2010
2010 is not leap year
5. To write a shell script program to find the biggest among given two numbers
echo "Enter the first number\n”
read a
echo "Enter the second number\n "
read b
if [$a –gt $b]
echo “ $a is greater”
else
echo “$b is greater”
if
OUTPUT:
Enter the first number
3
Enter the second number
5
5 is greater

You might also like