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; }
int main(int argc, char **argv) { if(signal(SIGINT, safe_exit) == SIG_ERR) SIGFAIL() if(signal(SIGQUIT, safe_exit) == SIG_ERR) SIGFAIL() if(signal(SIGTERM, safe_exit) == SIG_ERR) SIGFAIL() appname = argv[0]; setconfigdefaults(); if(parseargs(argc, argv) != 0) { return(1); } #ifdef DEBUG (void)printf("name: %s\n", config.name); (void)printf("host: %s\n", config.host); (void)printf("port: %s\n", config.port); (void)printf("limit: %d\n", (int)config.limit); #endif if(createandconnectsocket() != 0) { cleanup(); return(1); } while(rcvmsg() == 0 && quit == 0) { if(processmsg() != 0) { break; } } cleanup(); return(0); }