gpt4 book ai didi

c - 为什么共享内存段只检索最后输入的数据。

转载 作者:太空宇宙 更新时间:2023-11-04 08:46:13 26 4
gpt4 key购买 nike

我有一个服务器和客户端的示例代码。但是当我用第一个输入数据运行客户端时,服务器没有检测到当我第二次运行客户端时服务器打印第一个输入数据。请有人解释我的这种行为代码。

1.服务器.c

#include"share.h"
main()
{
int ret=0;
int shmid=0;
void *shmaddr=0;
int running=1;
shm_buf *shmstruct=NULL;
shmid=shminit();
if(ret== EXIT_FAILURE)
return;
if((shmaddr=shmattach(shmid)) == NULL)
return;
shmstruct=(shm_buf*)shmaddr;
while(running)
{
if( shmstruct->flag ==0)
{
printf("waiting for client......\n");
}
else
{
printf("%s\n", shmstruct->buffer);
if(strncmp(shmstruct->buffer,"end",3)==0)
{
break;
}
shmstruct->flag=0;
}
sleep(2);
}
ret=shmdetach(shmaddr);

if(ret== EXIT_FAILURE)
return;

ret=shmdel(shmid);
if(ret== EXIT_FAILURE)
return;

}

2.客户端.c

#include"share.h"
main()
{
int ret=0;
int shmid=0;
void *shmaddr=0;
shm_buf *shmstruct=NULL;
shmid=shminit();
if(ret== EXIT_FAILURE)
return;
if((shmaddr=shmattach(shmid)) == NULL)
return;
shmstruct=(shm_buf*)shmaddr;
if( shmstruct->flag ==0)
{
shmstruct->flag=1;
printf("Enter meeeage:");
fgets( shmstruct->buffer,sizeof(shm_buf),stdin);
}
}

3.分享.h

#include<stdio.h>
#include<string.h>
#include<sys/shm.h>
#include<sys/ipc.h>
#include<error.h>
#include<errno.h>
#define EXIT_FAILURE -1
#define EXIT_SUCCESS 0
#define TRUE 1
#define FALSE 0
struct share
{
int flag;
char buffer[BUFSIZ];
};
typedef struct share shm_buf;
int shminit()
{
int shmid=0;
key_t key=ftok("/home/rajdhar/sample/shm/share.h",'A');
if(key<0)
{
printf("ftok failed:%s\n",strerror(errno));
return EXIT_FAILURE;
}
printf("key has been created successfuuly\n");

shmid=shmget(key,sizeof(shm_buf),IPC_CREAT|0666);
if(shmid<0)
{
printf("shmget failed:%s\n",strerror(errno));
return EXIT_FAILURE;
}
printf("%d share memory has been created successfully\n",shmid);
return shmid;
}

void *shmattach(int shmid)
{
void *shmaddr=NULL;
shmaddr=shmat(shmid,(void *)0,0);
if(shmaddr == NULL)
{
printf("shmat failed:%s\n",strerror(errno));
return NULL;
}
printf("memory has been attached with process successfulyy\n");
return shmaddr;
}

int shmdetach(void *shmaddr)
{
int ret = FALSE;
ret=shmdt(shmaddr);
if(ret<0)
{
printf("shmdt failed:%s\n",strerror(errno));
return EXIT_FAILURE;
}
printf("share memory has been detached successfully\n");
return EXIT_SUCCESS;
}

int shmdel(int shmid)
{
int ret = FALSE;
ret=shmctl(shmid,IPC_RMID,0);
if(ret<0)
{
printf("shmdt failed:%s\n",strerror(errno));
return EXIT_FAILURE;
}
printf("Share memory has been deleted properly\n");
return TRUE;
}

例子:-1->./服务器2->./客户输入消息: Hello World 服务器不会打印任何东西。3-> ./客户输入信息:我在世界上。服务器打印“hello world”

最佳答案

在客户端你有这个:

shmstruct->flag=1;
printf("Enter meeeage:");
fgets( shmstruct->buffer,sizeof(shm_buf),stdin);

在数据被读入 shmstruct->buffer 之前通知服务器。

只有当它已将数据读入 shmstruct->buffer 时,您才应该将 shmstruct->flag 设置为 1:

printf("Enter meeeage:");
fgets( shmstruct->buffer,sizeof(shm_buf),stdin);
shmstruct->flag=1;

在生产代码中,您可能喜欢使用进程共享互斥锁和条件变量来保护您的共享状态免受损坏,并通知等待进程状态已更改。

关于c - 为什么共享内存段只检索最后输入的数据。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21585609/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com