LQ Newbie
Registered: Sep 2012
Posts: 9
Original Poster
Rep: 
|
hi..it will be very nice..can u plz explain me something abt thread affinity.
my server code is:-
//////////////////////////////////////////////////////////////////////////////////////////////
//This file's code is serving as SERVER for Multi Threading Programe. Its having two threads//
//data is transferring from main thread to thread1 and then from thread1 to thread2. For //
//fulfilling the purpose of data transferring we are using shared memory. For communicating //
//between two process we are using UDP server. And whichever packet client is sending will //
//be saved to shared memory and used afterwards. //
// //
/////////////////////////////////////////////////////////////////////////////////////////////
#include "/home/torres/Desktop/Pankaj_Exercises/multi_thread/trial_multithread/include/mt_include.h"
int main()
{
cpu_set_t cpuset1;
int i;
key=2;
cl_size=sizeof(struct sockaddr_in);
server.sin_family=AF_INET; //setting server attributes
server.sin_port=htons(7000);
//server.sin_addr.s_addr=inet_addr("127.0.0.1");
server.sin_addr.s_addr=INADDR_ANY;
//printf("in Server\n");
//creating shared memory//
shmid=shmget(key, 40, IPC_CREAT|0666); //Allocating shared memory of 40 bytes.
if(shmid==-1) //shmget returns -1 on failure and zero on success
{
syslog(PRI, "Error in creatin Shared Memory at server side\n");
exit(EXIT_FAILURE);
}
else
{
ptr=(char*)shmat(shmid,NULL,0); //shared memory is pointing by pointer ptr which is a char pointer
syslog(PRI,"Shared Memory is created with %d id\n", shmid);
}
//creating socket//
sockid=socket(AF_INET, SOCK_DGRAM, 0); //an UDP socket is created.
if(sockid==-1) //socket function also returns -1 on failure and socket id on success
{
syslog(PRI,"Problem in creating a socket at server side\n");
exit(EXIT_FAILURE);
}
else
{
int b=bind(sockid, (struct sockaddr*)&server, sizeof(server)); //binding to socket id
if(b==-1)
{
syslog(PRI, "SERVER:Problem with Binding\n");
exit(EXIT_FAILURE);
}
else
{
syslog(PRI, "Socket bounded with local address\n");
printf("Press ! Ctrl+C to disconnect\n");
while(1)
{
ret=recvfrom(sockid, ptr, 40, 0, (struct sockaddr*)&client, &cl_size); //receiving from client
if(ret==-1)
{
syslog(PRI, "Server not receiving\n");
exit(EXIT_FAILURE);
}
else
{
CPU_SET(0,&cpuset1);
pthread_create(&PID1, NULL, thread1, ptr); //creating thread with PID1 and function thread1
pthread_setaffinity_np(PID1, sizeof(cpuset1), &cpuset1);
pthread_join(PID1,(void**)&s); //joining of thread1
}
}
}
}
shmdt(ptr); //detaching the shared memory
return 0; //returning
}
void * thread1(void *data)
{
cpu_set_t cpuset2;
int end, *status;
CPU_SET(1,&cpuset2);
syslog(PRI,"Message from client=%s\n",data);
printf("Message from client :- %s\n", data);
pthread_create(&PID2, NULL, thread2, data);
pthread_setaffinity_np(PID2, sizeof(cpuset2), &cpuset2);
pthread_getaffinity_np(PID2, sizeof(cpuset2), &cpuset2);
printf("affinity=%d\n",cpuset2);
while(1)
{
pthread_join(PID2, (void**)&status); //joining thread 2
}
pthread_exit(&end); //exiting from thread1
}
void * thread2(void *data)
{
int bye;
printf("Message to Client :- ", exBuff);
fflush(stdin); //flushing the input buffer
fgets(send_message,sizeof(send_message),stdin);
strcpy(data,send_message);
sendto(sockid, data, 40, 0, (struct sockaddr*)&client, sizeof(client)); //sending data to client
syslog(PRI, "Message to Client=%s\n",data);
pthread_exit(&bye);
}
client code is :
////////////////////////////////////////-----client.c---//////////////////////////////////////////
// This file's code is serving as a client for our programme. Communication will be started by //
// the client only. First it will send data through shared memory using UDP socket. Then after //
// it will receive by server. This client-server will execute synchronously on different tty's //
// ///////////////////////////////////////////////////////////////////////////////////////////////
#include "/home/torres/Desktop/Pankaj_Exercises/multi_thread/trial_multithread/include/mt_include.h"
int main()
{
int i;
key=2;
cl_size=sizeof(struct sockaddr_in);
pthread_t PID1, PID2; //PID1 and PID2 are thread id's
server.sin_family=AF_INET;
server.sin_port=htons(7000);
server.sin_addr.s_addr=INADDR_ANY;
client.sin_family=AF_INET; //setting client and server attributes
client.sin_port=htons(8000); //htons=host to network short
client.sin_addr.s_addr=INADDR_ANY; //AF_INET used for domain. like internet ipv4
//inet_addr converts into binary format
//creating shared memory//
shmid=shmget(key, 40, IPC_CREAT|0666); //creating shared memory with same key so that client also can user this.
if(shmid==-1)
{
syslog(PRI, "Error in creating Shared Memory at client section\n");
exit(EXIT_FAILURE);
}
else
{
ptr=(char*)shmat(shmid,NULL,0); //accessing shared memory with rd/wr mode.
syslog(PRI, "Shared Memory is Created in client with %d id\n", shmid);
}
//creating socket//
cl_sockid=socket(AF_INET, SOCK_DGRAM, 0); //an UDP socket is creating at client side with cl_sockid is the socket id
if(cl_sockid==-1)
{
syslog(PRI,"Problem in Creating a socket at client side\n");
exit(EXIT_FAILURE);
}
else
{
syslog(PRI, "Socket is created in client\n");
int b=bind(cl_sockid, (const struct sockaddr*)&client, sizeof(client)); //binding to socket
if(b==-1)
{
syslog(PRI, "Problem with bind in client\n");
exit(EXIT_FAILURE);
}
else
{
while(1) //loop will be continued till ctrl+C
{
printf(" Message to server :- ");
fflush(stdin);
fgets(send_message,sizeof(send_message), stdin);
strcpy(ptr, send_message);
ret=sendto(cl_sockid, ptr, 40, 0, (struct sockaddr*)&server, sizeof(server));
if(ret==-1)
{
syslog(PRI, "Server Not Receiving\n");
exit(EXIT_FAILURE);
}
else
{
syslog(PRI, "Message to server=%s\n",ptr);
recvfrom(cl_sockid, ptr, 40, 0, (struct sockaddr*)&server, &serv_size);
printf("Message from Server :- %s",ptr);
}
}
}
}
return 0; //returning
}
|