UNIX Commands and Shell Programming Guide
UNIX Commands and Shell Programming Guide
LAB MANUAL
R LAKSHMAN NAIK
Assistant Professor©
You can customize your command prompt using environment variable PS1
explained in Environment tutorial.
Shell Types:
In UNIX there are two major types of shells:
1. The Bourne shell. If you are using a Bourne-type shell, the default prompt is the $
character.
2. The C shell. If you are using a C-type shell, the default prompt is the % character.
There are again various subcategories for Bourne Shell which is listed as follows:
• Bourne shell (sh)
• Korn shell (ksh)
• Bourne again shell (bash)
• POSIX shell (sh)
The different C-type shells follow:
• C shell (csh)
• TENEX/TOPS C shell (tcsh)
Example Script:
Assume we create a [Link] script. Note all the scripts would have .sh extension.
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.
To create a script containing these commands, you put the shebang line first and then
add the commands:
#!/bin/bash pwd
ls
Shell Comments:
You can put your comments in your script as follows:
#!/bin/bash
# Author : laxman
# Copyright (c) KU
# Script follows here:
pwd ls
Now you save the above content and make this script executable as follows:
$chmod +x [Link]
Now you have your shell script ready to be executed as follows:
$./[Link]
Example2:
To create a script containing athematic operation, create file
Vi [Link]
$ c=`expr $a + $b`
$ echo “the value of addition=$c”
$ d=`expr $a - $b`
$ echo “the value of subtraction=$d”
$ e= expr $a \* $b`
$ echo “the value of multiplication=$e”
$ f=`expr $a / $b`
$ echo “the value of division=$f”
$ g= echo `expr $a % $b`
R LAKSHMAN NAIK Dept. of IT, KUCE&T |4
#include <iostream>
using namespace std;
int main()
{
cout << "Hello Ubuntu Lover in C++" << endl;
return 0;
}
6. Compile your first C++ program
$g++ [Link] -o myhellocpp
7. Run your first C++ program
$ ./myhellocpp
Hello Ubuntu Lover in C++
Note: - If you should happen to get permission errors, you need to make the file executable.
You can do this by issuing the following commands below
#chmod +x HelloWorld.C
or
#chmod +x [Link]
R LAKSHMAN NAIK Dept. of IT, KUCE&T |5
#include<stdio.h>
main()
{
printf(“pid=%d pgrp=%d”,getpid(),getpgrp(0));
}
Output:-
pid=4267 pgrp=4267
#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>
main()
{
printf(“processid: pid=%d”,getpid());
printf(“parent processid: ppid=%d”,getppid());
printf(“userid : uid=%d”,getuid());
printf(“user groupid=%d”,getgid());
}
Output:-
processid: pid=4574
parent processid: ppid=3263
processid: pid=500
user groupid=500
#include<stdio.h>
main()
{
int fpid;
printf(“before forking fpid=%d”,fpid);
system(“ps”);
fpid=fork();
system(“ps”);
printf(“after forking fpid=%d”,fpid);
}
Output:-
Pid tty time cmd
3263 pts/11 00:00:00 bash
4865 pts/11 00:00:00 [Link]
R LAKSHMAN NAIK Dept. of IT, KUCE&T |6
#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>
main()
{
int fpid;
printf(“before forking fpid=%d pid=%d ppid=%d\n”,fpid,getpid(),getppid());
fpid=fork();
if(fpid==0)
{
printf(“child process fpid=%d pid=%d ppid=%d\n”, fpid,getpid(),getppid());
}
else
{
printf(“parent process fpid=%d pid=%d ppid=%d\n”, fpid,getpid(),getppid());
}
printf(“after forking fpid=%d pid=%d ppid=%d\n”, fpid,getpid(),getppid());
}
Output:-
before forking fpid=13725684 pid=3753 ppid=3062
child process fpid=0 pid=3754 ppid=3753
after forking fpid=0 pid=3754 ppid=3753
parent process fpid=3754 pid=3753 ppid=3062
after forking fpid=3754 pid=3753 ppid=3062
#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>
main()
{
int fpid,status;
printf(“before forking fpid=%d pid=%d ppid=%d\n”,fpid,getpid(),getppid());
fpid=fork();
if(fpid==0)
{
printf(“child process fpid=%d pid=%d ppid=%d\n”, fpid,getpid(),getppid());
}
else
{
printf(“parent process fpid=%d pid=%d ppid=%d\n”, fpid,getpid(),getppid());
}
Wait(status);
printf(“after forking fpid=%d pid=%d ppid=%d\n”, fpid,getpid(),getppid());
}
R LAKSHMAN NAIK Dept. of IT, KUCE&T |7
Output:-
before forking fpid=-1081197880 pid=4004 ppid=3062
child process fpid=0 pid=4005 ppid=4004
after forking fpid=0 pid=4005 ppid=4004
parent process fpid=4005 pid=4004 ppid=3062
after forking fpid=4005 pid=4004 ppid=3062
#include<stdio.h>
#include<unistd.h>
main()
{
If(fork()==0)
{
printf(“pid=%d\t ppid=%d”,getpid(),getppid());
If(fork()==0)
{
printf(“pid=%d\t ppid=%d”,getpid(),getppid());
If(fork()==0)
printf(“pid=%d\t ppid=%d”,getpid(),getppid());
}}}
Output:-
pid=4311 ppid=4310
pid=4311 ppid=4312
pid=4312 ppid=4313
pid=4311 ppid=4314
#include<stdio.h>
#include<unistd.h>
main()
{
int pipefd[2],n;
char buff[100];
if(pipe(pipefd)<0)
perror(“pipe error”);
printf(“read fd=%d,write fd=%d\n”,pipefd[1],pipefd[2]);
if(write(pipefd[1],”hello world\n”,12)!=12)
perror(“write error”);
if(n=read(pipefd[0],buff,sizeof(buff)<=0))
perror(“read error”);
write(1,buff,n);
}
R LAKSHMAN NAIK Dept. of IT, KUCE&T |8
Output:-
readfd=3
writefd=4
#include<stdio.h>
#include<unistd.h>
main()
{
int pid,fd[2];
char buff[10]={};
pipe(fd);
pid=fork();
if(pid==0)
{
close(fd[0]);
printf(“is the child\n”);
write(fd[1],”hello”,5);
}
else
{
close(fd[1]);
printf(“is the parent”);
read(fd[0],buff,5);
printf(“%s”,buff);
}}
Output:-
is the child
is the paent hello
#include<stdio.h>
#include<unistd.h>
#include<fctnl.h>
main()
{
int fd1,fd2,fd3;
printf(“before open”);
fd1=open(“/etc/passwd”,0_RDONLY);
fd2=open(“/etc/passwd”,0_WRONLY);
fd3=open(“/etc/passwd”,0_RDONLY);
printf(“fd1=%d fd2=%d fd3=%d” fd1,fd2,fd3”);
}
Output:-
before open fd1=3 fd2=-1 fd3=4
R LAKSHMAN NAIK Dept. of IT, KUCE&T |9
#include<stdio.h>
#include<unistd.h>
#include<fctnl.h>
main()
{
int fd1;
char buf1[20],buf2[20];
buf1[19]=’\0’;
buf2[19]=’\0’;
printf(“****************\n”);
fd1=open(“/etc/passwd”,O_RDONLY);
read(fd1,buf1,19);
printf(“fd1=%d buf1=%s\n”,fd1,buf1);
read(fd1,buf2,19);
printf(“fd1=%d buf2=%s\n”,fd1,buf2);
printf(“*************\n”);
}
Output:-
***************************
fd1=3 buf1=root:*:0:0:root:/00
fd1=4 buf2=root:*:0:0:root:/00
***************************
f. Write a program to demonstrate the read() system call reading two files.
#include<stdio.h>
#include<unistd.h>
#include<fctnl.h>
main()
{
int fd1,fd2,fd3;
char buf1[20],buf2[20];
buf1[19]=’\0’;
buf2[19]=’\0’;
printf(“****************\n”);
fd1=open(“/etc/passwd”,O_RDONLY);
fd2=open(“/etc/passwd”,O_RDONLY);
read(fd1,buf1,19);
printf(“fd1=%d buf1=%s\n”,fd1,buf1);
read(fd2,buf2,19);
printf(“fd2=%d buf2=%s\n”,fd2,buf2);
printf(“*************\n”);
}
Output:-
***************
fd1=-1 buf1=8t
R L A K S H M A N N A I K D e p t . o f I T , K U C E & T | 10
fd2=-1 buf2=8t
putty
***************
g. Write a program to demonstrate the creat(), write() and close() system calls.
#include<stdio.h>
#include<unistd.h>
#include<fctnl.h>
main()
{
int fd1;
char *buf1=”I am a string”;
char *buf1=”I am second string”;
printf(“****************\n”);
fd1=creat(“./[Link]”,O_WRONLY);
write(fd1,buf1,20);
write(fd1,buf2,30);
printf(“fd1=%d buf1=%s\n”,fd1,buf1);
close(fd1);
chmod(“./[Link]”,0666);
printf(“****************\n”);
}
Output:-
********************
fd1=3 buf1=i am a string
********************
#include<stdio.h>
#include<conio.h>
main()
{
int bt[20], wt[20], tat[20], i, n;
float wtavg, tatavg;
clrscr();
printf("\nEnter the number of processes -- ");
scanf("%d", &n);
for(i=0;i<n;i++)
{
}
wt[0] = wtavg = 0;
tat[0] = tatavg = bt[0];
for(i=1;i<n;i++)
{
wt[i] = wt[i-1] +bt[i-1];
tat[i] = tat[i-1] +bt[i];
wtavg = wtavg + wt[i];
tatavg = tatavg + tat[i];
}
printf("\t PROCESS \tBURST TIME \t WAITING TIME\t TURNAROUND
TIME\n");
for(i=0;i<n;i++)
printf("\n\t P%d \t\t %d \t\t %d \t\t %d", i, bt[i], wt[i], tat[i]);
printf("\nAverage Waiting Time -- %f", wtavg/n);
printf("\nAverage Turnaround Time -- %f", tatavg/n);
getch();
return(0);
}
#include<stdio.h>
#include<conio.h>
main()
{
int p[20], bt[20], wt[20], tat[20], i, k, n, temp;
float wtavg, tatavg;
clrscr();
printf("\nEnter the number of processes -- ");
scanf("%d", &n);
for(i=0;i<n;i++)
{
p[i]=i;
printf("Enter Burst Time for Process %d -- ", i);
scanf("%d", &bt[i]);
}
for(i=0;i<n;i++)
R L A K S H M A N N A I K D e p t . o f I T , K U C E & T | 12
for(k=i+1;k<n;k++)
if(bt[i]>bt[k])
{
temp=bt[i];
bt[i]=bt[k];
bt[k]=temp;
temp=p[i];
p[i]=p[k];
p[k]=temp;
}
wt[0] = wtavg = 0;
tat[0] = tatavg = bt[0];
for(i=1;i<n;i++)
{
wt[i] = wt[i-1] +bt[i-1];
tat[i] = tat[i-1] +bt[i];
wtavg = wtavg + wt[i];
tatavg = tatavg + tat[i];
}
printf("\n\t PROCESS \tBURST TIME \t WAITING TIME\t TURNAROUND
TIME\n");
for(i=0;i<n;i++)
printf("\n\t P%d \t\t %d \t\t %d \t\t %d", p[i], bt[i], wt[i], tat[i]);
printf("\nAverage Waiting Time -- %f", wtavg/n); printf("\nAverage
Turnaround Time -- %f", tatavg/n);
getch();
return(0);
}
#include<stdio.h>
main()
{
int i,j,n,bu[10],wa[10],tat[10],t,ct[10],max;
float awt=0,att=0,temp=0;
clrscr();
printf("Enter the no of processes -- ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
R L A K S H M A N N A I K D e p t . o f I T , K U C E & T | 13
#include<stdio.h>
main()
{
int p[20],bt[20],pri[20], wt[20],tat[20],i, k, n, temp;
float wtavg, tatavg;
clrscr();
printf("Enter the number of processes --- ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
p[i] = i;
printf("Enter the Burst Time & Priority of Process %d --- ",i);
scanf("%d %d",&bt[i], &pri[i]);
}
for(i=0;i<n;i++)
for(k=i+1;k<n;k++)
if(pri[i] > pri[k])
{
temp=p[i];
p[i]=p[k];
p[k]=temp;
temp=bt[i];
bt[i]=bt[k];
bt[k]=temp;
temp=pri[i];
pri[i]=pri[k];
pri[k]=temp;
}
wtavg = wt[0] = 0;
tatavg = tat[0] = bt[0];
for(i=1;i<n;i++)
{
wt[i] = wt[i-1] + bt[i-1];
tat[i] = tat[i-1] + bt[i];
#include<stdio.h>
main()
{
int p[20],bt[20], su[20], wt[20],tat[20],i, k, n, temp;
float wtavg, tatavg;
clrscr();
printf("Enter the number of processes --- ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
p[i] = i;
printf("Enter the Burst Time of Process %d --- ", i);
scanf("%d",&bt[i]);
printf("System/User Process (0/1) ? --- ");
scanf("%d", &su[i]);
}
for(i=0;i<n;i++)
for(k=i+1;k<n;k++)
if(su[i] > su[k])
{
temp=p[i];
p[i]=p[k];
p[k]=temp;
temp=bt[i];
bt[i]=bt[k];
bt[k]=temp;
temp=su[i];
su[i]=su[k];
su[k]=temp;
R L A K S H M A N N A I K D e p t . o f I T , K U C E & T | 16
wtavg = wt[0] = 0;
tatavg = tat[0] = bt[0];
for(i=1;i<n;i++)
{
wt[i] = wt[i-1] + bt[i-1];
tat[i] = tat[i-1] + bt[i];
wtavg = wtavg + wt[i];
tatavg = tatavg + tat[i];
}
printf("\nPROCESS\t\t SYSTEM/USER PROCESS \tBURST
TIME\tWAITING TIME\tTURNAROUND TIME");
for(i=0;i<n;i++)
printf("\n%d \t\t %d \t\t %d \t\t %d \t\t %d ",p[i],su[i],bt[i],wt[i],tat[i]);
#include<stdio.h>
void main()
{
int buffer[10], bufsize, in, out, produce, consume, choice=0;
in = 0;
out = 0;
bufsize = 10;
while(choice !=3)
{
printf(“\n1. Produce \t 2. Consume \t3. Exit”);
R L A K S H M A N N A I K D e p t . o f I T , K U C E & T | 17
else
{
printf(“\nEnter the value: “);
scanf(“%d”, &produce);
buffer[in] = produce;
in = (in+1)%bufsize;
} Break;
}
}
}
R L A K S H M A N N A I K D e p t . o f I T , K U C E & T | 18
#include<stdio.h>
struct file
{
int all[10];
int max[10];
int need[10];
int flag;
};
void main()
{
struct file f[10];
int fl;
int i, j, k, p, b, n, r, g, cnt=0, id, newr;
int avail[10],seq[10];
clrscr();
printf("Enter number of processes -- ");
scanf("%d",&n);
printf("Enter number of resources -- ");
scanf("%d",&r);
for(i=0;i<n;i++)
{
printf("Enter details for P%d",i); printf("\nEnter allocation\t -- \t");
for(j=0;j<r;j++)
scanf("%d",&f[i].all[j]);
printf("Enter Max\t\t -- \t");
for(j=0;j<r;j++)
scanf("%d",&f[i].max[j]);
f[i].flag=0;
}
printf("\nEnter Available Resources\t -- \t");
for(i=0;i<r;i++)
scanf("%d",&avail[i]);
for(i=0;i<n;i++)
{
for(j=0;j<r;j++)
R L A K S H M A N N A I K D e p t . o f I T , K U C E & T | 19
{
f[i].need[j]=f[i].max[j]-f[i].all[j];
if(f[i].need[j]<0)
f[i].need[j]=0;
}
}
cnt=0;
fl=0;
while(cnt!=n)
{
g=0;
for(j=0;j<n;j++)
{
if(f[j].flag==0)
{
b=0;
for(p=0;p<r;p++)
{
if(avail[p]>=f[j].need[p])
b=b+1;
else
b=b-1;
}
if(b==r)
{
printf("\nP%d is visited",j);
seq[fl++]=j; f[j].flag=1; for(k=0;k<r;k++)
avail[k]=avail[k]+f[j].all[k];
cnt=cnt+1; printf("("); for(k=0;k<r;k++)
printf("%3d",avail[k]);
printf(")");
g=1;
}
}
}
if(g==0)
{
printf("\n REQUEST NOT GRANTED -- DEADLOCK OCCURRED");
printf("\n SYSTEM IS IN UNSAFE STATE");
goto y;
}
}
printf("\nSYSTEM IS IN SAFE STATE");
printf("\nThe Safe Sequence is -- (");
for(i=0;i<fl;i++)
printf("P%d ",seq[i]);
printf(")");
y: printf("\nProcess\t\tAllocation\t\tMax\t\t\tNeed\n");
for(i=0;i<n;i++)
R L A K S H M A N N A I K D e p t . o f I T , K U C E & T | 20
{
printf("P%d\t",i);
for(j=0;j<r;j++)
printf("%6d",f[i].all[j]);
for(j=0;j<r;j++)
printf("%6d",f[i].max[j]);
for(j=0;j<r;j++)
printf("%6d",f[i].need[j]);
printf("\n");
}
getch();
}
#include<stdio.h>
#include<conio.h>
main()
{
int ms, ps, nop, np, rempages, i, j, x, y, pa, offset;
int s[10], fno[10][20];
R L A K S H M A N N A I K D e p t . o f I T , K U C E & T | 21
clrscr();
printf("\nEnter the memory size -- ");
scanf("%d",&ms);
#include<stdio.h>
#include<conio.h>
main()
{
int i, j, k, f, pf=0, count=0, rs[25], m[10], n;
clrscr();
printf("\n Enter the length of reference string -- ");
scanf("%d",&n);
printf("\n Enter the reference string -- ");
for(i=0;i<n;i++)
scanf("%d",&rs[i]);
printf("\n Enter no. of frames -- ");
scanf("%d",&f);
for(i=0;i<f;i++)
m[i]=-1;
printf("\n The Page Replacement Process is -- \n");
for(i=0;i<n;i++)
{
for(k=0;k<f;k++)
{
if(m[k]==rs[i])
break;
}
if(k==f)
{
m[count++]=rs[i];
pf++;
}
for(j=0;j<f;j++)
printf("\t%d",m[j]);
if(k==f)
printf("\tPF No. %d",pf);
printf("\n");
if(count==f)
count=0;
}
printf("\n The number of Page Faults using FIFO are %d",pf);
getch();
}
R L A K S H M A N N A I K D e p t . o f I T , K U C E & T | 23
#include<stdio.h>
#include<conio.h>
main()
{
int i, j , k, min, rs[25], m[10], count[10], flag[25], n, f, pf=0, next=1;
clrscr();
printf("Enter the length of reference string -- ");
scanf("%d",&n);
printf("Enter the reference string -- ");
for(i=0;i<n;i++)
{
scanf("%d",&rs[i]);
flag[i]=0;
}
printf("Enter the number of frames -- ");
scanf("%d",&f);
for(i=0;i<f;i++)
{
count[i]=0;
m[i]=-1;
}
printf("\nThe Page Replacement process is -- \n");
for(i=0;i<n;i++)
{
for(j=0;j<f;j++)
{
if(m[j]==rs[i])
{
flag[i]=1;
count[j]=next;
next++;
}
R L A K S H M A N N A I K D e p t . o f I T , K U C E & T | 24
}
if(flag[i]==0)
{
if(i<f)
{
m[i]=rs[i];
count[i]=next;
next++;
}
else
{
min=0;
for(j=1;j<f;j++)
if(count[min] > count[j])
min=j;
m[min]=rs[i];
count[min]=next;
next++;
}
pf++;
}
for(j=0;j<f;j++)
printf("%d\t", m[j]);
if(flag[i]==0)
printf("PF No. -- %d" , pf);
printf("\n");
}
printf("\nThe number of page faults using LRU are %d",pf);
getch();
}
R L A K S H M A N N A I K D e p t . o f I T , K U C E & T | 25
#include<stdio.h>
#include<conio.h>
main()
{
int rs[50], i, j, k, m, f, cntr[20], a[20], min, pf=0;
clrscr();
printf("\nEnter number of page references -- ");
scanf("%d",&m);
printf("\nEnter the reference string -- ");
for(i=0;i<m;i++)
scanf("%d",&rs[i]);
printf("\nEnter the available no. of frames -- ");
scanf("%d",&f);
for(i=0;i<f;i++)
{
cntr[i]=0;
a[i]=-1;
}
Printf(“\nThe Page Replacement Process is – \n“);
for(i=0;i<m;i++)
{
for(j=0;j<f;j++)
if(rs[i]==a[j])
{
cntr[j]++;
break;
}
if(j==f)
{
min = 0;
for(k=1;k<f;k++)
if(cntr[k]<cntr[min])
min=k;
a[min]=rs[i];
cntr[min]=1;
pf++;
}
printf("\n");
for(j=0;j<f;j++)
printf("\t%d",a[j]);
if(j==f)
printf(“\tPF No. %d”,pf);
}
printf("\n\n Total number of page faults -- %d",pf);
getch();
}
R L A K S H M A N N A I K D e p t . o f I T , K U C E & T | 26
#include<stdio.h>
int n; main() { int seq[30],fr[5],pos[5],find,flag,max,i,j,m,k,t,s;
int count=1,pf=0,p=0; float pfr;
clrscr();
printf("Enter maximum limit of the sequence: ");
scanf("%d",&max);
printf("\nEnter the sequence: ");
for(i=0;i<max;i++) scanf("%d",&seq[i]);
printf("\nEnter no. of frames: ");
scanf("%d",&n); fr[0]=seq[0];
pf++;
printf("%d\t",fr[0]);
i=1;
while(count<n)
{
flag=1;
p++;
for(j=0;j<i;j++)
{
if(seq[i]==seq[j]) flag=0;
}
if(flag!=0)
{
fr[count]=seq[i];
printf("%d\t",fr[count]);
count++; pf++;
}
i++;
}
printf("\n");
for(i=p;i<max;i++)
{
flag=1;
for(j=0;j<n;j++)
{
if(seq[i]==fr[j])
R L A K S H M A N N A I K D e p t . o f I T , K U C E & T | 27
flag=0;
}
if(flag!=0)
{
for(j=0;j<n;j++)
{
m=fr[j];
for(k=i;k<max;k++)
{
if(seq[k]==m)
{
pos[j]=k;
break;
}
else
pos[j]=1;
}
}
for(k=0;k<n;k++)
{
if(pos[k]==1)
flag=0;
}
if(flag!=0)
s=findmax(pos);
if(flag==0)
{
for(k=0;k<n;k++)
{
if(pos[k]==1)
{
s=k; break;
}
}
}
fr[s]=seq[i];
for(k=0;k<n;k++)
printf("%d\t",fr[k]);
pf++; printf("\n");
}
}
pfr=(float)pf/(float)max;
printf("\nThe no. of page faults are %d",pf);
printf("\nPage fault rate %f",pfr);
getch(); }
int findmax(int a[])
{
int max,i,k=0;
max=a[0];
for(i=0;i<n;i++)
R L A K S H M A N N A I K D e p t . o f I T , K U C E & T | 28
{
if(max<a[i])
{
max=a[i]; k=i;
}
}
return k;
}
#include<stdio.h>
#include<conio.h>
struct fileTable
{
char name[20];
int sb, nob;
}
ft[30];
void main()
{
int i, j, n; char s[20]; clrscr();
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);
R L A K S H M A N N A I K D e p t . o f I T , K U C E & T | 29
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
{
}
getch();
return (0);
}
#include<stdio.h>
#include<conio.h>
struct fileTable
{
char name[20];
int nob; struct block *sb;
}
R L A K S H M A N N A I K D e p t . o f I T , K U C E & T | 30
ft[30];
struct block
{
int bno;
struct block *next;
};
void main()
{
int i, j, n;
char s[20];
struct block *temp;
clrscr();
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 no of blocks in file %d :",i+1);
scanf("%d",&ft[i].nob);
ft[i].sb=(struct block*)malloc(sizeof(struct block));
temp = ft[i].sb;
printf("Enter the blocks of the file :");
scanf("%d",&temp->bno);
temp->next=NULL;
for(j=1;j<ft[i].nob;j++)
{
temp->next = (struct block*)malloc(sizeof(struct block));
temp = temp->next;
scanf("%d",&temp->bno);
}
temp->next = NULL;
}
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 NO OF BLOCKS BLOCKS OCCUPIED");
printf("\n %s\t\t%d\t",ft[i].name,ft[i].nob);
temp=ft[i].sb;
for(j=0;j<ft[i].nob;j++)
{
printf("%d -> ",temp->bno);
temp = temp->next;
}
}
getch();
R L A K S H M A N N A I K D e p t . o f I T , K U C E & T | 31
return(0);
}
#include<stdio.h>
#include<conio.h>
struct fileTable
{
char name[20];
int nob, blocks[30];
}ft[30];
void main()
{
int i, j, n;
char s[20];
clrscr();
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 no of blocks in file %d :",i+1);
scanf("%d",&ft[i].nob);
printf("Enter the blocks of the file :");
for(j=0;j<ft[i].nob;j++)
scanf("%d",&ft[i].blocks[j]);
}
printf("\nEnter the file name to be searched -- ");
R L A K S H M A N N A I K D e p t . o f I T , K U C E & T | 32
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 NO OF BLOCKS BLOCKS OCCUPIED"); printf("\n
%s\t\t%d\t",ft[i].name,ft[i].nob);
for(j=0;j<ft[i].nob;j++)
printf("%d, ",ft[i].blocks[j]);
}
getch();
return(0);
}
#include<stdio.h>
#include<conio.h>
main()
{
int ms, bs, nob, ef,n, mp[10],tif=0;
int i,p=0;
clrscr();
printf("Enter the total memory available (in Bytes) -- ");
scanf("%d",&ms);
printf("Enter the block size (in Bytes) -- ");
scanf("%d", &bs);
nob=ms/bs;
ef=ms - nob*bs;
R L A K S H M A N N A I K D e p t . o f I T , K U C E & T | 33
#include<stdio.h>
#include<conio.h>
main()
{
R L A K S H M A N N A I K D e p t . o f I T , K U C E & T | 34
a. First-FIT
#include<stdio.h>
#include<conio.h>
#define max 25
void main()
{
int frag[max],b[max],f[max],i,j,nb,nf,temp;
static int bf[max],ff[max];
clrscr();
printf("\n\tMemory Management Scheme - First Fit");
printf("\nEnter the number of blocks:");
scanf("%d",&nb);
printf("Enter the number of files:");
scanf("%d",&nf);
printf("\nEnter the size of the blocks:-\n");
for(i=1;i<=nb;i++)
{
printf("Block %d:",i);
scanf("%d",&b[i]);
}
printf("Enter the size of the files :-\n");
for(i=1;i<=nf;i++)
{
printf("File %d:",i);
scanf("%d",&f[i]);
}
for(i=1;i<=nf;i++)
{
for(j=1;j<=nb;j++)
{
if(bf[j]!=1)
{
temp=b[j]-f[i];
if(temp>=0)
{
ff[i]=j;
break;
}
}
}
frag[i]=temp;
R L A K S H M A N N A I K D e p t . o f I T , K U C E & T | 36
bf[ff[i]]=1;
}
printf("\nFile_no:\tFile_size :\tBlock_no:\tBlock_size:\tFragement");
for(i=1;i<=nf;i++)
printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d",i,f[i],ff[i],b[ff[i]],frag[i]);
getch();
}
b. BEST-FIT
#include<stdio.h>
#include<conio.h>
#define max 25
void main()
{
int frag[max],b[max],f[max],i,j,nb,nf,temp,lowest=10000;
static int bf[max],ff[max];
clrscr();
printf("\n\tMemory Management Scheme - Best Fit");
printf("\nEnter the number of blocks:");
scanf("%d",&nb);
printf("Enter the number of files:");
scanf("%d",&nf);
printf("\nEnter the size of the blocks:-\n");
for(i=1;i<=nb;i++)
{
printf("Block %d:",i);
scanf("%d",&b[i]);
}
printf("Enter the size of the files :-\n");
for(i=1;i<=nf;i++)
R L A K S H M A N N A I K D e p t . o f I T , K U C E & T | 37
{
printf("File %d:",i);
scanf("%d",&f[i]);
}
for(i=1;i<=nf;i++)
{
for(j=1;j<=nb;j++)
{
if(bf[j]!=1)
{
temp=b[j]-f[i];
if(temp>=0)
if(lowest>temp)
{
ff[i]=j;
lowest=temp;
}
}
}
frag[i]=lowest;
bf[ff[i]]=1;
lowest=10000;
}
printf("\nFile No\tFile Size \tBlock No\tBlock Size\tFragment");
for(i=1;i<=nf && ff[i]!=0;i++)
printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d",i,f[i],ff[i],b[ff[i]],frag[i]);
getch();
}
c. WORST-FIT
R L A K S H M A N N A I K D e p t . o f I T , K U C E & T | 38
#include<stdio.h>
#include<conio.h>
#define max 25
void main()
{
int frag[max],b[max],f[max],i,j,nb,nf,temp,highest=0;
static int bf[max],ff[max];
clrscr();
printf("\n\tMemory Management Scheme - Worst Fit");
printf("\nEnter the number of blocks:");
scanf("%d",&nb);
printf("Enter the number of files:");
scanf("%d",&nf);
printf("\nEnter the size of the blocks:-\n");
for(i=1;i<=nb;i++)
{
printf("Block %d:",i);
scanf("%d",&b[i]);
}
printf("Enter the size of the files :-\n");
for(i=1;i<=nf;i++)
{
printf("File %d:",i);
scanf("%d",&f[i]);
}
for(i=1;i<=nf;i++)
{
for(j=1;j<=nb; j++)
{
if(bf[j]!=1) //if bf[j] is not allocated
{
temp=b[j]-f[i];
if(temp>=0)
if(highest<temp)
{
ff[i]=j;
highest=temp;
}
}
}
frag[i]=highest;
bf[ff[i]]=1;
highest=0;
}
printf("\nFile_no:\tFile_size :\tBlock_no:\tBlock_size:\tFragement");
R L A K S H M A N N A I K D e p t . o f I T , K U C E & T | 39
for(i=1;i<=nf;i++)
printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d",i,f[i],ff[i],b[ff[i]],frag[i]);
getch();
}
#include<stdio.h>
struct
{
char dname[10],fname[10][10];
int fcnt;
}dir;
void main()
{
int i,ch;
char f[30];
clrscr();
[Link] = 0;
printf("\nEnter name of directory -- ");
scanf("%s", [Link]);
while(1)
{
printf("\n\n1. Create File\t2. Delete File\t3. Search File \n4. Display Files\t5. Exit\nEnter
your choice -- ");
R L A K S H M A N N A I K D e p t . o f I T , K U C E & T | 40
scanf("%d",&ch);
switch(ch)
{
case 1: printf("\nEnter the name of the file -- ");
scanf("%s",[Link][[Link]]);
[Link]++;
break;
case 2: printf("\nEnter the name of the file -- ");
scanf("%s",f);
for(i=0;i<[Link];i++)
{
if(strcmp(f, [Link][i])==0)
{
printf("File %s is deleted ",f); strcpy([Link][i],[Link][[Link]-1]);
break;
}
}
if(i==[Link])
printf("File %s not found",f);
else
[Link]--;
break;
else
{
printf("\nThe Files are -- ");
for(i=0;i<[Link];i++)
printf("\t%s",[Link][i]);
R L A K S H M A N N A I K D e p t . o f I T , K U C E & T | 41
}
break;
default: exit(0);
}
}
getch();
}
#include<stdio.h>
struct
{
char dname[10],fname[10][10];
int fcnt;
}dir[10];
void main()
{
int i,ch,dcnt,k;
char f[30], d[30];
clrscr();
dcnt=0;
while(1)
{
printf("\n\n1. Create Directory\t2. Create File\t3. Delete File");
printf("\n4. Search File\t\t5. Display\t6. Exit\t Enter your choice -- ");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("\nEnter name of directory -- ");
R L A K S H M A N N A I K D e p t . o f I T , K U C E & T | 42
scanf("%s", dir[dcnt].dname);
dir[dcnt].fcnt=0;
dcnt++;
printf("Directory created");
break;
case 2: printf("\nEnter name of the directory -- ");
scanf("%s",d);
for(i=0;i<dcnt;i++)
if(strcmp(d,dir[i].dname)==0)
{
printf("Enter name of the file -- ");
scanf("%s",dir[i].fname[dir[i].fcnt]); dir[i].fcnt++;
printf("File created");
break;
}
if(i==dcnt)
printf("Directory %s not found",d);
break;
case 3: printf("\nEnter name of the directory -- ");
scanf("%s",d);
for(i=0;i<dcnt;i++)
{
if(strcmp(d,dir[i].dname)==0)
{
printf("Enter name of the file -- ");
scanf("%s",f); for(k=0;k<dir[i].fcnt;k++)
{
if(strcmp(f, dir[i].fname[k])==0)
{
printf("File %s is deleted ",f);
dir[i].fcnt--; strcpy(dir[i].fname[k],dir[i].fname[dir[i].fcnt]);
goto jmp;
}
}
printf("File %s not found",f);
goto jmp;
}
}
printf("Directory %s not found",d);
jmp : break;
{
printf("Enter the name of the file -- ");
scanf("%s",f);
for(k=0;k<dir[i].fcnt;k++)
{
if(strcmp(f, dir[i].fname[k])==0)
{
printf("File %s is found ",f);
goto jmp1;
}
}
printf("File %s not found",f);
goto jmp1;
}
}
printf("Directory %s not found",d);
jmp1: break;
case 5: if(dcnt==0)
printf("\nNo Directory's ");
else
{
printf("\nDirectory\tFiles");
for(i=0;i<dcnt;i++)
{
printf("\n%s\t\t",dir[i].dname);
for(k=0;k<dir[i].fcnt;k++)
printf("\t%s",dir[i].fname[k]);
}
}
break;
default:exit(0);
}
}
getch();
}
R L A K S H M A N N A I K D e p t . o f I T , K U C E & T | 44
#include<stdio.h>
#include<graphics.h>
struct tree_element
{
char name[20];
int x, y, ftype, lx, rx, nc, level;
struct tree_element *link[5];
};
typedef struct tree_element node;
void main()
{
int gd=DETECT,gm;
node *root;
root=NULL;
clrscr();
create(&root,0,"root",0,639,320);
clrscr();
initgraph(&gd,&gm,"c:\tc\BGI");
display(root);
getch();
closegraph();
}
create(node **root,int lev,char *dname,int lx,int rx,int x)
{
R L A K S H M A N N A I K D e p t . o f I T , K U C E & T | 45
int i, gap;
if(*root==NULL)
{
(*root)=(node *)malloc(sizeof(node));
printf("Enter name of dir/file(under %s) : ",dname);
fflush(stdin);
gets((*root)->name);
printf("enter 1 for Dir/2 for file :");
scanf("%d",&(*root)->ftype); (*root)->level=lev;
(*root)->y=50+lev*50; (*root)->x=x;
(*root)->lx=lx; (*root)->rx=rx;
for(i=0;i<5;i++)
(*root)->link[i]=NULL;
if((*root)->ftype==1)
{
printf("No of sub directories/files(for %s):",(*root)->name);
scanf("%d",&(*root)>nc);
if((*root)->nc==0)
gap=rx-lx;
else
gap=(rx-lx)/(*root)->nc;
for(i=0;i<(*root)->nc;i++)
create(&((*root)>link[i]),lev+1,(*root)>name,lx+gap*i,lx+gap*i+gap,
lx+gap*i+gap/2);
}
else
(*root)->nc=0;
}
}
display(node *root)
{
int i; settextstyle(2,0,4);
settextjustify(1,1);
setfillstyle(1,BLUE);
setcolor(14);
if(root !=NULL)
{
for(i=0;i<root->nc;i++)
line(root->x,root->y,root->link[i]->x,root->link[i]->y);
if(root->ftype==1)
bar3d(root->x-20,root->y-10,root->x+20,root>y+10,0,0);
else
fillellipse(root->x,root->y,20,20);
outtextxy(root->x,root->y,root->name);
for(i=0;i<root->nc;i++)
display(root->link[i]);
R L A K S H M A N N A I K D e p t . o f I T , K U C E & T | 46
}
}
INPUT
Enter Name of dir/file(under root): ROOT
Enter 1 for Dir/2 for File: 1
No of subdirectories/files(for ROOT): 2
Enter Name of dir/file(under ROOT): USER1
Enter 1 for Dir/2 for File: 1
No of subdirectories/files(for USER1): 1
Enter Name of dir/file(under USER1): SUBDIR1
Enter 1 for Dir/2 for File: 1
No of subdirectories/files(for SUBDIR1): 2
Enter Name of dir/file(under USER1):
JAVA Enter 1 for Dir/2 for File: 1
No of subdirectories/files(for JAVA): 0
Enter Name of dir/file(under SUBDIR1): VB
Enter 1 for Dir/2 for File: 1
No of subdirectories/files(for VB): 0
Enter Name of dir/file(under ROOT): USER2
Enter 1 for Dir/2 for File: 1
No of subdirectories/files(for USER2): 2
Enter Name of dir/file(under ROOT): A
Enter 1 for Dir/2 for File: 2
Enter Name of dir/file(under USER2): SUBDIR2
Enter 1 for Dir/2 for File: 1
No of subdirectories/files(for SUBDIR2): 2
Enter Name of dir/file(under SUBDIR2):
PPL Enter 1 for Dir/2 for File: 1
No of subdirectories/files(for PPL): 2
Enter Name of dir/file(under PPL): B
Enter 1 for Dir/2 for File: 2
Enter Name of dir/file(under PPL): C
Enter 1 for Dir/2 for File: 2
Enter Name of dir/file(under SUBDIR): AI
Enter 1 for Dir/2 for File: 1
No of subdirectories/files(for AI): 2
Enter Name of dir/file(under AI): D
Enter 1 for Dir/2 for File: 2
Enter Name of dir/file(under AI): E
Enter 1 for Dir/2 for File: 2
R L A K S H M A N N A I K D e p t . o f I T , K U C E & T | 47
OUTPUT
#include<stdio.h>
main()
{
int t[20], n, i, j, tohm[20], tot=0;
float avhm;
clrscr();
printf("enter the [Link] tracks: ");
scanf("%d",&n);
printf("enter the tracks to be traversed: ");
for(i=2;i<n+2;i++)
scanf("%d",&t[i]);
for(i=1;i<n+1;i++)
{
tohm[i]=t[i+1]-t[i];
if(tohm[i]<0)
tohm[i]=tohm[i]*(-1);
}
for(i=1;i<n+1;i++)
tot+=tohm[i];
avhm=(float)tot/n;
printf("Tracks traversed\tDifference between tracks\n");
for(i=1;i<n+1;i++)
printf("%d\t\t\t%d\n",t[i],tohm[i]);
printf("\nAverage header movements:%f",avhm);
getch();
return 0;
}
R L A K S H M A N N A I K D e p t . o f I T , K U C E & T | 48
INPUT
Enter [Link] tracks:9
Enter track position:55 58 60 70 18 90 150 160 184
OUTPUT
Tracks traversed Difference between tracks
55 45
58 3
60 2
70 10
18 52
90 72
150 60
160 10
184 24
#include<stdio.h>
main()
{
int t[20], d[20], h, i, j, n, temp, k, atr[20], tot, p, sum=0;
clrscr();
printf("enter the no of tracks to be traveresed");
scanf("%d'",&n);
printf("enter the position of head");
scanf("%d",&h); t[0]=0;t[1]=h;
printf("enter the tracks");
for(i=2;i<n+2;i++)
scanf("%d",&t[i]);
for(i=0;i<n+2;i++)
{
for(j=0;j<(n+2)-i-1;j++)
{
if(t[j]>t[j+1])
{
temp=t[j];
t[j]=t[j+1];
t[j+1]=temp;
}
}
}
R L A K S H M A N N A I K D e p t . o f I T , K U C E & T | 49
for(i=0;i<n+2;i++)
if(t[i]==h)
j=i;k=i;
p=0;
while(t[j]!=0)
{
atr[p]=t[j];
j--;
p++;
}
atr[p]=t[j];
for(p=k+1;p<n+2;p++,k++)
atr[p]=t[k+1];
for(j=0;j<n+1;j++)
{
if(atr[j]>atr[j+1])
d[j]=atr[j]-atr[j+1];
else
d[j]=atr[j+1]-atr[j];
sum+=d[j];
}
printf("\nAverage header movements:%f",(float)sum/n);
getch();
}
INPUT
Enter [Link] tracks:9
Enter track position:55 58 60 70 18 90 150 160 184
OUTPUT
Tracks traversed Difference between tracks
150 50
33
160 10
184 24
90 94
70 20
60 10
58 2
55 3
18 37
#include<stdio.h>
main()
{
int t[20], d[20], h, i, j, n, temp, k, atr[20], tot, p, sum=0;
clrscr();
printf("enter the no of tracks to be traveresed");
scanf("%d'",&n);
printf("enter the position of head");
scanf("%d",&h);
t[0]=0;t[1]=h;
printf("enter total tracks");
scanf("%d",&tot);
t[2]=tot-1;
printf("enter the tracks");
for(i=3;i<=n+2;i++)
scanf("%d",&t[i]);
for(i=0;i<=n+2;i++)
for(j=0;j<=(n+2)-i-1;j++)
if(t[j]>t[j+1])
{
temp=t[j];
t[j]=t[j+1];
t[j+1]=temp;
}
for(i=0;i<=n+2;i++)
if(t[i]==h)
j=i;
break;
p=0;
while(t[j]!=tot-1)
{
atr[p]=t[j];
j++;
p++;
}
atr[p]=t[j];
p++;
i=0;
while(p!=(n+3) && t[i]!=t[h])
{
atr[p]=t[i];
i++;
p++;
}
for(j=0;j<n+2;j++)
{
R L A K S H M A N N A I K D e p t . o f I T , K U C E & T | 51
if(atr[j]>atr[j+1])
d[j]=atr[j]-atr[j+1];
else
d[j]=atr[j+1]-atr[j];
sum+=d[j];
}
printf("total header movements%d",sum);
printf("avg is %f",(float)sum/n);
getch();
}
INPUT
Enter the track position : 55 58 60 70 18 90 150 160 184
Enter starting position : 100
OUTPUT
Tracks traversed Difference Between tracks
150 50
160 10
184 24
18 240
55 37
58 3
60 2
70 10
90 20