Logo 
Search:

C Programming Articles

Submit Article
Home » Articles » C Programming » Parallel Processing ProgramsRSS Feeds

Program to find vowels, constants and digits from a file using threading

Posted By: Eleanor Hughes     Category: C Programming     Views: 5213

Write a Program to find vowels, constants and digits a file using threading.

Code for Program to find vowels, constants and digits from a file using threading in C Programming

# include<stdio.h>
# include <pthread.h>

void *count();
int vowels,consonants,ok,digit;
FILE *fp;
pthread_t id;
pthread_mutex_t lock1;
main()
{
    int i,j,n_thread;
    
    fp=fopen("read1","r");
    digit=0;vowels=0;consonants=0;
    if(fp==NULL)
    {
        printf("File Reading Error");
        exit(0);
    }

    pthread_mutex_init(&lock1,NULL);
    printf("Enter the NO of Thread ");
    scanf("%d",&n_thread);
    ok=0;
    for(i=0;i<n_thread;i++)
        {
                
        
        if(0==pthread_create(&id,NULL,count,NULL))
                {
                        continue;
            
                }
                else
                {
                        printf("Error In Thread Creation\n");
                        exit(0);
                }
        }
    /*for(i=0;i<n_thread;i++)
{
if(0==pthread_create(&id,NULL,count,(void*)&i) );
continue;
else
printf("Thread Creation Problem");
}*/
ok=1; pthread_join(id,NULL); //count(); fclose(fp); printf("\nVowels = %d",vowels); printf("\nConsonants = %d",consonants); printf("\nDigits = %d",digit); exit(0); } void *count() { int temp; char c; while(ok==0); while(feof(fp)==0) { pthread_mutex_lock(&lock1); c=fgetc(fp); pthread_mutex_unlock(&lock1); temp=toupper(c)-65; if(isalpha(c)) { if(toupper(c)=='A' || toupper(c)=='E' || toupper(c)=='I' || toupper(c)=='O' || toupper(c)=='U') { pthread_mutex_lock(&lock1); vowels++; pthread_mutex_unlock(&lock1); } else { pthread_mutex_lock(&lock1); consonants++; pthread_mutex_unlock(&lock1); } } elseif(temp>-17&&temp<-7) { pthread_mutex_lock(&lock1); digit++; pthread_mutex_unlock(&lock1); } } pthread_exit(NULL); }
  
Share: 



Eleanor Hughes
Eleanor Hughes author of Program to find vowels, constants and digits from a file using threading is from London, United Kingdom.
 
View All Articles

Related Articles and Code:


 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!