Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
СРВ_12_22_01_Механизмы_синхронизации.pptx
Скачиваний:
4
Добавлен:
20.06.2023
Размер:
163.67 Кб
Скачать

smsg

 

chid = ChannelCreate

smsg

 

 

 

 

Клиент

Канал

Сервер

rmsg

coid = ConnectAttach

rmsg

int ChannelCreate(unsigned flags);

int ConnectAttach(int nd, pid_t pid, int chid, unsigned index, int flags); MsgSend(int coid, void*smsg, int sbytes, void*rmsg, int rbytes); MsgReceive(int chid, void rmsg, int rbytes, struct _msg_info *info) MsgReply(int rcvid, int status, void*smsg, int sbytes);

Клиент

stc = MsgSend(coid, smsg, sbytes, rmsg, rbytes)

Сервер

1

4

3

 

 

 

 

 

rcvid = MsgReceive(chid, rmsg, rbytes, NULL) MsgReply(rcvid, sts, smsg, sbytes) 2

12. Механизмы синхронизации

2015 v.01

21

Пример - Клиент

int chid; pthread_barrier_t barrier;

void* Client(void* args){ int coid;

char *smsg = "Client sent ..."; char rmsg[200];

int msg_result; pthread_barrier_wait(&barrier);

coid = ConnectAttach(0, getpid(), chid, 0, 0); //Client runs

 

int i;

 

for(i=1; i<=5; i++) {

 

printf("++client call MsgSend i=%d\n", i);

 

msg_result = MsgSend(coid, smsg, strlen(smsg)+1, rmsg,

sizeof(rmsg));

printf("++client msg_result = %d, coid = %d\n", msg_result, coid); printf("++client This reply was received \"%s\"\n", rmsg); sleep(2);

}

return EXIT_SUCCESS;

}

12. Механизмы синхронизации

2015 v.01

22

Пример - Сервер

int main(int argc, char *argv[]) { int rcvid;

char message[512]; pthread_t client;

pthread_barrier_init(&barrier, NULL, 2);

chid = ChannelCreate(0);

pthread_create(&client, NULL, &Client, NULL); //Client created pthread_barrier_wait(&barrier);

printf("--server Server started ...\n"); while(1) {

printf("--server calls MsgReceive\n");

rcvid = MsgReceive(chid, message, sizeof(message), NULL); printf("--server End MsgReceive, rcvid=%d received mess %s\n", rcvid,

message);

strcpy(message, "This is reply"); MsgReply(rcvid, 0, message, sizeof(message));

}

return EXIT_SUCCESS;

}

12. Механизмы синхронизации

2015 v.01

23