/* This is a client using System V IPC */ #include #include #include #include #include #include #include #include #include // All globals that every thread should be able to see struct msgbufl { int mtype; char mtext[200]; }; pthread_mutex_t Screen_Lock; int mymsg; void *CommandSender(void *arg); #define COMMAND 1 #define RESPONSE 2 #define COMMAND_LEN 200 int main(int argc, char ** argv) { pthread_t commsender; char *buf; int myuid; myuid = getuid(); //Initialize the mutex that protects the output pthread_mutex_init(&Screen_Lock, NULL); pthread_mutex_lock(&Screen_Lock); cout<<"Welcome to the auction client!"<"); pthread_mutex_unlock(&Screen_Lock); cin.getline(Next_Input,COMMAND_LEN-1,'\n'); if(strlen(Next_Input)>0) { buf =(char *)malloc(strlen(Next_Input)+1); strcpy(buf, Next_Input); pthread_create(&commsender,NULL,CommandSender,(void *)buf); pthread_detach(commsender); } } exit(0); } void *CommandSender(void *arg) { char *buf=(char *)arg; struct msgbufl *msg_tosend; int msgsize, ret; //Create a buffer to store the response from dbserver char command_message[50]; char response_message[50]; msg_tosend =(struct msgbufl *)malloc(sizeof(struct msgbufl)); msgsize = strlen(buf); strncpy(msg_tosend->mtext, buf, msgsize); msg_tosend->mtype = COMMAND; // sending command to db server ret = msgsnd(mymsg,msg_tosend,msgsize,MSG_NOERROR); if(ret<0) { perror("client msgsnd error!"); free(buf); free(msg_tosend); return NULL; } // receiving and printing the response from db server free ( buf); free ( msg_tosend ); return NULL; }