C Programming Tutorial

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

void *count();
int count1[27],ok;
FILE *fp;
pthread_t id;
pthread_mutex_t lock1;
main()
{
    int i,j,n_thread;
    fp=fopen(\"ass3.c\",\"r\");
    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);
    for(i=0,j=65;i<26;i++,j++)
    {
        if(count1[i]!=0)
            printf(\"%c Is Occurc at %d times\\n\",j,count1[i]);
    }
    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))
             count1[temp]++;

    }
    pthread_exit(NULL);
}
/* OUTPUT:
   -------
Enter the NO of Thread 3
A Is Occurc at 37 times
B Is Occurc at 1 times
C Is Occurc at 41 times
D Is Occurc at 32 times
E Is Occurc at 64 times
F Is Occurc at 31 times
G Is Occurc at 3 times
H Is Occurc at 28 times
I Is Occurc at 61 times
J Is Occurc at 5 times
K Is Occurc at 11 times
L Is Occurc at 37 times
M Is Occurc at 11 times
N Is Occurc at 52 times
O Is Occurc at 46 times
P Is Occurc at 30 times
R Is Occurc at 51 times
S Is Occurc at 13 times
T Is Occurc at 62 times
U Is Occurc at 28 times
V Is Occurc at 3 times
W Is Occurc at 2 times
X Is Occurc at 8 times
*/

 
 
Didn't find what you were looking for? Find more on PROGRAM to Search Occurances of Charaters from File in Parallel