服务器代码下面的一个字符串转换成共享内存变量。使用信号量共享内存安全写入
客户端代码显示共享内存中可用的字符串。
全码:available in this github link
Server.c
int main(int argc, char *argv[])
{
/* code to create posix shared memory and posix named semaphore */
/* critical section start */
snprintf(shared_msg->content, MAX_MSG_LENGTH, "%s", argv[1]);
/* critical section end */
}
Client.c
int main(int argc, char *argv[])
{
/* code to open posix shared memory and posix named semaphore */
for(i=0; i<20; i++){
/* critical section start */
//operation of semaphore
while(loop < 15){
printf("Type : %d, content : %s\n", shared_msg->type, shared_msg->content);
sleep(1);
loop++;
}
/* Critical section end */
loop = 0;
printf("loop %d finished\n", i);
}
}
如何使用上面的代码(等待和后)POSIX信号,以达到以下要求
- 客户端启动时必须显示共享内存数据。一旦内部while循环完成后,只有客户端释放共享内存。
- 如果服务器启动并尝试将数据写入共享内存,当客户端在while循环运行时,信号量不会写入允许写入,直到客户端释放信号量。当客户端释放信号
感谢
你试过'男人sem_overview'上命令行? –
是的。我仍然有些挣扎。 – sujin
什么客户端服务器通过共享内存进行通信?为什么不使用管道或套筒? –