Thread: multithreading program help?

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    10

    multithreading program help?

    this is my problem....................

    1. Within main(), read the contents of a file and store in a

    single char array, compute the array length

    The file name is got as a command line argument, use some

    small file for 1000 – 2000 characters.

    2. Create a message Queue, for use by the threads that will be

    spawned below.

    3. Now, spawn 11 threads, which will do the below activites.

    – thread creation

    1. the first 10 threads will read 1/10 th of the file content

    from the array - use synchronization for this - mutex

    read the 1/10th of the array and store in a local array

    after reading the array, they will convert all upper case chars

    to lower case – use toupper and all lower case chars to

    upper case – use tolower

    After converting to upper case, the modified local array

    should be written into a message queue. – message queue

    2. The 11th thread, will continuously poll for messages on

    the queue, and whenever a message comes,it will read the

    message and append it into the file "output.txt".

    This thread should write "All thread processing over",

    when all the 10 processing threads have finished processing

    the data, and then return. – use a semaphore for this

    4. The main program should exit when the 11th thread

    (which writes into the file) completes – thread join

    i wrote a code for this

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<pthread.h>
    #include<sys/types.h>
    #include<sys/ipc.h>
    #include<sys/msg.h>
    #include<sys/sem.h>
    #include<semaphore.h>
    
    #define NUM_CHARS 2000
    #define MSGKEY 85
    #define SEMKEY 1492
    
    char buffer[2000];
    char a[300];
    int offset = 0;
    int count = 0;
    int semset_id;
    struct sembuf sem_op;
    
    union semun{
            int val;
            struct semid_ds *buf;
            unsigned short *array;
    };
    
    
    struct msgbuf {
            long type;
            char *temp;
    }snd,rcv;
    
    pthread_mutex_t lock1 = PTHREAD_MUTEX_INITIALIZER;
    
    void *function1()
    {
    //      msg snd;
    //      msg rcv;
            int i;
            int j=0;
            FILE *fopen(),*output;
            char *outputfile="output.txt";
            char *b;
            int local_count;
            int msgid,x,y,k;
            snd.type=1;
    
            pthread_mutex_lock(&lock1);
            local_count = strlen(buffer)/10;
            pthread_mutex_unlock(&lock1);
    //printf("%s",buffer);
    
            b = (char *)malloc(local_count);
            snd.temp = (char *)malloc(local_count);
            printf("thread number: %ld\n",pthread_self());
            for(i=offset;i<(local_count+offset);i++)
            {
               if((int)buffer[i]>64&&(int)buffer[i]<91)
                    {
    
                       a[i]=tolower(buffer[i]);
                       b[j]=tolower(buffer[i]);
    
            //         msg.buff[i]=a[i];
                    }
               if((int)buffer[i]>96&&(int)buffer[i]<123)
                    {
                       a[i]=toupper(buffer[i]);
                       b[j]=toupper(buffer[i]);
                     //         msg.buff[i]=a[i];
                    }
               if(buffer[i]=='\n')
                    {
                       a[i]='\n';
                       b[j]='\n';
            //           msg.buff[i]=a[i];
                    }
               j++;
            }
    
            offset = local_count+offset;
            memcpy(snd.temp,b,local_count);
            printf("temp array:%s",snd.temp);
            free(b);
            printf("%d\n",strlen(a));
    
    //      for(j=0;j<=(strlen(a));j++)
    //      {
    //         msg.buff[i]=a[i];
    //      }
            msgid = msgget(MSGKEY, 0666|IPC_CREAT);
            printf("msgid is :%d\n",msgid);
            x = msgsnd(msgid,&snd,sizeof(snd),0);
            if (x==-1)
            printf("message sending failed \n");
            else
            output=fopen(outputfile,"a");
      //       fwrite(&temp,sizeof(temp),strlen(a),output);
     //       fclose(output);
    
            printf("value returned:%d \n",x);
    //      free(snd.temp);
            y = msgrcv(msgid,&rcv,sizeof(rcv),1,0);
            if (y==-1)
            printf("message receiving failed \n");
            else
            {
            printf("no of bytes received = %d \n",y);
    //      printf("the message received is %s\n",rcv.temp[0]);
            }
    //      msgctl(msgid,IPC_RMID,0);
            printf("the contents of temporary array %s\n",a);
            fflush(stdout);
    
            output = fopen(outputfile,"a");
            fwrite(&rcv,sizeof(rcv),50,output);
            fclose(output);
    
            sem_op.sem_num = 0;
            sem_op.sem_op =1;
            sem_op.sem_flg = 0;
            semop(semset_id,&sem_op,1);
    
            pthread_exit(NULL);
    //      pthread_mutex_unlock(&lock1);
    }
    
    void *function()
    {
            sem_op.sem_num = 0;
            sem_op.sem_op = -10;
            sem_op.sem_flg = 0;
            semop(semset_id,&sem_op,1);
            printf("All threads processing over..");
            pthread_exit(NULL);
    }
    //struct msgbuf
    //{
    //      long type;
    //      char buff[2000];
    //};
    int main()
    {
            FILE *fpopen(),*fp;
            int i,j;
            int x[300];
            int y[300];
            char ch;
            char filename[50];
            union semun sem_val;
            void *ret_val;
            int pid;
            unsigned int len;
            pthread_t thread[10],thread11;
    
            printf("Enter the file to be displayed:");
            gets(filename);
    
            fp = fopen(filename,"r");
    
            while(fp==NULL)
            {
               printf("file %s doesnot exist\n",filename);
               printf("Enter the correct file name:");
               gets(filename);
               fp = fopen(filename,"r");
            }
    
    
            for(i=0;(i<NUM_CHARS)&&(feof(fp)==0);i++)
            {
               ch = fgetc(fp);
               buffer[i] = ch;
            }
    
            len = strlen(buffer);
            printf("number of characters in the array is %u\n",len);
    //      printf("contents of the file are %s \n",buffer);
            fclose(fp);
    
            semset_id = semget(IPC_PRIVATE,1,0600);
            sem_val.val = 0;
            semctl(semset_id,0,SETVAL,sem_val);
            for(j=0;j<10;j++)
            {
              pthread_create(&thread[j],NULL,&function1,NULL);
            }
            pthread_create(&thread11,NULL,function,NULL);
    
    //      for(j=0;j<10;j++)
    //      pthread_join(thread[j],&ret_val);
            pthread_join(thread11,NULL);
    //      fclose(output);
    }

    but there is some problem with the code while appending its showing some garbage value along with the strings.......................


    i want the contents of output file to be same as that of input file and also i am trying to use the above concepts threads,messagequeues,semaphores


    thanx for help in advance

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Code:
    for(i=0;(i<NUM_CHARS)&&(feof(fp)==0);i++)
            {
               ch = fgetc(fp);
               buffer[i] = ch;
            }
    
            len = strlen(buffer);
    1. you check for the eof before reading character. You should check after you tried to read before you assigning it in the array. Or just check the return value of the fgetc
    2. strlen work only for null terminated strings - you didn't put null character into your array
    3. you do not need strlen here - because you know what last character was put in the array
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    10
    i need help in my output .......

  4. #4
    Registered User
    Join Date
    Nov 2006
    Posts
    10
    move this thread to linux programming

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Erm, you need to work on your basic C skills before you make things 1000% more difficult for yourself by using threads.

    > FILE *fpopen(),*fp;
    what's fpopen() ?

    > gets(filename);
    Stand back!, she's gonna blow, the engines cannae tek it cap'tn

    > fwrite(&rcv,sizeof(rcv),50,output);
    You've only got one of them, not 50

    Prototype your ideas using a single thread, and a single regular buffer.
    Then move than memory to shared memory
    Then add threads
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Nov 2006
    Posts
    10
    hi sl4nted..... help in this multithreading program ....... all are finding faults in c no one is helping in the ipc concepts..........as they didnt know
    Last edited by pthread; 11-23-2006 at 02:42 AM.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Because your program is like a house built on sand.

    I see little point in explaining the points of shared memory or threads to someone who's yet to master stdio.

    What's next, "I've done a thread program, now to write an OS"
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    Dec 2006
    Posts
    28

    hi

    salem is correct ,u will need to brush up your c skills ,then move to multithreading.It will help to be confortable in multithreading.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  2. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM