",..." />
void* client(void* data) {
while (true) {
char message[MSGLEN];
sprintf(message, "%d:\t[%07llu]->", pthread_self(), mark());
MsgSend((int)data, message, strlen(message) + 1, message, MSGLEN);
sprintf(message + strlen(message), "[%07llu]", mark());
cout << message << endl;
static unsigned int seed = 0;
delay(numclient*(((long)rand_r(&seed ) * TEMP / RAND_MAX) + TEMP));
// имитация вычислений...
}
return NULL;
}
int main(int argc, char** argv) {
// 1-й параметр - число потоков клиентов:
if (argc > 1 && atoi(argv[1]) > 0)
numclient = atoi(argv[1]);
tb = ClockCycles();
int chid = ChannelCreate(0);
if (pthread_create(NULL, NULL, server, (void*)chid) != EOK)
perror("server create"), exit(EXIT_FAILURE);
for (int i = 0; i < numclient; i++)
if (pthread_create(NULL, NULL, client,
(void*)ConnectAttach(0, 0, chid, _NTO_SIDE_CHANNEL, 0)) != EOK)
perror("client create"), exit(EXIT_FAILURE);
sigpause(SIGINT);
return EXIT_SUCCESS;
}
Все происходит в рамках единого процесса:
• Создается единый поток сервера, ожидающий сообщений от клиентов и отвечающий на них.