Tuesday 25 February 2014

7 File programms

c program for first come first serve scheduling

c program for first come first serve scheduling. This c program computes average and total 
waiting time and turn around time of processes. c program for first come first serve gives 
practical knowledge of how a operating system perform task scheduling.

c program for first come first serve scheduling

#include<stdio.h>
#include<conio.h>

void display();
void getdata();
void waitTime();
void turnAroundTime();

int n, a[100],b[100];

int main()
{
    printf("***** FCFS ( FIRST COME FIRST SERVE ) SCHEDULING ******\n\n");

    getdata();
    display();
    waitTime();
    turnAroundTime();
    getch();
}

void getdata()
{   
    char c;
    printf("Enter the number of process ");
    scanf("%d",&n);

    printf("\nDo you need to enter the arrival time of process [y/n] or [Y/N]\n");
    scanf("%s",&c);

    for(int i=0; i<n; i++)
    {
          printf("Enter the %d process burst time : ",i+1);
          scanf("%d",&b[i]);
          if(c=='y' || c=='Y')
          {
                  printf("Enter the %d process arrival time : ",i+1);
                  scanf("%d",&a[i]);
          }
          else
          {
                  a[i]=0;
          }
    }
}

void display()
{
     printf("\nPROCESS\tBURST TIME\tARRIVAL TIME6\n");
     for(int i=0; i<n; i++)
     {
             printf("    %d\t      %d\t      %d\n",i+1,b[i],a[i]);
     }
}

void waitTime()
{
     int w[100];
     float totalWait=0;
     w[0]=0;
     for(int i=1; i<n; i++)
     {
           w[i]=b[i-1]-a[i]+w[i-1];
           totalWait=w[i]+totalWait;
     }
     printf("\nTotal Waiting time = %f",totalWait);
     printf("\nAverage Waiting time = %f",totalWait); 
}

void turnAroundTime()
{
     int tat[100];
     float totalTat=0;
     tat[-1]=0;
     for(int i=0; i<n; i++)
     {
             tat[i]=b[i]-a[i]+tat[i-1];               
             totalTat=totalTat+tat[i];
     }
     printf("\n\nTotal Turn Around Time(TAT) = %f",totalTat);
     printf("\nAverage Turn Around Time Avg.(TAT) = %f",totalTat/n);
}
//CREATE A FILE AND STORE DATA IN IT IN C PROGRAM
#include<stdio.h> int main(){ FILE *fp; char ch; fp=fopen("file.txt","w"); printf("\nEnter data to be stored in to the file:"); while((ch=getchar())!=EOF) putc(ch,fp); fclose(fp); return 0; }
//COPY DATA FROM ONE FILE TO ANOTHER FILE 
USING C PROGRAM
#include<stdio.h>
int mai()
{
  FILE *p,*q;
  char file1[20],file2[20];  
 char ch;
  printf("\nEnter the source file name to be copied:");
  gets(file1);
  p=fopen(file1,"r");  
 if(p==NULL)
{   
    printf("cannot open %s",file1);
      exit(0);
  }
  printf("\nEnter the destination file name:");
  gets(file2);
  q=fopen(file2,"w");   
if(q==NULL)
{
      printf("cannot open %s",file2);
      exit(0);
  }
  while((ch=getc(p))!=EOF)
      putc(ch,q);   
printf("\nCOMPLETED");   
fclose(p);
  fclose(q);
return 0;
}
//DISPLAY SOURCE CODE AS OUTPUT IN C PROGRAM
#include<stdio.h>
int main()
{
     FILE *p;
    char ch;
    p=fopen("raja.c","r");
   while((ch=getc(p))!=-1)   
      putchar(ch);
    fclose(p);   
 return 0;
 }//How to read a text file by c program
#include<stdio.h>
 int main()
{ char str[70];
 FILE *p;
 if((p=fopen("string.txt","r"))==NULL)
{
printf("\nUnable t open file string.txt");
 exit(1);
 }
 while(fgets(str,70,p)!=NULL) puts(str);
 fclose(p);
 return 0;
 }
//WRITING OF ENTIRE ARRAY TO A FILE USING C
 PROGRAM
#include<stdio.h>
 int main()
{  
 FILE *p;  
 int i,a[10]; 
  if((p=fopen("myfile.dat","wb"))==NULL)

      printf("\nUnable to open file myfile.dat");
       exit(1); 
  }
   printf("\nEnter ten values, one value on each line\n");   
for(i=0;i<10;i++)     
  scanf("%d",&a[i]);   
fwrite(a,sizeof(a),1,p);
   fclose(p);   
return 0;
 }
//CONCATENATE MANY FILES AND STORE THEM IN A FILE IN C
#include<stdio.h>
void concatenate(FILE *fp1,FILE *fp2,char *argv[],int argc);
 int main(int argc,char *argv[])
{  
 FILE *fp1,*fp2;   
concatenate(fp1,fp2,argv,argc);
    return 0;
 }
void concatenate(FILE *fp1,FILE *fp2,char **argv,int argc)
{
    int i,ch;
    fp2=fopen("files","a");   
for(i=1;i<argc-1;i++)
{  
    fp1=fopen(argv[i],"r");  
    while((ch=getc(fp1))!=EOF)       putc(ch,fp2);   
}
 } 
//WRITE A C PROGRAM TO FIND SIZE OF A FILE
#include <time.h>
#include <sys\stat.h>
 #include <stdio.h>
 void main()
 {
    struct stat status;   
 FILE *fp;
   fp=fopen("test.txt","r");    
fstat(fileno(fp),&status);  
  clrscr();
    printf("Size of file : %d",status.st_size);
     printf("Drive name   : %c",65+status.st_dev);
   getch();
 }
Explanation: Function int fstat (char *, struct stat *) store the information of open file in form of structure struct stat 
Structure struct stat has been defined in sys\stat.h as
struct stat {     short  st_dev,   st_ino;     short  st_mode,  st_nlink;     int    st_uid,   st_gid;     short  st_rdev;     long   st_size,  st_atime;     long   st_mtime, st_ctime; };
Here
(a)st_dev: It describe file has stored in which drive of your computer  ,it returns a number.
(b)st_mode:  It describes various modes of file like file is read only, write only, folder, character file etc.
(c)st_size: It tells the size of file in byte.
(d)st_ctime:It tells last data of modification of the file in date format.
Note: 65 is ASCII value of A .So after adding status.st_dev with 65 it will return appropriate drvie name as in
 your computer.
//How to read a text file by c program
#include<stdio.h>
int main()
{
 char str[70];
 FILE *p;
 if((p=fopen("string.txt","r"))==NULL)
{
 printf("\nUnable t open file string.txt");
 exit(1);
 }
 while(fgets(str,70,p)!=NULL) puts(str);
 fclose(p);
return 0;
}
//WRITING OF ENTIRE ARRAY TO A FILE USING C PROGRAM
#include<stdio.h>
 int main()
{
  FILE *p;
   int i,a[10];
   if((p=fopen("myfile.dat","wb"))==NULL)
{
       printf("\nUnable to open file myfile.dat");     
 exit(1);
   }
  printf("\nEnter ten values, one value on each line\n");
   for(i=0;i<10;i++)   
   scanf("%d",&a[i]);
 fwrite(a,sizeof(a),1,p);
   fclose(p);  
return 0;
}
//CONCATENATE MANY FILES AND STORE THEM IN A FILE IN C
#include<stdio.h>
 void concatenate(FILE *fp1,FILE *fp2,char *argv[],int argc);
 int main(int argc,char *argv[])
{
    FILE *fp1,*fp2;   
concatenate(fp1,fp2,argv,argc);  
 return 0;
}
void concatenate(FILE *fp1,FILE *fp2,char **argv,int argc)
{
    int i,ch;    fp2=fopen("files","a");
    for(i=1;i<argc-1;i++)
{
      fp1=fopen(argv[i],"r");      
while((ch=getc(fp1))!=EOF)    
  putc(ch,fp2);  
 }
 } 
//CONCATENATE MANY FILES AND STORE THEM IN A FILE IN C
#include<stdio.h>
 void concatenate(FILE *fp1,FILE *fp2,char *argv[],int argc);
 int main(int argc,char *argv[])
{
    FILE *fp1,*fp2;   
concatenate(fp1,fp2,argv,argc);
   return 0;
 }
void concatenate(FILE *fp1,FILE *fp2,char **argv,int argc)
{
   int i,ch;
    fp2=fopen("files","a");   
for(i=1;i<argc-1;i++)
{
       fp1=fopen(argv[i],"r");      
while((ch=getc(fp1))!=EOF)
      putc(ch,fp2);
   }
 } 
/Write a c program to know given file is regular file, character special or it is directory?
#include "time.h"
 #include "sys\stat.h"
 #include "stdio.h"
void main()
{
     struct stat status;
     FILE *fp;
    stat("c:\\tc\\bin",&status);
    clrscr();
   if (status.st_mode & S_IFDIR)    
     printf("It is directory.\n");  
  if (status.st_mode & S_IFCHR)     
    printf("It is chracter file.");    
if (status.st_mode & S_IFREG)       
  printf("It is reggular file.");
     getch();
}
Output: It is directory. Explanation: Function int stat (char *, struct stat *) store the information of open file in form of structure struct stat Structure struct stat has been defined in sys\stat.h as
struct stat {     short  st_dev,   st_ino;     short  st_mode,  st_nlink;     int    st_uid,   st_gid;     short  st_rdev;     long   st_size,  st_atime;     long   st_mtime, st_ctime; };
Here
(m)st_dev: It describe file has stored in which drive of your computer.
(n)st_mode:  It describes various modes of file like file is read only, write only, folder, character file etc.
(o)st_size: It tells the size of file in byte.
(p)St_ctime:It tells last data of modification of    the file.
There are some macro has been defined in sys\stat.h
Name         Meaning S_IFMT        File type mask S_IFDIR       Directory S_IFIFO      FIFO special S_IFCHR      Character special S_IFBLK      Block special S_IFREG      Regular file S_IREAD      Owner can read S_IWRITE     Owner can write S_IEXEC      Owner can execute
So masking or bitwise and operation between status.st_mode and S_IFDIR return true if it is directory and so on 
//Write a c program to know read/write permission of given file.
#include "time.h"
 #include "sys\stat.h"
 #include "stdio.h"
void main()
{
     struct stat status;
   FILE *fp;
    stat("test.txt",&status);   
 clrscr();
     if (status.st_mode & S_IREAD)      
   printf("You have read permission.\n");  
  if (status.st_mode & S_IWRITE)   
      printf("You have write permission.");
     getch();
}
//Write a c program to know the last date of modification of any file
#include "time.h"
 #include "sys\stat.h"
 #include "stdio.h"
int main()
{    
struct stat status;
   FILE *fp;
    fp=fopen("test.txt","r");    
fstat(fileno(fp),&status);   
      printf("Last date of modification : %s",ctime(&status.st_ctime));    
return 0;
 }
Explanation: Function int fstat(char *, struct stat *) store the  information of open file in form of  structure struct stat
Structure struct stat has been defined in sys\stat.h as
struct stat {     short  st_dev,   st_ino;     short  st_mode,  st_nlink;     int    st_uid,   st_gid;     short  st_rdev;     long   st_size,  st_atime;     long   st_mtime, st_ctime; }; Here
(e)st_dev: It describe file has stored in which drive of your computer.
(f)st_mode:  It describes various modes of file like file is read only, write only, folder, character file etc.
(g)st_size: It tells the size of file in byte.
(h)st_ctime:It tells last data of modification of the file in date format.
Function ctime convert date type in string format 
//Write a c program to find out the size and drive where file has stored of any given file?
#include "time.h"
 #include "sys\stat.h"
 #include "stdio.h"
int main()
{
     struct stat status;  
  FILE *fp;
   fp=fopen("test.txt","r");    
fstat(fileno(fp),&status);    
printf("Size of file : %d",status.st_size);
     printf("Drive name   : %c",65+status.st_dev);  
  return 0;
 }

No comments:

Post a Comment