Saturday, August 9, 2014

These are a list of various unix system calls that are implemented in C. If you have any doubts, please let me know.

PROGRAM DEVELOPMENT

#include<stdio.h>
#include<dirent.h>
struct dirent *dptr;
void main(int argc, char *argv[])
{
  char buff[100];
  DIR *dirp;
  printf("\n\nEnter Directory Name: ");
  scanf("%s",buff);
  if((dirp=opendir(buff))==NULL)
  {
   printf("\n The given directory does not exist\n");
   exit(1);
  }
  while(dptr=readdir(dirp))
  {
    printf("%s\n",dptr->d_name);
  }
  closedir(dirp);
}
























PROGRAM DEVELOPMENT

#include<stdio.h>
#include<unistd.h>
main()
{
  int pid,pid1,pid2;
  pid=fork();
  if(pid==-1)
  {
    printf("ERROR IN PROCESS CREATION\n");
    exit(1);
  }
  if(pid!=0)
  {
   pid1=getpid();
   printf("\nThe parent process id is %d\n",pid1);
   }
  else
   {
    pid2=getpid();
   printf("\nThe child process id is %d\n",pid2);
  }

}













PROGRAM DEVELOPMENT

#include<stdio.h>
int main()
{
  int fd;
  if((fd=open("file.dat"))==-1)
  {
   perror("cannot open the file.dat");
   exit(0);
  }
  else
  printf("\nFILE OPENED SUCCESSFULLY");
  return 0;
}

















PROGRAM DEVELOPMENT

#include<stdio.h>
main()
{
 char b[20];
 int fd,xr;
 if((fd=open("balaji",0))==-1)
 {
  printf("Cannot open file\n");
  exit(1);
 }
 do
 {
   xr=read(fd,b,20);
   b[xr]='\0';
   printf("%s",b);
 }
while(xr==20);
 close(fd);
}















PROGRAM DEVELOPMENT

#include<stdio.h>
main(int ac,char *av[])
 {
  int fd;
  int i=1,j=0;
  char *sep="";
  printf("%s",av[1]);
  if(ac<1)
  {
   printf("\n INSUFFICIENT ARGUMENTS");
   exit(1);
  }
  if((fd=open("balaji",0660))==-1)
  {
   printf("\n cannot create the file");
   exit(1);
  }
  while(i<ac)
  {
 j=write(fd,av[i],(unsigned)strlen(av[i]));
  printf("%d",j);
   write(fd,sep,(unsigned)strlen(sep));
   i++;
   }
   close(fd);
}










PROGRAM DEVELOPMENT

#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
int main()
{
 int s;
 struct stat st;
  s=stat("sss.c",&st);
  if(s<0)
  {
   printf("use of stat() is unsuccessful\n");
  exit(0); 
   }
   printf("\ninode number of sss.c file is %d",st.st_ino);
   printf("\nnumber of links to sss.c is %d",st.st_nlink);
   printf("\nfile permissions of sss.c file is %d",st.st_mode);
   printf("\nsize of sss.c file is %d",st.st_size);
   printf("\nlast access time of sss.c file is %d\n",st.st_atime);
}


Creating, writing to and reading from shared memory program

This is an operating systems program to implement creating, writing to and reading from a shared memory. If you have any doubts please let me know.

#include<sys/mman.h>                                                                                                                                                                                                                           
#include<sys/stat.h>
#include<fcntl.h>
#include<stdio.h>
main()
{
    int fd;
    char mamname[20];
    printf(“\nEnter the shared memory name: “);
   scanf(“%s”,memname);
   fd=shm_open(memname,O_CREAT,S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
   ftruncate(fd,100);
   if(fd>0)
   printf(“\nSUCCESS”);
   else
   printf(“\nFAIL”);
}



#include<sys/mman.h>                                                                                                                                                                                                                            
#include<sys/stat.h>
#include<fcntl.h>
#include<stdio.h>
main()
{
    int fd,i;
    char mamname[20];
    unsigned char *ptr;
    printf(“\nEnter the shared memory name: “);
   scanf(“%s”,memname);
   fd=shm_open(memname,O_CREAT,S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
   ptr=mmap(NULL,100,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0);
   for(i=0;*(ptr+i)!=’\0’;i++)
    printf(“%c”,*(ptr+i));
}



#include<sys/mman.h>                                                                                                                                                                                                                            
#include<sys/stat.h>
#include<fcntl.h>
#include<stdio.h>
main()
{
    int fd,i;
    char mamname[20],s[100];
    unsigned char *ptr;
    printf(“\nEnter the shared memory name: “);
   scanf(“%s”,memname);
   fd=shm_open(memname,O_CREAT,S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
   ptr=mmap(NULL,100,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0);
   if(fd>0)
   {
    printf(“\nEnter data:”);
   gets(s);
   for(i=0;s[i]!=’\0’;i++)
    *(ptr+i)=s[i];
}


Round Robin and Priority Scheduling Program

This is an operating systems lab program to implement round robin as well as priority scheduling. If you have any doubts please let me know.

#include<stdio.h>

struct process
{
char p[10];
int at,bt,pr,wt,tt,r,ar;
}a[100],temp;
int n;

void sorta()
{
    int i,j;
    for(i=0;i<n-1;i++)
    {
         for(j=0;j<n-i-1;j++)
         {
            if(a[j].at>a[j+1].at)
           {
               temp=a[j+1];
               a[j+1]=a[j];
               a[j]=temp;
           }
         }
    }
}

void sortp()
{
      int i,j;
      for(i=0;i<n-1;i++)
      {
           for(j=0;j<n-i-1;j++)
           {
               if(a[j].pr>a[j+1].pr)
              {
                   temp=a[j+1];
                   a[j+1]=a[j];
                   a[j]=temp;
              }
           }
      }
}


void sortar()
{
   int i,j;
   for(i=0;i<n-1;i++)
   {
        for(j=0;j<n-i-1;j++)
        {
             if((a[j].ar>a[j+1].ar)||(a[j].ar==a[j+1].ar&&a[j].ar!=a[j].at))
             {
    temp=a[j+1];
                a[j+1]=a[j];
                 a[j]=temp;
}
        }
   }
}

void main()
{
         int i,c,t=0,f1=1,f=0,tq=0,cf,nt=0;
         float aw=0,at=0;
         printf("Enter the number of Processes: ");
         scanf("%d",&n);
         printf("Enter the process details");
        for(i=0;i<n;i++)
        {
printf("\nProcess ID:");
scanf("%s",&a[i].p);
printf("\nPriority:");
scanf("%d",&a[i].pr);
printf("\nArrival time:");
scanf("%d",&a[i].at);
printf("\nBurst time:");
scanf("%d",&a[i].bt);

a[i].wt=a[i].tt=0;
a[i].r=a[i].bt;
a[i].ar=a[i].at;
         }
printf("\nMENU:\n1.Priority\n2.Round Robin\n");
printf("\nEnter your choice:");
scanf("%d",&c);
switch(c)
{
case 1:
  sorta();
  sortp();
  printf("\nGantt Chart for Priority\n");
  printf("%d",t);
  while(f1==1)
  {
     f1=0;
     for(i=0;i<n;i++)
     {
          if(a[i].at<=t&&a[i].r!=0)
          {
            a[i].wt=t-a[i].at;
            aw+=a[i].wt;
            printf("-----%s",a[i].p);
            t=t+a[i].bt;
            printf("--------");
            printf("%d",t);
            a[i].tt=t-a[i].at;
             at+=a[i].tt;
            a[i].r=0;
            f=0;
            f1=1;
            break;
           }
            else if(f==0&&a[i].r!=0)
            {
            nt=a[i].at;
            f=f1=1;
            }
            else if(nt>a[i].at&&a[i].r!=0)
 
                nt=a[i].at;
       }
     if(f==1)
     {
            t=nt;
            printf(" %d",t);
            f=0;
     }
  }

  printf("\nAverage waiting time=%f",(aw/n));
  printf("\nAverage turn around time=%f\n",(at/n));
  break;
case 2:
printf("\nEnter the time quantum:");
scanf("%d",&tq);
if(tq>0)
{
sorta();
printf("\nGantt Chart for Round Robin\n");
printf("%d",t);
while(f1==1)
{
   cf=0;
   f1=0;
   sortar();
   for(i=0;i<n;i++)
   {
         if(a[i].at<=f&&a[i].r!=0)
        {
a[i].wt+=t-a[i].tt;
printf("------%s",a[i].p);
printf("------");
if(a[i].r<tq)
{
    t=t+a[i].r;
    a[i].r=0;
}
        else
        {
t=t+tq;
a[i].r-=tq;
         }
          printf("%d",t);
          a[i].tt=a[i].ar=t;
         if(cf==0)
        {
cf=1;
f=t;
         }
  }
  if(a[i].r!=0&&f1==0)
  f1=1;
  if(a[i].at>f)
  break;
}
if(cf==0&&f1==1)
  {
      t=f=a[i].at;
      printf(" %d",t);
   }
}
for(i=0;i<n;i++)
{
a[i].wt=a[i].wt-a[i].at;
a[i].tt=a[i].tt-a[i].at;
aw+=a[i].wt;
at+=a[i].tt;
}


printf("\nAverage waiting time=%f",(aw/n));
  printf("\nAverage turn around time=%f\n",(at/n));
      }
else
printf("\nTime quantum is wrong");
break;
default:
printf("\nWrong Choice\n");  
break;
   }
}


Shell programs for prime and palindrome

Shell scripting programs to implement whether a number is prime as well as a program to check for a palindrome. If you have any doubts please let me know 

PRIME
#!/bin/bash
echo " enter a number"
read n
i=2
flag=0
while((i<=$n/2))
do
((c = n % i))
if ((c==0))
then
flag=1
fi
((i=i+1))
done
if ((flag==0))
then
echo "prime"
else
echo "composite"
fi



PALINDROME
echo “enter a string to be entered:”
read str
echo
len=`echo $str | wc –c`
len= ((len-1))
i=1
j= ((len/2))
while((i<j))
do
k=`echo $str | cut –c $i`
l=`echo $str | cut –c $len`
if((k!=l))
then
flag=1;
fi
((i=i+1))
((len=len-1))
Done
If((flag==0))
then
Echo “palindrome”
Else
Echo “ not palindrome”

fi    

Implementation of ls and grep

C programs for the implementation of ls and grep for the operating systems lab. If you have any doubts, please let me know.

ls
#include<stdio.h>
#include<dirent.h>
main(int argc, char ** argv)
{
    DIR *dp;
    struct dirent *link;
    dp=opendir(argv[1]);
    printf("\n the contents of directory %s are \n",argv[1]);
     while((link==readdir(dp))!=0)
     printf("%s",link->d_name);
      closedir(dp);
}

GREP
#include<stdio.h>
#include<string.h>
#define max 1024
void usage()
{
   printf("usage:\t./a.out filename word\n");
}
int main(int argc, char *argv[])
{
   FILE *fp;
   char fline[max];
   int count=0;
   int occurrences=0;
   if(argc!=3)
   {
     usage();
      exit(1);
   }
   if(!(fp=fopen(argv[1],"r")))
   {
      printf("grep : could not open file: %s\n",argv[1]);
       exit(1);
   }
   while(fgets(fline,max,fp)!=NULL)
    {
        count++;
         if(newline=strchr(fline,'\n'))
         *newline='\0';
         if(strchr(fline,argv[2]!=NULL)
          {
              printf("%s: %d %s\n",argv[1],count,fline);
              occurrences++;
          }
      }
}