Can someone explain why output is printing only 4 times

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jinnejeevansai
    New Member
    • Nov 2014
    • 48

    Can someone explain why output is printing only 4 times

    I was writing a code to create posix threads print function is printing p,q for 4 loops but it is not printing for fifth loop but the loop is called. Can someone explain the reason.


    #include<stdio. h>
    #include<pthrea d.h>
    #define max 5
    struct data{
    int a;
    int b;
    };
    struct data s[max];
    void *print(void *t)
    {
    int p,q;
    struct data *thread;
    thread=(struct data *)t;
    p=thread->a;
    q=thread->b;
    printf("%d %d\n",p,q);
    }
    int main()
    {
    pthread_t th[max];
    char m[100];
    int rc,x=45,y=56,i;
    for(i=0;i<max;i ++)
    {
    scanf("%s",m);
    s[i].a=x;
    s[i].b=y;
    //printf("%d",s[i].a);
    rc=pthread_crea te(&th[i],NULL,print,(vo id *)&s[i]);
    if(rc)
    printf("error:% d",rc);
    }
    }
  • mikeyit87
    New Member
    • Aug 2015
    • 12

    #2
    Because your using less than 5 and so it only goes to 4. So make it less than or equal to 5.

    Comment

    • jinnejeevansai
      New Member
      • Nov 2014
      • 48

      #3
      but i was starting from 0

      Comment

      • mikeyit87
        New Member
        • Aug 2015
        • 12

        #4
        it includes 5 if you use =<

        Comment

        • mikeyit87
          New Member
          • Aug 2015
          • 12

          #5
          an array you start from 0 but and integer starts at 1...like counting your fingers

          Comment

          • jinnejeevansai
            New Member
            • Nov 2014
            • 48

            #6
            output for =<5 is
            g
            45 56
            l
            45 56
            j
            45 56
            k
            45 56
            l
            45 56
            h

            print is called for 6th time but why is no output printing.could you explain please.

            Comment

            • mikeyit87
              New Member
              • Aug 2015
              • 12

              #7
              Because you have 6 letters but you only looped 5 times. When you use arrays it counts 0 as 1 so when we set =< it went up to 5. 012345 for the array. But the loop for int starts at 1 excluding the 0.

              Comment

              • kiseitai2
                New Member
                • Jul 2007
                • 93

                #8
                All of the responses are wrong! The original code is correct for a program that runs in a single thread. I suspect the problem lies in lack of synchronization between the main thread and the threads you created. The following line is called 5 times (0, 1, 2, 3, 4):
                Code:
                //printf("%d",s[i].a);
                rc=pthread_create(&th[i],NULL,print,(void *)&s[i]);
                The problem is probably when the 5th thread is created. It is possible that main quits before the 5th thread has a chance to execute properly. When main exits, you have two possibilities, either the running thread is terminated unexpectedly (I think if it is a child thread but I could be wrong) or the variables you were working with are deleted and become invalid. In the former case, the thread doesn't get to execute at all. In the ladder case, the thread executes but printing from a NULL pointer/ pointer to random invalid memory can cause the thread to crash or to print nothing.

                The solution to this problem is to add synchronization . Make a global int variable initialized to 0, add a line at the end of the print function that increments this variable, add a while loop at the end of main that checks for whether this variable == max (if it does, break from the loop and let main end/ exit).

                I hope my explanation helps.

                Comment

                • jinnejeevansai
                  New Member
                  • Nov 2014
                  • 48

                  #9
                  by debugging i got to know that the loop was running 5 times for i<5 but the problem is arising at thread=(struct data *)t; in last loop,but i cannot understand why Can someone analyse please

                  Comment

                  • mikeyit87
                    New Member
                    • Aug 2015
                    • 12

                    #10
                    i < 5 does not run 5 times. means i will always be less than 5.

                    Comment

                    • kiseitai2
                      New Member
                      • Jul 2007
                      • 93

                      #11
                      The loop runs 5 times. For loops don't increment until after the execution of the body. As far as I know, 0,1,2,3,4 = 5 runs (I could be wrong for I am not a math major). I have worked with parallel programming and I am 90% sure main quits and deletes s before the 5th iteration can use it. I explained it in my previous reply. Please, read it carefully.

                      Original code:
                      Code:
                      for(i=[B]0[/B];i<max;i++) <-I think I see a 0, maybe I am blind...
                      {
                      scanf("%s",m);
                      s[i].a=x;
                      s[i].b=y;
                      //printf("%d",s[i].a);
                      rc=pthread_create(&th[i],NULL,print,(void *)&s[i]);
                      if(rc)
                      printf("error:%d",rc);
                      }

                      Comment

                      Working...