C Programming Tutorial

 
 
 
 
# 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);
                }
            }
            else if(temp>-17&&temp<-7) 
            {
                pthread_mutex_lock(&lock1);
                digit++;
                pthread_mutex_unlock(&lock1);
            } 
    }
    pthread_exit(NULL);
}

 
 
Didn't find what you were looking for? Find more on Program to find vowels, constants and digits from a file using threading