Thursday, 3 November 2016

Shell Scripting #Check a number to be Even or Odd.

#Check a number to be Even or Odd.
echo "Enter the number";
read a
if [ $((a%2)) -ne 0 ]
then
 echo "odd"
else
  echo "even"
fi

( Shell Scripting ) check a number to be Positive or negative

#check a number to be Positive or negative
echo "enter the number";
read a
if [ $a -lt 0 ]
then
 echo "Negative";
else
 echo "Positive";
fi

( Shell Scripting ) Search a String in the file.

clear
if [ $# = 3 ]
then
   echo "Enter the file int this format---";
   echo "filename string";
 else
  echo "the no. of $1 occurences are ";
  grep -c $2 $1
fi

Wednesday, 19 October 2016

Program of Banker's Algoritham Using C language

#include<stdio.h>
#define MAX_NO 10

int main()
{
 int allo[MAX_NO][MAX_NO],max[MAX_NO][MAX_NO],need[MAX_NO][MAX_NO],av[MAX_NO];
 int i,n,j,p,flag[MAX_NO],count;
 printf("Enter the number of Process: ");
 scanf("%d",&n);
 count=n;
 printf("Enter the number of Resource : ");
 scanf("%d",&p);
 printf("Enter the allocation amtrix: ");
 for(i=0;i<n;i++)
  {
    for(j=0;j<p;j++)
      {     
       scanf("%d",&allo[i][j]);
      }
   }
printf("Enter the Max amtrix: ");
 for(i=0;i<n;i++)
  {
    for(j=0;j<p;j++)
      {
      
       scanf("%d",&max[i][j]);
      }
   }
printf("\n Enter the available matrix: ");
  for(i=0;i<p;i++)
    {  
       scanf("%d",&av[i]);
     }

 printf("\n\n********Allocation Matrix**********\n\n");
  for(i=0;i<n;i++)
  {
    for(j=0;j<p;j++)
      {
        printf("%d\t",allo[i][j]);
       }
    printf("\n");
   }
printf("\n\n********Max Matrix**********\n\n");
  for(i=0;i<n;i++)
  {
    for(j=0;j<p;j++)
      {
        printf("%d\t",max[i][j]);
       }
      printf("\n");
   }
printf("\n\n********Need Matrix**********\n\n");
 for(i=0;i<n;i++)
  {
    for(j=0;j<p;j++)
      {
        if(max[i][j]>=allo[i][j])
          {
           need[i][j]=max[i][j]-allo[i][j];
           }
        else
           {
           printf("\n Allocation is more than max\n");
           }
      }
  }
for(i=0;i<n;i++)
  {
    for(j=0;j<p;j++)
      {
        printf("%d\t",need[i][j]);
       }
      printf("\n");
   }
printf("\n***********available matrix*************\n ");
  for(i=0;i<p;i++)
    {  
       printf("%d\t",av[i]);
     }
printf("\n*******safe sequence*********\n");
/*while(count!=0)
 {
  
   for(i=0;i<n;i++)
     {
       flag[i]=0;
    for(j=0;j<p;j++)
      {
        if(need[i][j]<=av[i] && flag[i]!=1)
         {
          av[i]=av[i]+allo[i][j];
          printf("P[%d]",i+1);
          flag[i]=1;
          count--;
          }
      
       }
    }
}
*/
          





}


Output :
/home/ABC/

Find Unix Directory using C program

 #include<stdio.h>
 #include<stdlib.h>
 #include <sys/types.h>
 #include <dirent.h>
int main()
{
  char dir_name[20];
  DIR *p;
  struct dirent *d;
  printf("\nEnter Directory name :");
  scanf("%s",dir_name);
  p=opendir(dir_name);
  if(p==NULL)
    {
      perror("Can't find directory\n");
      exit(-1);
    }
 while(d=readdir(p))
printf("%s\n",d->d_name);
return 0;
}

Ls command of Unix implement in c language

#include<stdio.h>
 #include<stdlib.h>
 #include <sys/types.h>
 #include <dirent.h>
 
 int main()
 {
    char *curr_dir = NULL;
    DIR *dp = NULL;
    struct dirent *dptr = NULL;
    unsigned int count = 0;
     curr_dir = getenv("PWD");
    if(NULL == curr_dir)
    {
        printf("\n ERROR : Could not get the working directory\n");
        return -1;
    }
   dp = opendir((const char*)curr_dir);
    if(NULL == dp)
    {
        printf("\n ERROR : Could not open the working directory\n");
        return -1;
    }
      printf("\n");
        for(count = 0; NULL != (dptr = readdir(dp)); count++)
    {
        printf("%s  \n",dptr->d_name);
    }
    printf("\n %u", count);
      return 0;
 }

Implementing the ls -al command in C

#include<stdio.h>
 #include<stdlib.h>
 #include <sys/types.h>
 #include <dirent.h>
 
 int main(void)
 {
    char *curr_dir = NULL;
    DIR *dp = NULL;
    struct dirent *dptr = NULL;
    unsigned int count = 0;
 
    // Get the value of environment variable PWD
    curr_dir = getenv("PWD");
    if(NULL == curr_dir)
    {
        printf("\n ERROR : Could not get the working directory\n");
        return -1;
    }
 
    // Open the current directory
    dp = opendir((const char*)curr_dir);
    if(NULL == dp)
    {
        printf("\n ERROR : Could not open the working directory\n");
        return -1;
    }
 
    printf("\n");
    // Go through and display all the names (files or folders)
    // Contained in the directory.
    for(count = 0; NULL != (dptr = readdir(dp)); count++)
    {
        printf("%s  \n",dptr->d_name);
    }
    printf("\n %u", count);
 
    return 0;
 }