sam_uncounted 0 Newbie Poster

I am trying to divide a string in 3 parts and assign to each part to one thread. And luckily I am able to do so. The purpose of the program is to match a character and each thread tries to match that charcter and return results.The main problem comes in while uses mutex lock I am updating a common variable among all the threads a the result should be printed from the main program.

#include<pthread.h>
#include<stdio.h>
#include<string.h>
char* arry[512];//="I am good boy and I am strong";
char match_ch,*d;
#define lenght 3
#define NUM_THREADS 3
int str(void *len);
int count;

pthread_mutex_t mutexsum;


int main()
{
    pthread_t threadid[NUM_THREADS];    
    pthread_attr_t attr;
    void *status;
    int i=1,len,len1,pos=0;
    while(1)
    {
    printf("Enter the String for lenght 40 =");
    scanf("%s",&arry);
    len=strlen(arry);
    if(len<40)
    continue;
    else
    break;
    }

    len=strlen(arry);
    d=(char *)malloc(len);
    printf("%d",len);
    len1=len/3;
    printf("\nlen1   %d",len1);
    printf ("\nEnter the letter to be matched = ");
    scanf("%c",&match_ch);
    pthread_mutex_init(&mutexsum, NULL);

    pthread_attr_init(&attr);
    pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);



    for(i=0;i<NUM_THREADS;i++)
    {
        if(i==0)
        {
        //pos=pos+i*len1;
        pos=0;
        strncpy(d, arry, 9);
        //printf("\n%s",d);
        printf("\n%d\n",len1);
        }
        else if(i==1)                                                                                               
        {
    //  xpos=pos+i*len1;
        pos=len1;
        strncpy(d, arry+pos, len1);
        printf("\n%s",d);
        printf("\n%d\n",len1);

        }
        else if(i==2)
        {
        pos=2*len;
        len1=len-2*len1;
        strncpy(d, arry+pos, len1);
        //printf("\n%d",len1);
        }
    pthread_create(&threadid[i],NULL,str,(void *)len1);
    printf("\nCreating thread %d\n", i);
    //printf("\nCreating thread %d\n",threadid[i]);
    }
    pthread_attr_destroy(&attr);
    for(i=0; i<NUM_THREADS; i++)
        {
      pthread_join(threadid[i],&status);
    }

    printf("count  %d",count);  
    pthread_mutex_destroy(&mutexsum);
    pthread_exit(NULL);
    free(d);    
}

int str(void *len1) 
{

  //  len =strlen(arry);
    int count,i;
    count=0;
   // printf("\nlen1 %d and %d",len1);
    for(i=0;i<len1;i++)
    {
    //printf("%s",d);
    if(match_ch == d[i])
    {
    pthread_mutex_lock (&mutexsum);
     count++;
    pthread_mutex_unlock (&mutexsum);
    }
    }
    printf("\n The number of times the letter which is to be matched in a entered string is %d",count);
}




//
//int extract(char *s,char *d,int pos,int len)
//{
//s=s+(pos-1);
//while(s!=s+len)
//{
//*d=*s;
//s++;
//d++;
//}
//*d='\0';
//return d;
//}
//thread_data_array[i].thread_id = i;
//printf ("\nThread ID  = %d",i);
//
////thread_data_array[i].match_t = match_ch;
////printf ("\nEnter the letter to be matched = ");
//
//printf ("\nEnter the letter to be matched = ");
//
//thread_data_array[i].message = d; 
//pthread_create(&threadid[i],NULL,str,(void *)&thread_data_array[i]);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.