Hi ,
I have one client and two servers . I need to send messages on two
servers randomly or alternately to balance the load .
I m using SCHED_RR to do that . I am facing following problems :
1. My client is sending the message to the 2nd server only when there
is a failover of first server.
2. when only 2nd server is running from the beginning and 1st server
hasn't started at all , Client gives an error for sending and receiving
bytes .
The Client program is :
int main()
int i;
pthread_t threads[2];
pthread_attr_t attr_1, attr_2;
struct sched_param param_1, param_2;
pthread_attr_init(&attr_1);
pthread_attr_init(&attr_2);
pthread_attr_setinheritsched(&attr_1, PTHREAD_EXPLICIT_SCHED);
pthread_attr_setinheritsched(&attr_2, PTHREAD_EXPLICIT_SCHED);
pthread_attr_setdetachstate(&attr_1, PTHREAD_CREATE_JOINABLE);
pthread_attr_setdetachstate(&attr_2, PTHREAD_CREATE_JOINABLE);
/* set thread priority randomly and scheduling policy SCHED_RR for
socket_1 */
param_1.sched_priority = rand();
pthread_attr_setschedparam(&attr_1, ¶m_1);
pthread_attr_setschedpolicy(&attr_1, SCHED_RR);
pthread_create(&threads[0], &attr_1, send_1, NULL);
/* set thread priority randomly and scheduling policy SCHED_RR for
low thread */
param_2.sched_priority = rand();
pthread_attr_setschedparam(&attr_2, ¶m_2);
pthread_attr_setschedpolicy(&attr_2, SCHED_RR);
pthread_create(&threads[1], &attr_2, send_2, NULL);
for (i = 0; i < 2; i++)
{
pthread_join(threads[i], NULL);
}
printf ("Main(): Waited on 2 threads. Done.\n");
/* Clean up and exit */
pthread_attr_destroy(&attr_1);
pthread_attr_destroy(&attr_2);
pthread_exit(NULL);
}
void send_1(void *arg)
{
socket creation on port 2007
and sending the message to Server at port 2007
}
void send_2(void *arg)
{
socket creation on port 2008
and sending the message to Server at port 2008
}
Can anyne please tell me how load balancing can be achieved using
SCHED_RR. ??
Thanks !
|