Thursday, April 12, 2012

Insert and Search


clear
echo "Enter your file name:"
read fn
search=` find $fn `
if [ $fn = $search ]
then
echo "Enter your word to be searched:"
read se
word=`fgrep -a -o -m 1 $se $fn`
if [ $se = $word ]
then
choice=`zenity --list \
--title="search result" \
--column= " "\
"sucess-found the specified word"`
else
choice=`zenity --list \
--title="search result" \
--column=" "\
"failed-specified word not found"`
fi
else
choice=`zenity --list \
--title="error" \
--column=" "\
"failed-file not found"`
fi

Fork Child


#include <stdio.h>
int main(void)
{
 int pid;
 pid=fork();
if(pid==0)
 {
   int j;
  for(j=0;j<10;j++)
  {
   printf("child:=%d\n",j);
   sleep(1);
  }
  exit(0);
}
  else if(pid>0)
 {
 int i;
 for(i=0;i<10;i++)
 {
  printf("parent:%d\n",i);
  sleep(1);
 }
 }
 else
 {
  fprintf(stderr,"couldn't fork");
  exit(1);
 }
}

Fan of Process


#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>
int main()
{
int i;
int n=10;
pid_t childpid;
for(i=1;i<n;++i)
if((childpid=fork())<=0)
break;
fprintf(stderr,"this is process %ld with parent %ld\n",(long)getpid(),(long)getpid());
}

Error Checking


#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
const char *FILE_NAME="File.c";
int main ( int argc, char ** argv )
{
int fd=0;
printf ("FINDING %s...\n",FILE_NAME);
fd = open (FILE_NAME,O_RDONLY,0644);
if
(fd<0)
{
perror("Error opening file");
printf("Error opening file:%s\n",strerror( errno ) );
}
else
{
printf("\n File Present");
}
return EXIT_SUCCESS;
}

Error Handling


#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int divident = 50;
int divisor = 0;
int quotient;
if (divisor == 0)
 {
 fprintf (stderr,"Division by zero ! aborting.....\n");
 exit(EXIT_FAILURE);
 }
else
 {
 quotient=divident /divisor ;
 exit (EXIT_SUCCESS);
 }
}

Chain of Process


#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>
int main()
{
  int i;
  int n=10;

  pid_t childpid;

  for(i=1;i<n;++i)
   if(childpid=fork())
   break;

 printf("this is process %ld with parent %ld\n",(long)getpid(),(long)getppid());

}

Computer architecture Q . Paper {MCA}


*Please download and rotate the second image

Just practice these questions for the exams.
Hope U find this useful.