Saturday, July 27, 2013

Operating System Lab Programs



Ex NO : 2.1

//To execute the system calls of execlp,exit,wait
#include
#inlcude
#include
int main()
{
int a;
a=fork();
if(a==0)
{
excelp(“\bin\ls”,”ls”,NULL);
exit(-1);
}
elseif (a<0 p="">
{
printf(“Chilp process failed”);
exit(0);
}
else
{
printf(“Process completed”);
}
wait(NULL);
return 0;
}
Ex No :2.2
//PROGRAM FOR SYSTEM CALLS OF UNIX OPERATING SYSTEMS //(OPENDIR, READDIR, CLOSEDIR, ETC)

#include
#include
struct dirent *dptr;
int main(int argc, char *argv[])
{
char buff[100];
DIR *dirp;
printf(“\n\n ENTER DIRECTORY NAME”);
scanf(“%s”, buff);
if((dirp=opendir(buff))==NULL)
{
printf(“The given directory does not exist”);
exit(1);
}
while(dptr=readdir(dirp))
{
printf(“%s\n”,dptr->d_name);
}
closedir(dirp);
}


SAMPLE OUTPUT:

Cc opdir.c
./a.out

ENTER THE DIRECTORY NAME  UNIT

CHAP1.C
CHAP2.C

Ex no : 2.3
//Tree structure child processes

 









#include
#include
#include
#include
main()
{
int mr,mrl,mrr,ml,mlr,mll;
printf("\nmain(m): %d",getpid());
mr=fork();
ml=fork();
if((mr==0)&&(ml!=0))
{
        mrr=fork();
        mrl=fork();
                if((mrr==0)&&(mrl!=0))
                {
                printf("\nmrr : %d\n",getpid());
                printf("\nmr(parent(mrr)) : %d\n",getppid());
                }
                if((mrl==0)&&(mrr!=0))
                {
                printf("\nmrl : %d\n",getpid());
                printf("\nmr(parent(mrl)) : %d\n",getppid());
                }
}
if((ml==0)&&(mr!=0))
{
        mlr=fork();
        mll=fork();
                if((mlr==0)&&(mll!=0))
                {
                printf("\nmlr : %d\n",getpid());
                printf("\nml(parent(mlr)) : %d\n",getppid());
                }
                if((mll==0)&&(mlr!=0))
                {
                printf("\nmll : %d\n",getpid());
                printf("\nml(parent(mll)) : %d\n",getppid());
                }
}
}
Output

main(m): 8787
ml(parent(mll)) : 8791                                                   
mr(parent(mrl)) : 1         
ml(parent(mlr)) : 8791                                                  
mr(parent(mrl)) : 1
mll : 8794                                            
mlr : 8792            
mrl : 8795                                            
mrr : 8790




// STAT command
#include
#include
#include
#include

int main(int argc, char **argv)
{
    if(argc != 2)   
        return 1;

    struct stat fileStat;
    if(stat(argv[1],&fileStat) < 0)   
        return 1;

    printf("Information for %s\n",argv[1]);
    printf("---------------------------\n");
    printf("File Size: \t\t%d bytes\n",fileStat.st_size);
    printf("Number of Links: \t%d\n",fileStat.st_nlink);
    printf("File inode: \t\t%d\n",fileStat.st_ino);

    
    return 0;
}


Cc filename.c
./a.out filename.c
stat structure
The struct stat structure, also defined in , includes at least the following members:
st_dev 
Identifier of device containing file
st_ino 
st_mode 
Protection mode. See also Unix permissions.
st_nlink 
st_uid 
User identifier of owner
st_gid 
st_rdev 
Device identifier (if special file)
st_size 
Total file size, in bytes
st_atime 
Time of last access
st_mtime 
Time of last modification
st_ctime 
Time of last status change
st_blksize 
Block size for filesystem I/O
st_blocks 
Number of blocks allocated

//C Program for File Operations (READ, WRITE, OPEN)



#include
#include
#include
#include
#include
#include
struct stud
{
char name[20],id[10],dept[10];
int year;
} s[10];
int main()
{
int x,y,z;
char dump[1];
char newline[2]={“\n”};
char sep[5]=””;
char l1[]={“enter the student name:”},l2={“student id:”},l3={“dept name:”}, l4={“year:”};
x=open(“psrec.txt”,O_RDWR|O_CREAT|O_TRUNC,S_IRWXU);
int i;
for(i=0;i<5 i="" p="">
{
write(1,l1,strlen(l1));
read(0,s[i].name,40);
write(1,l2,strlen(l2));
read(0,s[i].id,40);
write(1,l3,strlen(l3));
read(0,s[i].dept,40);
write(1,l4,strlen(l4));
read(0,s[i].year,1);
read(0,dump,1);
}
for(i=0;i<5 i="" p="">
{
wirte(x,s[i].name,strlen(s[i].name));
write(x,sep,4);
wirte(x,s[i].id,strlen(s[i].id));
write(x,sep,4);
wirte(x,s[i].dept,strlen(s[i].dept));
write(x,sep,4);
wirte(x,s[i].year,strlen(s[i].year));
write(x,newline,4);
}
return 0;
}
/*To Execute LS command
#include
#include
#include
#include
#include
main(int arg, char *argv[])
{
DIR *dir;
struct dirent *ent;
struct stat statbuf;
dir=opendir(“.”);
while((ent=readdir(dir))!=NULL)
{
printf(“\n”);
if(strcmp(argv[1],”-1”));
}
stat(ent->d_name,&statbuf);
if(statbuf.st_mode,s_iread)
printf(“r”);
if(statbuf.st_mode,s_iwrite)
printf(“w”);
if(statbuf.st_mode,s_iexec)
printf(“x”);
printf(“\t%s, \t%d  \t %s ”,ent->d_name, statbuf.st_ctime,ctime(&statbuf.st_ctime));
}
}
}






// To Execute Grep Command with option

#include
#include
#include
#include
#include
#include
#include
main(int argc, char *argv[])
{
int i,k,size,l=0,c=0;
char buf,line[100];
k=open(argv[3],O_RDONLY);
size=read(k,&buf,l);
while(size!=0)
{
for(i=0;buf!=’\n’;i++)
{
line[i]=buf;
size=read(k,&buf,l);
}
l++;
line[i]=’\0’;
if(strstr(line,srgv[2])!=”\0”)
{
  if(strcmp(argv[l],”-n”)==0)
  {
   printf(“%d%s\n”,l,line);
  }
  else
  c++;
}
size=read(k,&buf,l);
}
if(strcmp(argv[l],”-c”)==0)
{
printf(“%d\n”,c);
}
close(k);
}






// To Execute Grep Command without option

#include
#include
#include
#include
#include
#include
#include
main(int argc, char *argv[])
{
int i,k,size,l=0,c=0;
char b,a[100];
k=open(argv[3],O_RDONLY);
size=read(k,&buf,l);
while(size!=0)
{
if(b!=’\n’)
{
a[c++]=b;
}
else
{
a[c]=’\0’;
if(strstr(argv[l]!=null)
printf(“%s\n”,a);
}
size=read(k,&b,l);
}
}
//FCFS
#include
#include
#include
void main()
{
 int n,b[10],w[10],i,j,h;
 int stime[10];
 float avg=0;
 clrscr();
 printf("\n\tJOB SCHEDULING ALGORITHM[FCFS]");
 printf("\n\t************************************************* ****\n");
 printf("\nEnter howmany jobs:");
 scanf("%d",&n);
 printf("\nEnter burst time for corresponding job....\n");
 for(i=0;i
 {
  printf("\nProcess %d:",i+1);
  scanf("%d",&b[i]);
 }
 w[0]=0;
 printf("\nprocess 1 waiting time is 0");
 for(i=1;i
 {
  w[i]=b[i-1]+w[i-1];
  printf("\nProcess %d waiting time: %d",i+1,w[i]);
  avg+=w[i];
 }
printf("\ntotal waiting time:%f",avg);
 printf("\n\nthe average waiting time is:%f\n",avg/n);
 printf("\nGantt  Chart\n***************************************\n\n\t");
 h=22;
 for(i=0;i
 {
  printf("%d",b[i]);
  for(j=1;j<=b[i];j++)
   printf("%c",h);
 }
 printf("\n\t");
 for(i=0;i
 {
  printf("%d",w[i]);
  for(j=1;j<=w[i];j++)
   printf("%c",h);
  delay(1000);
 }
getch();
}

#include
#include
#include

void main()
{
 int n,b[10],w[10],i,j,h,t,tt;
 int stime[10],a[10];
 float avg=0;
 clrscr();
 printf("\n\tJOB SCHEDULING ALGORITHM[SJF]");
 printf("\n\t*****************************************************\n");
 printf("\nEnter howmany jobs:");
 scanf("%d",&n);
 printf("\nEnter burst time for corresponding job....\n");
 for(i=1;i<=n;i++)
 {
  printf("\nProcess %d:",i);
  scanf("%d",&b[i]);  a[i]=i;
 }
                for(i=1;i<=n;i++)
                                   for(j=i;j<=n;j++)
                                      if(b[i]>b[j])
                                      {
                                      t=b[i]; tt=a[i];
                                      b[i]=b[j];  a[i]=a[j];
                                      b[j]=t;     a[j]=tt;
                                      }

 w[1]=0;


 printf("\nprocess %d waiting time is 0",a[1]);

 for(i=2;i<=n;i++)
 {
  w[i]=b[i-1]+w[i-1];
  printf("\nProcess %d waiting time: %d",a[i],w[i]);
  avg+=w[i];
 }

 printf("\ntotal waiting time:%f",avg);
 printf("\n\nthe average waiting time is:%f\n",avg/n);

 printf("\nGaunt Chart\n***************************************\n\n\t");

 h=22;

 for(i=1;i<=n;i++)
 {
  printf("%d",b[i]);
  for(j=1;j<=b[i];j++)
   printf("%c",h);
 }
 printf("\n\t");

 for(i=1;i<=n;i++)
 {
  printf("%d",w[i]);
  for(j=1;j<=w[i];j++)
   printf("%c",h);
  delay(1000);
 }

getch();
}
SAMPLE INPUT AND OUTPUT:

Enter howmany jobs:3

Enter burst time for corresponding job....
Process 1:5
Process 2:2
Process 3:3
process 2 waiting time is 0
Process 3 waiting time: 2
Process 1 waiting time: 5
total waiting time:7.000000

the average waiting time is:2.333333
Gaunt Chart
***************************************

        2▬▬3▬▬▬5▬▬▬▬▬
        02▬▬5▬▬▬▬▬


//priority scheduling
#include
#include
#include

void main()
{
 int n,b[10],w[10],i,j,h,t,tt;
 int stime[10],a[10],p[10];
 float avg=0;
 clrscr();
 printf("\n\tPRIORITY SCHEDULING ALGORITHM");
 printf("\n\t*****************************************************\n");
 printf("\nEnter howmany jobs:");
 scanf("%d",&n);
 printf("\nEnter burst time & priority for corresponding job....\n");
 for(i=1;i<=n;i++)
 {
  printf("\nProcess %d:",i);
  scanf("%d %d",&b[i],&p[i]);  a[i]=i;
 }
                for(i=1;i<=n;i++)
                                   for(j=i;j<=n;j++)
                                      if(p[i]
                                      {
                                      t=b[i]; tt=a[i];
                                      b[i]=b[j]; a[i]=a[j];
                                      b[j]=t;     a[j]=tt;
                                      }

 w[1]=0;


 printf("\nprocess %d waiting time is 0",a[1]);
 for(i=2;i<=n;i++)
 {
  w[i]=b[i-1]+w[i-1];
  printf("\nProcess %d waiting time: %d",a[i],w[i]);
  avg+=w[i];
 }
 printf("\ntotal waiting time:%f",avg);
 printf("\n\nthe average waiting time is:%f\n",avg/n);
 printf("\nGaunt Chart\n***************************************\n\n\t");
 h=22;

 for(i=1;i<=n;i++)
 {
  printf("%d",b[i]);
  for(j=1;j<=b[i];j++)
   printf("%c",h);
 }
 printf("\n\t");
 for(i=1;i<=n;i++)
 {
  printf("%d",w[i]);
  for(j=1;j<=w[i];j++)
   printf("%c",h);
  delay(1000);
 }

getch();
}

SAMPLE INPUT AND OUTPUT:

Enter howmany jobs:3

Enter burst time & priority for corresponding job....
Process 1:5 2
Process 2:7 1
Process 3:6 3
        PRIORITY SCHEDULING ALGORITHM
        *****************************************************

Enter howmany jobs:3
Enter burst time & priority for corresponding job....
Process 1:5 2
Process 2:7 1
Process 3:6 3
process 3 waiting time is 0
Process 1 waiting time: 6
Process 2 waiting time: 11
total waiting time:17.000000

the average waiting time is:5.666667
Gaunt Chart
***************************************

        6▬▬▬▬▬▬5▬▬▬▬▬7▬▬▬▬▬▬▬
        06▬▬▬▬▬▬11▬▬▬▬▬▬▬▬▬▬▬


//Round Robin
#include
#include

int z[10],b[10],n,m[50],r,q,e=0,avg=0,i,j;
float f;

main()
{
 clrscr();
 printf("\n\tJOB SCHEDULING ALGORITHM[RR]");
 printf("\n\t*******************************************************\n");
 printf("\nEnter how many jobs:");
 scanf("%d",&n);
 printf("\nEnter burst time for corresponding job...\n");
 for(i=1;i<=n;i++)
 {
  printf("\nProcess %d: ",i);
  scanf("%d",&b[i]); z[i]=b[i];
 }

 printf("\nENTER THE TIME SLICE VALUE:");
 scanf("%d",&q);

 rr();

 average();

 getch();
 return 0;
}




rr()
{
 int max=0;
 max=b[1];
                for(j=1;j<=n;j++)
                   if(max<=b[j])
                max=b[j];

 if((max%q)==0)
   r=(max/q);
 else
  r=(max/q)+1;
   for(i=1;i<=r;i++)
   {
      printf("\nround %d",i);
      for(j=1;j<=n;j++)
                {
                  if(b[j]>0)
                    {
                     b[j]=b[j]-q;

                     if(b[j]<=0)
                                {
                                b[j]=0;
                     printf("\nprocess %d is completed",j);
                                }
                     else
                     printf("\nprocess %d remaining time is %d",j,b[j]);
                    }
                 }
                 delay(1000);
   }
   return 0;
}





average()
{
 for(i=1;i<=n;i++)
   {
    e=0;
    for(j=1;j<=r;j++)
     {
                if(z[i]!=0)
                {
                  if(z[i]>=q)
                  {
                  m[i+e]=q;  z[i]-=q;
                  }
                  else
                  {
                  m[i+e]=z[i];  z[i]=0;
                  }
                }
                else
                  m[i+e]=0;
       e=e+n;
     }
   }
  for(i=2;i<=n;i++)
     for(j=1;j<=i-1;j++)
      avg=avg+m[j];
  for(i=n+1;i<=r*n;i++)
  {
    if(m[i]!=0)
    {
      for(j=i-(n-1);j<=i-1;j++)
      avg=m[j]+avg;
    }
  }
 f=avg/n;
 printf("\nTOTAL WATING:%d",avg);
 printf("\n\nAVERAGE WAITING TIME:%f\n",f);
                for(i=1;i<=r*n;i++)
                 { if(m[i]!=0)
                   if(i%n==0){
                   printf("P%d",(i%n)+(n));      }


                   else
                   printf("P%d",(i%n));
                   for(j=1;j<=m[i];j++)
                                printf("%c",22);
                 }
printf("\n");
 getch();
 return 0;
}

SAMPLE INPUT AND OUTPUT:

Enter how many jobs:4
Enter burst time for corresponding job...
Process 1: 7
Process 2: 5
Process 3: 4
Process 4: 2
ENTER THE TIME SLICE VALUE:2
round 1
process 1 remaining time is 5
process 2 remaining time is 3
process 3 remaining time is 2
process 4 is completed
round 2
process 1 remaining time is 3
process 2 remaining time is 1
process 3 is completed
round 3
process 1 remaining time is 1
process 2 is completed
round 4
process 1 is completed
TOTAL WATING:39
AVERAGE WAITING TIME:9.000000
P1▬▬P2▬▬P3▬▬P4▬▬P1▬▬P2▬▬P3▬▬P1▬▬P2▬P1▬

//SIMULATION OF PIPE COMMAND
#include
#include
#include
main()
{
 int fd1[2],fd2[2],fd3[2];
 int i,n,m,p;
 pid_t x,y,z;
 pipe(fd1);
 pipe(fd2);
 pipe(fd3);
 x=fork();
 y=fork();
 z=fork();
if((x==0)&&(y!=0)&&(z!=0))
{
 printf("\nEnter a number to check:");
 scanf("%d",&n);
 close(fd1[0]);
 write(fd1[1],&n,sizeof(n));
 if(n%2==0)
   printf("\nEven number");
 else
   printf("\nOdd number");
}
if((y==0)&&(x!=0)&&(z!=0))
{
 read(fd1[0],&m,sizeof(m));
 close(fd1[1]);
 close(fd2[0]);
 write(fd2[1],&m,sizeof(m));
 printf("\nThe gn no is %d",m);
if(m==1)
 printf("\n1 is neither prime nor composite");
if(m==2)
 printf("\n2 is the only even prime number");
else
{
 for(i=2;i<=m/2;i++)
  if(m%i==0)
  {
   printf("\n%d is not a prime number",m);
   goto stop;
  }
 printf("\n%d is a prime number",m);
stop:
 printf("\nexit y");
 }
}
if((z==0)&&(x!=0)&&(y!=0))
{
 read(fd2[0],&p,sizeof(p));
 close(fd2[1]);
 write(fd3[1],&p,sizeof(p));
 close(fd3[0]);
 for(i=0;i
 {
  printf("\nHai");
 }
}
}
Ex.no:                        
                                    IPC USING SHARED MEMORY

#include
#include
#include
main()
{
int sh,sh2,sh3,p,q,s;
sh=shmget(2845,30,0777|IPC_CREAT|IPC_EXCL);
if(sh==-1)
        perror("can't create");

q=shmat(sh,0,0);
printf("Enter the value of Q \n");
scanf("%d",&q);
printf("%d",q);

sh2=shmget(2522,30,0666|IPC_CREAT|IPC_EXCL);
if(sh2==-1)
        perror("can't create");
p=shmat(sh2,0,0);
printf("Enter the value of P\n");
scanf("%d",&p);
printf("%d",p);

sh3=shmget(2646,32,0705|IPC_CREAT|IPC_EXCL);
if(sh3==-1)
        perror("can't create");
s=shmat(sh3,0,0);
printf("VALUE OF Q AND P\n");
printf("addition is %d",(q+p));

shmdt(sh);
shmdt(sh2);
shmdt(sh3);

}
//consumer.c
#include
#include
#include
#include
#include
#include
#include
#include

void wait(int id,int num)
{
struct sembuf *op=(struct sembuf *)malloc(sizeof(struct sembuf));
op->sem_num=num;
op->sem_op=-1;
semop(id,op,1);
}
void signal(int id,int num)
{
struct sembuf *op=(struct sembuf *)malloc(sizeof(struct sembuf));
op->sem_num=num;
op->sem_op=1;
semop(id,op,1);
}

int main()
{
int *b,s;
int id,i,n,sid,j,k;

union semun
{
int val;
unsigned short array[3];
struct semid_ds *buf;
struct seminfo *_buf;
}arg;

 id=shmget((key_t)7855,(50*sizeof(int)),0777);
 b=(int *)shmat(id,NULL,0777);
 sid=semget((key_t)5300,3,0766);
 n=*(b+0);
 semctl(sid,0,GETALL,&arg);
 printf("consume the strings\n");
 for(i=2;i<=n+1;i++)
 {

         wait(sid,1);
         wait(sid,0);
         s=(*(b+i))*(*(b+i));
        printf("%d",s);


         signal(sid,0);
         signal(sid,2);
 }
 *(b+1)=-99;
 shmdt(b);
}


// producer.c
#include
#include
#include
#include
#include
#include
#include
#include

int main()
{
        int id,sid,n,i,j;
        void wait(int id,int num);
        void signal(int id,int num);
        int r,*b;

  union semun {
          int val;
          struct semid_ds *buf;
          unsigned short array[3];
          struct seminfo *__buf;
  }arg;
printf("\nenter the no.of strings\n");
scanf("%d",&n);
arg.array[0]=1;
arg.array[1]=0;
arg.array[2]=50;

id=shmget((key_t)7855,(50*(sizeof(int))),0777|IPC_CREAT|IPC_EXCL);
b=(int *)shmat(id,NULL,0777);
if(id==-1)
{
perror("\nshm cannot create\n");
}
sid=semget((key_t)5300,3,0766|IPC_CREAT|IPC_EXCL);
semctl(sid,0,SETALL,&arg);
*(b+0)=n;
*(b+1)=0;

semctl(sid,0,GETALL,arg);
printf("\nproducer\n");

for(i=2;i<=n+1;i++)
{
  wait(sid,2);
  wait(sid,0);
        printf("enter the number\n");
        scanf("%d",&r);
*(b+i)=r;

 signal(sid,0);
 signal(sid,1);

}

while(*(b+1)==0);
if(*(b+1)=-99)
{
  shmdt(b);
  shmctl(id,IPC_RMID,NULL);
  semctl(sid,0,IPC_RMID,NULL);

}


}

void signal(int id,int num)
{
struct sembuf *op=(struct sembuf *)malloc(sizeof(struct sembuf));
op->sem_num=num;
op->sem_op=1;
semop(id,op,1);
}
void wait(int id,int num)
{
struct sembuf *op=(struct sembuf *)malloc(sizeof(struct sembuf));
op->sem_num=num;
op->sem_op=-1;
semop(id,op,1);
}

//BEST FIT MEMORY MANAGEMENT ALGORITHM IMPLEMENTATION
#include
#include
#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++) {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:\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();
}
//FIRST FIT MEMORY MANAGEMENT ALGORITHM IMPLEMENTATION

#include
//#include
#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;
  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();
}

//WORST FIT MEMORY MANAGEMENT ALGORITHM IMPLEMENTATION

#include
#include
#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     {
     ff[i]=j;
     highest=temp;
    }
   }
  }
  frag[i]=highest;
  bf[ff[i]]=1;
  highest=0;
 }
 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();
}
#include
int main()
{
int ref[25],a[10],index[10];
int i,j,k,flag,n,m,p,pf,l;
char ch;
printf("entert the no of reference string:\n");
scanf("%d",&n);
printf("enter the ref string:\n");
for(i=0;i
{
scanf("%d",&ref[i]);
}
do
{
printf("enter the no of frames:\n");
scanf("%d",&m);
printf("\nFIFO ALGM\n");
printf("*************");
j=0;
pf=0;
for(i=0;i
a[i]=-1;
for(i=0;i
{
p=ref[i];
flag=0;
for(k=0;k
if(a[k]==p)
flag=1;
if(flag!=1)
{
a[j]=p;
j=(j+1)%m;
pf++;
}
for(l=0;l
{
printf("%d\t",a[l]);
}
printf("\n");
}
printf("no of page faults:%d\n",pf);
printf("\nOPTIMAL ALGM\n");
printf("****************");
                j=0;
                pf=0;
                for(i=0;i
                        a[i]=-1;
                for(i=0;i
                {
                        p=ref[i];
                        flag=0;
                        for(k=0;k
                                if(a[k]==p)
                                {
                                        flag=1;
                                        break;
                                }
                                else if(a[k]==-1)
                                {
                                       a[k]=p;
                                       pf++;
                                      flag=1;
                                       break;
                                 }
                       if(flag==0)
                       {

                                 pf++;
                                 for(k=0;k
                                         index[k]=get_index2(a[k],ref,n,i);
                                 for(k=0;k
                                        if(index[k]==-99)
                                        {
                                                a[k]=p;
                                                flag=1;
                                                break;
                                        }
                                 if(flag==0)
                                 {
                                         j=get_max(index,m);
                                         for(k=0;k
                                               if(a[k]==ref[j])
                                                       a[k]=p;
                                 }
                        }
for(l=0;l
{
printf("%d\t",a[l]);
}
printf("\n");
}
printf("no of page faults:%d\n",pf);
printf("\nLRU ALGM\n");
printf("************");
j=0;
pf=0;
for(i=0;i
a[i]=-1;
for(i=0;i
{
p=ref[i];
flag=0;
for(k=0;k
if(a[k]==p)
{
flag=1;
break;
}
else if(a[k]==-1)
{
a[k]=p;
pf++;
flag=1;
break;
}
if(flag!=1)
{
pf++;
for(k=0;k
index[k]=get_index1(a[k],ref,i);
j=get_min(index,m);
for(k=0;k
if(a[k]==ref[j])
a[k]=p;
}
for(l=0;l
{
printf("%d\t",a[l]);
}
printf("\n");
}
printf("no of page faults %d\n",pf);
printf("\n\nwish to print the no of pf for another frame\n");
printf("\ngive[yes='y' or no='n']\t");
scanf("%s",&ch);
}while(ch=='y');

}
int get_index1(int p,int ref[],int c)
{
int i;
for(i=c-1;i>=0;i--)
{
if(ref[i]==p)
return i;
}
return 99;
}
int get_index2(int p,int ref[],int c,int n)
{
int i;
for(i=c+1;i
{
if(ref[i]==p)
return i;
}
return -99;
}
int get_min(int index[],int m)
{
int i,min=99;
for(i=0;i
if(min>index[i])
min=index[i];
return min;
}
int get_max(int index[],int m)
{
int i,max=-99;
for(i=0;i
if(max
max=index[i];
return max;
}

#include
#include
#include
#include

struct node
{
        char info[15];
        struct node *next;
}*root;
struct data
{
        char str[15];
        struct data *next1;
}*root1;
void create()
{
        struct node *p,*prev;
        int no,size,i;
        char val[15];
        root=p=NULL;
        size=sizeof(struct node);
        printf("\n Enter the limit to create a empty list");
        scanf("%d",&no);
        for(i=0;i
        {
                p=(struct node *)malloc(size);
                strcpy(p->info,"0_0_0");
                p->next=NULL;
                if(root==NULL)
                        root=p;
                else
                        prev->next=p;
                prev=p;
        }
}

void showFree()
{
        struct node *v=root;
        printf("\n Root->");
        while(v!=NULL)
        {
                printf("%s",v->info);
                printf("->");
                v=v->next;
        }
        printf("NULL");
}

void insert()
{
        void deleNode();
        struct data *p,*prev=root1;
        char val[15];
        int size;
        printf("\n Enter the data:");
        scanf("%s",val);
        size=sizeof(struct data);
        p=(struct data *)malloc(size);
        strcpy(p->str,val);
        p->next1=NULL;
        if(prev!=NULL)
        {
                while(prev->next1!=NULL)
                        prev=prev->next1;
                p->next1=prev->next1;
                prev->next1=p;
        }
        else
                root1=p;
        deleNode();
}

void deleNode()
{
        struct node *prev=root;
        root=root->next;
}

void showAlloc()
{
        struct data *v=root1;
        printf("\n Root->");
        while(v!=NULL)
        {
                printf("%s",v->str);
                printf("->");
                v=v->next1;
        }
        printf("NULL");
}

int main()
{
        int choice;
//      void create();
//      void insert();
//      void showFree();
//      void showAlloc();
        while(1)
        {
        printf("\n 1. Create a Empty List");
        printf("\n 2. MemoryAllocation for a Process");
        printf("\n 3. Show Free Spaces");
        printf("\n 4. Show Allocated Spaces");
        printf("\n 5. Exit");
        printf("\n Enter your Choice:");
        scanf("%d",&choice);
        switch(choice)
        {
        case 1:
                create();
                break;
        case 2:
                insert();
                break;
        case 3:
                showFree();
                break;
        case 4:
                showAlloc();
                break;
        case 5:
                exit(1);
        }
}


}

//FIFO ALGORITHM
#include
int main()
{
int ref[25],a[10],index[10];
int i,j,k,flag,n,m,p,pf,l;
char ch;
printf("entert the no of reference string:\n");
scanf("%d",&n);
printf("enter the ref string:\n");
for(i=0;i
{
scanf("%d",&ref[i]);
}
printf("enter the no of frames:\n");
scanf("%d",&m);
printf("\nFIFO ALGM\n");
printf("*************\n");
j=0;
pf=0;
for(i=0;i
a[i]=-1;
for(i=0;i
{
p=ref[i];
flag=0;
for(k=0;k
if(a[k]==p)
flag=1;
if(flag!=1)
{
a[j]=p;
j=(j+1)%m;
pf++;
}
for(l=0;l
{
printf("%d\t",a[l]);
}
printf("\n");
}

printf("no of page faults:%d\n",pf);
}
//LRU Algorithm
#include
int get_index1(int p,int ref[],int c)
{
int i;
for(i=c-1;i>=0;i--)
{
if(ref[i]==p)
return i;
}
return 99;
}

int get_min(int index[],int m)
{
int i,min=99;
for(i=0;i
if(min>index[i])
min=index[i];
return min;
}
int main()
{
int ref[25],a[10],index[10];
int i,j,k,flag,n,m,p,pf,l;
char ch;
printf("entert the no of reference string:\n");
scanf("%d",&n);
printf("enter the ref string:\n");
for(i=0;i
{
scanf("%d",&ref[i]);
}
printf("Enter the no. of frames\n");
scanf("%d",&m);
printf("\nLRU ALGM\n");
printf("************\n");
j=0;
pf=0;
        for(i=0;i
        a[i]=-1;
        for(i=0;i
        {
                p=ref[i];
                flag=0;
                for(k=0;k
                if(a[k]==p)
                {
                flag=1;
                break;
                }
        else if(a[k]==-1)
        {
        a[k]=p;
        pf++;
        flag=1;
        break;
        }
                if(flag!=1)
                {
                pf++;
                for(k=0;k
                index[k]=get_index1(a[k],ref,i);
                j=get_min(index,m);
                for(k=0;k
                if(a[k]==ref[j])
                a[k]=p;
                }
for(l=0;l
{
printf("%d\t",a[l]);
}
printf("\n");
}
printf("no of page faults %d\n",pf);
}
//OPTIMAL ALGORITHM
#include
int get_index2(int p,int ref[],int n,int c)
{
        int i;
        for(i=c+1;i
        {
                if(ref[i]==p)
                        return i;
        }
        return -99;
}
int get_max(int index[],int m)
{
        int i,max=-99;
        for(i=0;i
                if(max
                        max=index[i];
        return max;
}
int main()
{
        int ref[25],a[10],index[10];
        int i,j,k,flag,n,m,p,pf,l;
        char ch;
        printf("Enter the no. of reference string\n");
        scanf("%d",&n);
        printf("Enter the ref. string\n");
        for(i=0;i
        {
                scanf("%d",&ref[i]);
        }
                printf("Enter the no. of frames\n");
                scanf("%d",&m);
                printf("\nOPTIMAL ALGORITHM\n");
                printf("==================\n");
                j=0;
                pf=0;
                for(i=0;i
                        a[i]=-1;
                for(i=0;i
                {
                        p=ref[i];
                        flag=0;
                        for(k=0;k
                                if(a[k]==p)
                                {
                                        flag=1;
                                        break;
                                }
                                else if(a[k]==-1)
                                {
                                       a[k]=p;
                                       pf++;
                                      flag=1;
                                       break;
                                 }
                       if(flag==0)
                       {
                                 pf++;
                                 for(k=0;k
                                         index[k]=get_index2(a[k],ref,n,i);
                                 for(k=0;k
                                        if(index[k]==-99)
                                        {
                                                a[k]=p;
                                                flag=1;
                                                break;
                                        }
                                 if(flag==0)
                                 {
                                         j=get_max(index,m);
                                         for(k=0;k
                                               if(a[k]==ref[j])
                                                       a[k]=p;
                                 }

                        }

                 for(l=0;l
                 {
                 printf("%d\t",a[l]);
                 }
                 printf("\n");
                }
printf("No.of page fault %d",pf);

}

No comments:

Post a Comment

Followers