int main(int argc, char *argv[]) { int val = 0; int num = getpid(); // 连接服务器 printf("%d号消费者开启。正在连接服务器。。。\n", num); int sfd = connect_server(SERVER_IP); char msg[BUF_SIZE]; char type[BUF_SIZE]; printf("连接成功。开始消费。\n"); int t = OPCNTS; while (t--) { sprintf(msg, "%s %d -1\n", CONSUMER, num); sndmsg(sfd, msg, strlen(msg)); rcvmsg(sfd, msg, BUF_SIZE); sscanf(msg, "%s%d%d", type, &num, &val); printf("%d号消费者进程从共享内存区中取出数据%d成功.\n", num, val); usleep(100000); // 等待100毫秒 } // 消费者消费结束标志 sprintf(msg, "%s %d -1\n", END, num); sndmsg(sfd, msg, strlen(msg)); printf("%d号消费者结束。\n", num); close(sfd); return 0; }
/* Name: processmsg Desc: process messages after retrieval from server. this includes handling message overflow (a message has been cut off and will be continued in the next buffer) and messge splitting (buffer contains more than one msg) Args: - Returns: 0 on success, 2 if server requests connection termination, other vals on failure Globals:inbuffer,overflowbuffer,appname,scores,config */ int processmsg(void) { char *unmodifiedbuffer, /* unmodified version of tokbuffer */ *tok[5], /* tokens of current cmd (tokbuffer) */ *save, /* savepoint for strtok */ *search = " \n"; int i, ret = 0; /* return code */ unmodifiedbuffer = strdup(inbuffer); if(unmodifiedbuffer == NULL) { (void)fprintf(stderr, "%s: error allocating memory\n", appname); return(1); } /* split cmd into tokens (we only care - at most - about the first 5 */ tok[0]= strtok_r(inbuffer, search, &save); for (i = 1; i < 5; i++) { tok[i] = strtok_r(NULL, search, &save); } /* parse cmd and take appropriate action */ if(tok[0] == NULL) { (void)fprintf(stderr, "%s: empty token , ignoring...\n", appname); } else if(strcmp(tok[0], CMDHELO) == 0) { if(sndmsg("AUTH %s\n", config.name) != 0) ret = 1; } else if(strcmp(tok[0], CMDTURN) == 0) { if(processmsg_turn(tok[1]) != 0) ret = 1; } else if(strcmp(tok[0], CMDTHRW) == 0) { if(processmsg_throw(tok[1], tok[2], tok[3]) != 0) ret = 1; } else if(strcmp(tok[0], CMDWIN) == 0 || strcmp(tok[0], CMDDEF) == 0) { (void)printf("%s\n", tok[0]); if(sndmsg("BYE %d %d %d\n", scores[2], scores[0], scores[1]) != 0) ret = 1; } else if(strcmp(tok[0], CMDBYE) == 0) { ret = 2; } else if(strcmp(tok[0], CMDERR) == 0) { (void)fprintf(stderr, "%s: server error: %s\n", appname, unmodifiedbuffer); ret = 1; } else { (void)fprintf(stderr, "%s: unrecognized token %s, ignoring...\n", appname, tok[0]); } free(unmodifiedbuffer); return(ret); }
/* Name: processmsg_turn Desc: handle TURN msg received from server Args: ownscore: current value of own score Returns: 0 on success, nonzero on failure Globals:appname,scores */ int processmsg_turn(const char *ownscore) { long int iownscore; char *answer; if(strtoint(ownscore, &iownscore) != 0) { (void)fprintf(stderr, "%s: protocol error\n", appname); return(-1); } /* make sure we are tracking the score correctly */ assert(iownscore == scores[ME]); /* decide whether to save or roll */ answer = (torollornottoroll() == 0) ? CMDSAVE : CMDROLL; if(sndmsg("%s\n", answer) != 0) { return(1); } return(0); }