/* This is a server using System V IPC */ #include #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; void *Worker(void *arg); #define COMMAND 1 #define RESPONSE 2 #define COMMAND_LEN 200 struct Wrapper { int rcvsize; struct msgbufl *msgp; }; int main(int argc, char **argv) { int ret, myuid; struct Wrapper *mywrapper; struct msgbufl *msg_rcvd; pthread_t worker_id; myuid = getuid(); //Initialize the mutex that protects the output and PVM send/receive pthread_mutex_init(&Screen_Lock, NULL); pthread_mutex_lock(&Screen_Lock); printf("Welcome to the auction database manager !\n"); pthread_mutex_unlock(&Screen_Lock); // int mymsg = msgget(myuid, (IPC_CREAT|IPC_EXCL|0400|0200)); int mymsg = msgget(myuid, (IPC_CREAT|0400|0200)); if( mymsg<0) { perror("msgget error!"); exit(1); } printf("mymsg id = %d\n", mymsg); while(1) { printf("waiting for new message..\n"); msg_rcvd = (struct msgbufl *)malloc(sizeof(struct msgbufl)); ret = msgrcv(mymsg, msg_rcvd, COMMAND_LEN, COMMAND, MSG_NOERROR); if( ret<0 ) { perror("msgrcv error!"); free(msg_rcvd); if(msgctl(mymsg, IPC_RMID, NULL)<0) perror("server: msgctl error!!"); exit(0); } mywrapper = (struct Wrapper *)malloc(sizeof(struct Wrapper)); mywrapper->rcvsize = ret; mywrapper->msgp = msg_rcvd; pthread_create(&worker_id, NULL, Worker, (void *)mywrapper); } exit(0); } void *Worker(void *arg) { struct Wrapper *mywrapper=(struct Wrapper*)arg; char response[50]; int msgsize = mywrapper->rcvsize; pthread_mutex_lock(&Screen_Lock); printf("server received \" %s \"from t%d.\n", mywrapper->msgp->mtext,msgsize); pthread_mutex_unlock(&Screen_Lock); strcpy(response, "Server Response OK"); free(mywrapper); return NULL; }