C Programming Tutorial

 
 
 
 
#include <stdio.h>
#include <stdlib.h>
#include \"common.h\"
int main(int argc,char *argv[])
{
 FILE *fp;
 int id,nproc,i,*lock,*lock1,*digit,*vovel,*consonet,ldigit=0,lvovel=0,lconsonet=0,shmid[5];

 lock=(int *)shared(sizeof(int),&shmid[0]);
 sem_init(lock);
 lock1=(int *)shared(sizeof(int),&shmid[1]);
 sem_init(lock1);

 digit=(int *)shared(sizeof(int),&shmid[2]);
 *digit=0;
 vovel=(int *)shared(sizeof(int),&shmid[3]);
 *vovel=0;
 consonet=(int *)shared(sizeof(int),&shmid[4]);
 *consonet=0;

 char c=\'p\';

 if(argc!=2)
 {
  printf(\"\\nError - insufficient no of arguments.\");
  getchar();
  exit(-1);
 }
 fp=fopen(argv[1],\"r\");
 if(fp==NULL)
 {
  printf(\"\\nError - unable to open input file.\");
  getchar();
  exit(-1);
 }

 printf(\"\\nEnter total no of processes : \");
 scanf(\"%d\",&nproc);

 id=process_fork(nproc);

  while(1)
  {
   sem_lock(lock);

     c=getc(fp);
     sem_unlock(lock);

       if(c==EOF)
          break; 


        if(c>=\'0\'&&c<=\'9\')
            ldigit++;
        else if(c==\'a\'||c==\'A\'||c==\'e\'||c==\'E\'||c==\'o\'||c==\'O\'||c==\'i\'||c==\'I\'||
                c==\'u\'||c==\'U\')
            lvovel++;
        else if((c>=\'A\'&&c<=\'Z\')||(c>=\'a\'&&c<=\'z\'))
           lconsonet++;

    }


  sem_lock(lock1);
  *digit+=ldigit;
  *vovel+=lvovel;
  *consonet+=lconsonet;
  sem_unlock(lock1);
  process_join(id,nproc);
  printf(\"\\nFile Name : %s\",argv[1]);
  printf(\"\\nNo of digits : %d\",*digit);
  printf(\"\\nNo of vovels : %d\",*vovel);
  printf(\"\\nNo of consonet : %d\\n\",*consonet);
  getchar();      
  return(0);
}
/*
  Output:-

          Enter total no of processes : 4

        File Name : p.txt
        No of digits : 4
        No of vovels : 3
        No of consonet : 2
*/

 
 
Didn't find what you were looking for? Find more on Program to find total numbers vowels, constants and digits in an input file