Showing posts with label algorithms. Show all posts
Showing posts with label algorithms. Show all posts

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];
}


FCFS and SJF program

 This is a program for the implementation of First Come First Serve and Shortest Job First Scheduling. If you have any doubts, please let me know.

#include<stdio.h>
#include<string.h>
struct process
{
   int arr,bur,wait,turn,t;
   char pid[5];
}p[100],temp;

int n;
void chart()
{
            int i,j,k;
            printf("0");
            for(j=0;j<n;j++)
            {
                        i=0;
                        while(i!=p[j].bur)
                        {
                                    printf("=");
                                    i++;
                        }
            printf("%d",p[j].t);
            }
            printf("\n");
            i=0;
            printf("p%s",p[0].pid);
           
            for(j=0,k=1;j<n-1;j++,k++)
            {
                        while(i!=p[j].t)
                        {
                                    printf(" ");
                                    i++;
                        }
            printf("p%s",p[k].pid);
            }
            printf("\n");

}

void avg()
    {
        int i;
        float avgw=0,avgt=0;
        for(i=0;i<n;i++)
       {
        avgw=avgw+p[i].wait;
        avgt=avgt+p[i].turn;
      }
    avgw=avgw/n;
    avgt=avgt/n;

    printf("\nAverage waiting time :%d\nAverage turnaround time :%d\n",avgw,avgt);
 }


void disp()
{
         printf("\npid\tburst\tarrival\twait\tturnaround\n");
         int i;
         for(i=0;i<n;i++)
            {
            printf("%s\t%d\t%d\t%d\t%d\n",p[i].pid,p[i].bur,p[i].
arr,p[i].wait,p[i].turn);
        }
    avg();
    chart();

}

void fcfs()
{
        int i,j;
         for(i=0;i<n-1;i++)
            {
            for(j=i+1;j<n;j++)
               {
                if(p[i].arr>p[j].arr)
                {
                    temp=p[i];
                    p[i]=p[j];
                    p[j]=temp;
                }
             }
        }
        p[0].wait=0;
        p[0].turn=p[0].wait+p[0].bur;
                        p[0].t=p[0].bur;

        for(i=1;i<n;i++)
       {
            p[i].t=p[i-1].t+p[i].bur;
            p[i].wait=p[i-1].t-p[i].arr;
            p[i].turn=p[i].wait+p[i].bur;
      }
                        disp();
}


void sjf()
{
            int i,j;
            for(i=0;i<n-1;i++)
            {
            for(j=i+1;j<n;j++)
                {
                if(p[i].arr>p[j].arr)
                {
                    temp=p[i];
                    p[i]=p[j];
                    p[j]=temp;
                }
                }
            }
            for(i=1;i<n-1;i++)
            {
            for(j=i+1;j<n;j++)
                {
                if(p[i].bur>p[j].bur)
                {
                    temp=p[i];
                    p[i]=p[j];
                    p[j]=temp;
                }
                }
          }

            p[0].wait=0;
            p[0].turn=p[0].wait+p[0].bur;
            p[0].t=p[0].bur;
            for(i=1;i<n;i++)
            {
               p[i].t=p[i-1].t+p[i].bur;
               p[i].wait=p[i-1].t-p[i].arr;
               p[i].turn=p[i].wait+p[i].bur;
            }
                disp();
}
void main()
{
    int j,i,ch;
    printf("Enter number of processess : ");
    scanf("%d",&n);
    for(i=0;i<n;i++)
        {
            printf("Enter pid : ");
            scanf("%s",p[i].pid);
            printf("enter arrival time : ");
            scanf("%d",&p[i].arr);
            printf("Enter burst time : ");
            scanf("%d",&p[i].bur);
        }


    printf("\nMENU\n1.FCFS\n2.SJF\n3.exit\nEnter your choice : ");
    scanf("%d",&j);
    switch(j)
    {
    case 1:fcfs();
              break;
    case 2:  sjf();
              break;
    case 3:
          break;
    default : printf("Wrong Choice");
    }
}