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.