2

I am working on client server code where a client has two threads running; I want both these threads to run continuously for 60 sec. However, I am facing two problems.

Firstly, when I am running the for loop in main(), the retail_thread() is generating the same number it should have generated random number instead, as bulk_thread() is generating. Secondly, I am not able to get the idea of running the for loop exactly 60 sec.

As it is a homework problem I will really appreciate any hints or assistance if not the exact solution. Please ignore typos if any.

int main(int argc, char *argv[]) {

    int pt,i;
    pthread_t thread;
    /* n a very large number */
    /* run below code for 60 seconds */

    for(i=0;i<n;i++)
    {
        pt = pthread_create(&thread, NULL, retail_thread, (void*) NULL);
        bulk_thread(NULL);
    }
}

void* retail_thread(void* ){
    srand(time(NULL));                       
    int order_size = rand()%20 + 1;         
    printf("in retail \n ");
    sendtoserver_R(RETAIL_PORT,order_size);
    int wait_time = 100 + (5*order_size);
    printf("Retail thread order = %d and execution fully completed \n\n",order_size);
}

void* bulk_thread(void* ){
    srand(time(NULL));                      
    int order_size = rand()%90 + 10;        
    printf("in bulk \n");
    int wait_time = 100 + (5*order_size);
    sendtoserver_B(BULK_PORT,order_size);

    printf("Bulk thread order = %d and execution fully completed \n\n",order_size);
}

sendtoserver() is just for creating socket and sending data to server.

Bart
  • 19,692
  • 7
  • 68
  • 77
Vbp
  • 1,912
  • 1
  • 22
  • 30
  • What exactly do you want to happen after 60s? Beware there might several threads be running at that moment, doing different things. – alk Mar 16 '13 at 10:49
  • @Bart I will keep that in mind for next time. Thank you!! – Vbp Mar 16 '13 at 10:50
  • @alk I just want to count the number of orders server has received – Vbp Mar 16 '13 at 10:51
  • "*... where a client has two threads running ...*" your code could easily produces more then two threads running at a time. – alk Mar 16 '13 at 10:54
  • What the server received you should better count on the server side. Did you mean to count the number of orders **sent to** the server? – alk Mar 16 '13 at 10:57
  • @alk Yes I mean the numbers of order sent to server that I am handling in a separate function I appreciate your time and assistance actually counting is just a small part of the problem, I will comment back if i need any help. Thank you!! – Vbp Mar 16 '13 at 11:03

1 Answers1

3

Well, I think the first problem is:

// Seed the RNG once, at the start of the program
srand(time(NULL));  

void* retail_thread(void* ){
    //srand(time(NULL));                       
    int order_size = rand()%20 + 1;         
    printf("in retail \n ");
    sendtoserver_R(RETAIL_PORT,order_size);
    int wait_time = 100 + (5*order_size);
    printf("Retail thread order = %d and execution fully completed \n\n",order_size);
  }

[EDITED] To run a thread 60 sec you can try this:

time_t end = time(NULL) + 60;
while (time(NULL) <= end)
{
    … // do something
}
Leo Chapiro
  • 13,678
  • 8
  • 61
  • 92
  • please correct me if I am wrong, But rand() is not thread-safe if I seed it only once I may get undefined behavior or segfault. – Vbp Mar 16 '13 at 10:33
  • Take a look at this answer please, it's exactly your problem: http://stackoverflow.com/a/6161352/1758762 – Leo Chapiro Mar 16 '13 at 10:37
  • Thank you so much, I did seed once in main() and now retail_thread() is generating random numbers could you please assist me with the second problem where in I have to run the "for loop" exactly 60 seconds – Vbp Mar 16 '13 at 10:42
  • I accepted your answer Thank you! so much I was trying so hard figuring out where I am going wrong. Appreciate your time!! – Vbp Mar 16 '13 at 10:58
  • This might be nitpicking, depending on the use case, but as the OP mentions *exactly 60s* your proposal does not really work, as it will continue "*... doing something ...* after the 60th second. Even if you change `<= end` to be `< end` this would not help as the loop might break before 60s have elapsed. So as the use case is not 60.0s a possible way to go would be to use a clock providing a higher resolution than `time()` does. – alk Mar 16 '13 at 11:03