You are on page 1of 29

OSMANIA UNIVERSITY

BSC(CS) – V SEMESTER
OPERATING SYSTEM LAB

1
a) Use vi editor to create different files, writing data into files, modifying data in files.
Creating Files

Method -1

You can use the vi editor to create ordinary files on any Unix system. You simply need
to give the following command –

$ vi filename

The above command will open a file with the given filename. Now, press the key i to
come into the edit mode. Once you are in the edit mode, you can start writing your
content in the file as in the following program −
This is unix file....I created it for the first time.....
I'm going to save this content in this file.
Once you are done with the program, follow these steps −

 Press the key esc to come out of the edit mode.

 Press two keys Shift + ZZ together to come out of the file completely.

You will now have a file created with filename in the current directory.
$ vi filename
$

Method – 2

Create a file using cat command

The cat (short for “concatenate“) command is one of the most frequently used command
in Linux/Unix like operating systems. cat command allows us to create single or
multiple files, view contain of file, concatenate files and redirect output in terminal or
files.
# cat >test2

Writing data into the files

The following operators can be used with the cat command:

Operato
Description
r

Redirect the output of the cat command to a file rather than standard


> output. The destination file will be created if it doesn't exist and will be
overwritten if it does.

Append the output of the cat command to the end of an existing file. The


>>
destination file will be created if it doesn't exist.

b) Use different types of Unix commands on the files created in first program.

To view contents of a file using cat


The cat command allows you to view contents of a file on the standard output (stdout).
This can be done in the following way:
$ cat [filename]
For example:
$ cat file1.txt

To display multiple files using cat


The tool also allows you to display contents of multiple files in one go. This can be done
in the following way:
$ cat [filename] [filename] ...
For example:
$ cat file1.txt  file2.txt

To display contents of file with line numbers


If you want, you can also display contents of a file with line numbers printed at the
beginning of each line. This can be done by using the tool’s -n command line option.
$ cat -n [filename]
For example:
$ cat -n  file1.txt

To copy the contents of one file to another file


You can also use cat to copy the contents of one file to another file. This can be done in
the following way: 
$ cat [filename-whose-contents-is-to-be-copied] > [destination-filename]
For example:
$ cat file1.txt > file3.txt

To concatenate two files.


$ cat sample.txt sample1.txt > sample2.txt
$ cat sample2.txt
This is a sample text file
This is a another sample text file

To put content of a file in a variable.


$ variable_content = 'cat sample.txt'

2 Write shell programs using ‘case’, ‘then’ and ‘if’ & ’else’ statements.

 CASE Example

clear
sum=0
i="y"

echo " Enter one no."


read n1
echo "Enter second no."
read n2
while [ $i = "y" ]
do
echo "1.Addition"
echo "2.Subtraction"
echo "3.Multiplication"
echo "4.Division"
echo "Enter your choice"
read ch
case $ch in
1)sum=`expr $n1 + $n2`
echo "Sum ="$sum;;
2)sum=`expr $n1 - $n2`
echo "Sub = "$sum;;
3)sum=`expr $n1 \* $n2`
echo "Mul = "$sum;;
4)sum=`expr $n1 / $n2`
echo "Div = "$sum;;
*)echo "Invalid choice";;
esac
echo "Do u want to continue ?"
read i
if [ $i != "y" ]
then
exit
fi
done

OUTPUT
***********
[04mca58@LINTEL 04mca58]$ sh calculator.sh
Enter any no.
121
Enter one no.
21
Enter second no.
58
1.Addition
2.Subtraction
3.Multiplication
4.Division
Enter your choice
1
Sum =79
Do u want to continue ?
y
1.Addition
2.Subtraction
3.Multiplication
4.Division
Enter your choice
2
Sub = -37
Do u want to continue ?
y
1.Addition
2.Subtraction
3.Multiplication
4.Division
Enter your choice
3
Mul = 1218
Do u want to continue ?
y
1.Addition
2.Subtraction
3.Multiplication
4.Division
Enter your choice
4
Div = 0
Do u want to continue ?
n

 if then fi example:

#!/bin/bash

count=100

if [ $count -eq 100 ]

then

echo "Count is 100"

 If – else Example

Program to find the year entered is leap year or not?

clear
echo -e "Enter an year : \c";
read year
if test $year -gt 0 ; then
x=`expr $year % 4`
y=`expr $year % 100`
z=`expr $year % 400`
if test $x -eq 0
then
if test $y -eq 0
then
if test $z -eq 0
then
cal 2 $year
else
echo "`echo $year` is not a leap-year"
fi
else
cal 2 $year
fi
else
echo "`echo $year` is not a leap-year"
fi
else
echo "Bad input!!!!!!!!!!!!!!!!!!"
fi

3 Write shell programs using while, do-while and for loop statements.

 WHILE loop Example

shell script to find the EVEN & ODD numbers in a given list of numbers?

clear

echo -e "Enter how many inputs you want :- \c"

read a

c=1

while test $c -le $a;do

echo -e "Enter no $c :- \c"

read b

if test `expr $b % 2` -eq 0;then

echo $b>>e

else

echo $b>>o

fi

c=`expr $c + 1`

done
echo -e "\nList of Odd numbers"

cat o

echo -e "\nList of Even numbers"


cat e

rm e

Output

Enter how many inputs you want :- 8

Enter no 1 :- 32

Enter no 2 :- 9

Enter no 3 :- 14

Enter no 4 :- 2

Enter no 5 :- 5

Enter no 6 :- 17

Enter no 7 :- 11

Enter no 8 :- 3

List of Odd numbers

17

11

List of Even numbers

32

14

 DO- WHILE
# SHELL SCRIPT FOR COUNT DOWN NUMBER TRIANGLE
#!/bin/sh

a=0

while [ "$a" -lt 10 ] # this is loop1

do

b="$a"

while [ "$b" -ge 0 ] # this is loop2

do

echo -n "$b "

b=`expr $b - 1`

done

echo

a=`expr $a + 1`

done
OUT PUT
0
10
210
3210
43210
543210
6543210
76543210
876543210
9876543210

 # FOR LOOP
shell script to rename all the files in the directory as extension .sh. Directories in
that directory do not get renamed.

for file in * ; do
x=`ls -ld $file | cut -c1`
if test $x != 'd' ; then
mv $file ${file}.sh
fi
done
4 . a) Write a shell script that accepts two integers as its arguments and
computers the value of first number raised to the power of the second number.

if [ $# -ne 2 ]
then
echo "Invalid number of arguments"
exit
fi
pwr=`echo $1^$2 |bc`
echo "$1 raised to $2 is : $pwr"

b) Write a shell script that takes a command –line argument and reports on
whether it is directory, a file, or something else.

echo "Enter a file name:"


read f
if [ -f $f ]
then
            echo "File"
elif [ -d $f ]
then
            echo "Directory"
else
            echo "Not"
fi

Output:

Directory

5 a) Write a shell script that accepts a file name, starting and ending line numbers
as arguments and displays all the lines between the given line numbers..

echo "enter the filename"


read fname
echo "enter the starting line number"
read s
echo "enter the ending line number"
read n
sed -n $s,$n\p $fname | cat > newline
cat newline
output:
[root@localhost ~]# vi 1s.sh
[root@localhost ~]# ./1s.sh
bash: ./1s.sh: Permission denied
[root@localhost ~]# chmod 777 1s.sh
[root@localhost ~]# ./1s.sh
enter the filename
sales.dat
enter the starting line number
2
enter the ending line number
4
1 computers 9161
1 textbooks 21312 2 clothing 3252

b) Write a shell script that deletes all lines containing a specified word in one or
more files supplied as arguments to it.

if [ $# -eq 0 ]
then
echo "Please enter one or more filenames as argument"
exit
fi
echo "Enter the word to be searched in files"
read word
for file in $*
do
sed "/$word/d" $file | tee tmp
mv tmp $file
done

6 a) Write a shell script that displays a list of all the files in the current directory
to which the user has read, write and execute permissions.

echo "enter the directory name"


read dir
if [ -d $dir ]
then
cd $dir
ls > f
exec < f
while read line
do
if [ -f $line ]
then
if [ -r $line -a -w $line -a -x $line ]
then
echo "$line has all permissions"
else
echo "files not having all permissions"
fi
fi
done
fi
Output:
student@ubuntu:~$sh prg3.sh
enter the directory name
dir1
ff has all permissions
files not having permissions

b) Develop an interactive script that ask for a word and a file name and then tells
how many times that word occurred in the file.

echo " Enter the word to be searched"


read word
echo "Enter the filename to be used"
read flname
echo "the no. of times the word ['$word'] occured in the file."
grep -o $word $flname|wc –l
echo " the no of line that contains ['$word'] is"
grep -c $word $flname
Output:
$ sh wcount.sh
Enter the word to be searched
By
Enter the filename to be used
f1.txt
the no. of times the word ['by'] occured in the file.
6
the no of line that contains ['by'] is
3

7 Write a program that simulate the following Unix commands like ls, mv, cp.
#include<fcntl.h>
#include<unistd.h>
#include<stdio.h>
main(int argc,char *argv[])
{
FILE *fp;
char ch;
int sc=0;

fp=fopen(argv[1],"r");
if(fp==NULL)
   printf("unable to open a file",argv[1]);
else
{
  while(!feof(fp))
  {
   ch=fgetc(fp);
   if(ch==' ')
   sc++;
 }
  printf("no of spaces %d",sc);
  printf("\n");
  fclose(fp);
 }
}
8 Write a program to convert upper case to lower case letters of a given ASCII file.
/* C Program - Convert Uppercase Character to Lowercase */

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
char ch;
printf("Enter a character in uppercase : ");
scanf("%c",&ch);
ch=ch+32;
printf("character in lowercase = %c",ch);
getch();
}

9 Write a program to program to search the given pattern in a file.


#include <stdio.h>

int main()
{
char file_to_open[] = "text_file.txt", ch;
FILE *file_ptr;

if((file_ptr = fopen(file_to_open, "r")) != NULL)


{
while((ch = fgetc(file_ptr)) != EOF)
{
putchar(ch);
}
}
else
{
printf("Could not open %s\n", file_to_open);
return 1;
}
return(0);
}

10 Write a program to demonstrate FCFS process schedules on the given data.

#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<10;i++)
            {
                        a[i]=0; b[i]=0; w[i]=0; g[i]=0;
            }
printf("enter the number of process");
            scanf("%d",&n);
printf("enter the burst times");
            for(i=0;i<n;i++)
                scanf("%d",&b[i]);
    printf("\nenter the arrival times");
            for(i=0;i<n;i++)
                scanf("%d",&a[i]);
    g[0]=0;
             for(i=0;i<10;i++)
                   g[i+1]=g[i]+b[i];
             for(i=0;i<n;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\tprocess\twaiting time\tturn arround time\n");
            for(i=0;i<n;i++)
            {
                        printf("\tp%d\t\t%d\t\t%d\n",i,w[i],t[i]);
            }
printf("the average waiting time is %f\n",awt);
printf("the average turn around time is %f\n",att);
}
 
OUTPUT:
enter the number of process 4
enter the burst times
4  9  8  3
enter the arrival times
0  2  4  3
        process        waiting time     turn arround time
            p0                       0                          4
            p1                       2                         11
            p2                       9                         17
            p3                      18                        21
the average waiting time is 7.250000
the average turn around time is 13.250000
11 Write a program to demonstrate SJF process schedules on the given data.

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int et[20],at[10],n,i,j,temp,st[10],ft[10],wt[10],ta[10];
int totwt=0,totta=0;
float awt,ata;
char pn[10][10],t[10];
//clrscr();
printf("Enter the number of process:");
scanf("%d",&n);
for(i=0; i<n; i++)
{
printf("Enter process name, arrival time& execution time:");
//flushall();
scanf("%s%d%d",pn[i],&at[i],&et[i]);
}
for(i=0; i<n; i++)
for(j=0; j<n; j++)
{
if(et[i]<et[j])
{
temp=at[i];
at[i]=at[j];
at[j]=temp;
temp=et[i];
et[i]=et[j];
et[j]=temp;
strcpy(t,pn[i]);
strcpy(pn[i],pn[j]);
strcpy(pn[j],t);
}
}
for(i=0; i<n; i++)
{
if(i==0)
st[i]=at[i];
else
st[i]=ft[i-1];
wt[i]=st[i]-at[i];
ft[i]=st[i]+et[i];
ta[i]=ft[i]-at[i];
totwt+=wt[i];
totta+=ta[i];
}
awt=(float)totwt/n;
ata=(float)totta/n;
printf("\nPname\tarrivaltime\texecutiontime\twaitingtime\ttatime");
for(i=0; i<n; i++)
printf("\n%s\t%5d\t\t%5d\t\t%5d\t\t%5d",pn[i],at[i],et[i],wt[i],ta[i]);
printf("\nAverage waiting time is:%f",awt);
printf("\nAverage turnaroundtime is:%f",ata);
getch();
}

 OUTPUT:

Enter the number of process:3


Enter process name, arrival time&amp; execution time:2 5 7
Enter process name, arrival time&amp; execution time:3 6 14
Enter process name, arrival time&amp; execution time:4 7 12
 
Pname arrivaltime executiontime waitingtime tatime
2 5 7 0 7
4 7 12 5 17
3 6 14 18 32
Average waiting time is:7.666667
Average turnaroundtime is:18.666666

12 Write a program to demonstrate Priority Scheduling on the given burst time


and arrival times.

#include<stdio.h>
 int main()
{
    int bt[20],p[20],wt[20],tat[20],pr[20],i,j,n,total=0,pos,temp,avg_wt,avg_tat;
    printf("Enter Total Number of Process:");
    scanf("%d",&n);
 
    printf("\nEnter Burst Time and Priority\n");
    for(i=0;i<n;i++)
    {
        printf("\nP[%d]\n",i+1);
        printf("Burst Time:");
        scanf("%d",&bt[i]);
        printf("Priority:");
        scanf("%d",&pr[i]);
        p[i]=i+1;           //contains process number
    }
 
    //sorting burst time, priority and process number in ascending order using selection
sort
    for(i=0;i<n;i++)
    {
        pos=i;
        for(j=i+1;j<n;j++)
        {
            if(pr[j]<pr[pos])
                pos=j;
        }
 
        temp=pr[i];
        pr[i]=pr[pos];
        pr[pos]=temp;
 
        temp=bt[i];
        bt[i]=bt[pos];
        bt[pos]=temp;
 
        temp=p[i];
        p[i]=p[pos];
        p[pos]=temp;
    }
 
    wt[0]=0;    //waiting time for first process is zero
 
    //calculate waiting time
    for(i=1;i<n;i++)
    {
        wt[i]=0;
        for(j=0;j<i;j++)
            wt[i]+=bt[j];
 
        total+=wt[i];
    }
 
    avg_wt=total/n;      //average waiting time
    total=0;
 
    printf("\nProcess\t    Burst Time    \tWaiting Time\tTurnaround Time");
    for(i=0;i<n;i++)
    {
        tat[i]=bt[i]+wt[i];     //calculate turnaround time
        total+=tat[i];
        printf("\nP[%d]\t\t  %d\t\t    %d\t\t\t%d",p[i],bt[i],wt[i],tat[i]);
    }
 
    avg_tat=total/n;     //average turnaround time
    printf("\n\nAverage Waiting Time=%d",avg_wt);
    printf("\nAverage Turnaround Time=%d\n",avg_tat);
     return 0;
}
13 Write a program to demonstrate Round Robin Scheduling on the given burst
time and arrival times.
#include<stdio.h>
 
int main()
{
 
  int count,j,n,time,remain,flag=0,time_quantum;
  int wait_time=0,turnaround_time=0,at[10],bt[10],rt[10];
  printf("Enter Total Process:\t ");
  scanf("%d",&n);
  remain=n;
  for(count=0;count<n;count++)
  {
    printf("Enter Arrival Time and Burst Time for Process Process Number
%d :",count+1);
    scanf("%d",&at[count]);
    scanf("%d",&bt[count]);
    rt[count]=bt[count];
  }
  printf("Enter Time Quantum:\t");
  scanf("%d",&time_quantum);
  printf("\n\nProcess\t|Turnaround Time|Waiting Time\n\n");
  for(time=0,count=0;remain!=0;)
  {
    if(rt[count]<=time_quantum && rt[count]>0)
    {
      time+=rt[count];
      rt[count]=0;
      flag=1;
    }
    else if(rt[count]>0)
    {
      rt[count]-=time_quantum;
      time+=time_quantum;
    }
    if(rt[count]==0 && flag==1)
    {
      remain--;
      printf("P[%d]\t|\t%d\t|\t%d\n",count+1,time-at[count],time-at[count]-bt[count]);
      wait_time+=time-at[count]-bt[count];
      turnaround_time+=time-at[count];
      flag=0;
    }
    if(count==n-1)
      count=0;
    else if(at[count+1]<=time)
      count++;
    else
      count=0;
  }
  printf("\nAverage Waiting Time= %f\n",wait_time*1.0/n);
  printf("Avg Turnaround Time = %f",turnaround_time*1.0/n);
    return 0;
}

14 Write a program to implementing Producer and Consumer problem using


Semaphores.

#include<stdio.h>
#include<stdlib.h>
 
int mutex=1,full=0,empty=3,x=0;
int main()
{
    int n;
    void producer();
    void consumer();
    int wait(int);
    int signal(int);
    printf("\n1.Producer\n2.Consumer\n3.Exit");
    while(1)
    {
        printf("\nEnter your choice:");
        scanf("%d",&n);
        switch(n)
        {
            case 1:    if((mutex==1)&&(empty!=0))
                        producer();
                    else
                        printf("Buffer is full!!");
                    break;
            case 2:    if((mutex==1)&&(full!=0))
                        consumer();
                    else
                        printf("Buffer is empty!!");
                    break;
            case 3:
                    exit(0);
                    break;
        }
    }
    
    return 0;
}
 
int wait(int s)
{
    return (--s);
}
 int signal(int s)
{
    return(++s);
}
 
void producer()
{
    mutex=wait(mutex);
    full=signal(full);
    empty=wait(empty);
    x++;
    printf("\nProducer produces the item %d",x);
    mutex=signal(mutex);
}
 
void consumer()
{
    mutex=wait(mutex);
    full=wait(full);
    empty=signal(empty);
    printf("\nConsumer consumes item %d",x);
    x--;
    mutex=signal(mutex);
}
Output
1.Producer
2.Consumer
3.Exit
Enter your choice:1
Producer produces the item 1
Enter your choice:2
Consumer consumes item 1
Enter your choice:2
Buffer is empty!!
Enter your choice:1
Producer produces the item 1
Enter your choice:1
Producer produces the item 2
Enter your choice:1
Producer produces the item 3
Enter your choice:1
Buffer is full!!
Enter your choice:3

15 Write a program to simulate FIFO, LRU, LFU Page replacement algorithms.


#include
void FIFO(char [],char [],int,int);
void lru(char [],char [],int,int);
void opt(char [],char [],int,int);

int main()
{
   int ch,YN=1,i,l,f;
   char F[10],s[25];
   clrscr();
   //system(“clear”);
   printf(“\n\n\tEnter the no of empty frames: “);
   scanf(“%d”,&f);
   printf(“\n\n\tEnter the length of the string: “);
   scanf(“%d”,&l);
   printf(“\n\n\tEnter the string: “);
   scanf(“%s”,s);
   for(i=0;i<f;i++)
     F[i]=-1;
      do
    {
     // system(“clear”);
      printf(“\n\n\t*********** MENU ***********”);
      printf(“\n\n\t1:FIFO\n\n\t2:LRU\n\n\t3:OPT\n\n\t4:EXIT”);
      printf(“\n\n\tEnter your choice: “);
      scanf(“%d”,&ch);
      //system(“clear”);
      switch(ch)
       {
      case 1:
          for(i=0;i<f;i++)
           {
             F[i]=-1;
           }

          FIFO(s,F,l,f);


          break;
      case 2:
          for(i=0;i<f;i++)
           {
             F[i]=-1;
           }
          lru(s,F,l,f);
          break;
      case 3:
          for(i=0;i<f;i++)
           {
             F[i]=-1;
           }
          opt(s,F,l,f);
          break;
      case 4:
          exit(0);
       }
      printf(“\n\n\tDo u want to continue IF YES PRESS 1\n\n\tIF NO PRESS 0 : “);
      scanf(“%d”,&YN);
    }while(YN==1);return(0);
}

//FIFO
void FIFO(char s[],char F[],int l,int f)
{
   int i,j=0,k,flag=0,cnt=0;
   printf(“\n\tPAGE\t    FRAMES\t  FAULTS”);
   for(i=0;i<l;i++)
    {
       for(k=0;k<f;k++)
    {
      if(F[k]==s[i])
        flag=1;
    }

       if(flag==0)
    {
      printf(“\n\t%c\t”,s[i]);
      F[j]=s[i];
      j++;

      for(k=0;k<f;k++)
       {
        printf(”   %c”,F[k]);
       }
      printf(“\tPage-fault%d”,cnt);
      cnt++;
    }

       else
    {
      flag=0;
      printf(“\n\t%c\t”,s[i]);
      for(k=0;k<f;k++)
       {
        printf(”   %c”,F[k]);
       }

      printf(“\tNo page-fault”);


    }
       if(j==f)
    j=0;
    }

//LRU
void lru(char s[],char F[],int l,int f)
{
   int i,j=0,k,m,flag=0,cnt=0,top=0;
   printf(“\n\tPAGE\t    FRAMES\t  FAULTS”);
   for(i=0;i<l;i++)
    {
       for(k=0;k<f;k++)
    {
      if(F[k]==s[i])
       {
        flag=1;
         break;
       }
    }

       printf(“\n\t%c\t”,s[i]);
       if(j!=f && flag!=1)
    {
      F[top]=s[i];
      j++;

      if(j!=f)
       top++;
    }
       else
    {
       if(flag!=1)
        {
          for(k=0;k<top;k++)
           {
        F[k]=F[k+1];
           }

           F[top]=s[i];


        }
       if(flag==1)
        {
           for(m=k;m<top;m++)
           {
        F[m]=F[m+1];
           }
           F[top]=s[i];
        }
    }
       for(k=0;k<f;k++)
    {
     printf(”   %c”,F[k]);
    }

       if(flag==0)
    {
      printf(“\tPage-fault%d”,cnt);
      cnt++;
    }
       else
     printf(“\tNo page fault”);
       flag=0;
    }

//LFU
void LFU()
{
int q[20],p[50],c=0,c1,d,f,i,j,k=0,n,r,t,b[20],c2[20];
printf(“Enter no of pages:”);
scanf(“%d”,&n);
printf(“Enter the reference string:”);
for(i=0;i<n;i++)
scanf(“%d”,&p[i]);
printf(“Enter no of frames:”);
scanf(“%d”,&f);
q[k]=p[k];
printf(“\n\t%d\n”,q[k]);
c++;
k++;
for(i=1;i<n;i++)
{
c1=0;
for(j=0;j<f;j++)
{
if(p[i]!=q[j])
c1++;
}
if(c1==f)
{
c++;
if(k<f)
{
q[k]=p[i];
k++;
for(j=0;j<k;j++)
printf(“\t%d”,q[j]);
printf(“\n”);
}
else
{
for(r=0;r<f;r++)
{
c2[r]=0;
for(j=i-1;j<n;j–)
{
if(q[r]!=p[j])
c2[r]++;
else
break;
}
}
for(r=0;r<f;r++)
b[r]=c2[r];
for(r=0;r<f;r++)
{
for(j=r;j<f;j++)
{
if(b[r]<b[j])
{
t=b[r];
b[r]=b[j];
b[j]=t;
}
}
}
for(r=0;r<f;r++)
{
if(c2[r]==b[0])
q[r]=p[i];
printf(“\t%d”,q[r]);
}
printf(“\n”);
}
}
}
printf(“\nThe no of page faults is %d”,c);
}

/*
16 Write a program to simulate Sequential, Indexed, and Linked file allocation
strategies.

Sequential File Allocation Program:

#include
#include
main()
{
int f[50],i,st,j,len,c,k;
clrscr();
for(i=0;i<50;i++)
f[i]=0;
X:
printf("\n Enter the starting block & length of file");
scanf("%d%d",&st,&len);
for(j=st;j<(st+len);j++)
if(f[j]==0)
{
f[j]=1;
printf("\n%d->%d",j,f[j]);
}
else
{
printf("Block already allocated");
break;
}
if(j==(st+len))
printf("\n the file is allocated to disk");
printf("\n if u want to enter more files?(y-1/n-0)");
scanf("%d",&c);
if(c==1)
goto X;
else
exit();
getch();
}

b)Linked File Allocation Program:

#include
#include
main()
{
int f[50],p,i,j,k,a,st,len,n,c;
clrscr();
for(i=0;i<50;i++)
f[i]=0;
printf("Enter how many blocks that are already allocated");
scanf("%d",&p);
printf("\nEnter the blocks no.s that are already allocated");
for(i=0;i<p;i++)
{
scanf("%d",&a);
f[a]=1;
}
X:
printf("Enter the starting index block & length");
scanf("%d%d",&st,&len);
k=len;
for(j=st;j<(k+st);j++)
{
if(f[j]==0)
{
f[j]=1;
printf("\n%d->%d",j,f[j]);
}
else
{
printf("\n %d->file is already allocated",j);
k++;
}
}
printf("\n If u want to enter one more file? (yes-1/no-0)");
scanf("%d",&c);
if(c==1)
goto X;
else
exit();
getch( );
}

c)Indexed File Allocation Program:


#include
int f[50],i,k,j,inde[50],n,c,count=0,p;
main()
{
clrscr();
for(i=0;i<50;i++)
f[i]=0;
x:
printf("enter index block\t");
scanf("%d",&p);
if(f[p]==0)
{
f[p]=1;
printf("enter no of files on index\t");
scanf("%d",&n);
}
else
{
printf("Block already allocated\n");
goto x;
}
for(i=0;i<n;i++)
scanf("%d",&inde[i]);
for(i=0;i<n;i++)
if(f[inde[i]]==1)
{
printf("Block already allocated");
goto x;
}
for(j=0;j<n;j++)
f[inde[j]]=1;
printf("\n allocated");
printf("\n file indexed");
for(k=0;k<n;k++)
printf("\n %d->%d:%d",p,inde[k],f[inde[k]]);
printf(" Enter 1 to enter more files and 0 to exit\t");
scanf("%d",&c);
if(c==1)
goto x;
else
exit();
getch();
}

You might also like