从共享内存中读取字符串 C++ POSIX
Read string from shared memory C++ POSIX
在我的另一个问题中,一位用户帮助我通过共享内存发送字符串。但是当我尝试接收此数据时,程序显示 Core dumped
。我试试这个代码:
key_t lineKey = ftok("/tmp", '1');
int sharedLine = shmget(lineKey, sizeof(std::string), IPC_CREAT | 0666);
std::string *line = (std::string *)shmat(sharedLine, NULL, 0);
sem_t *reader1_sem;
reader1_sem = sem_open("reader1_sem", O_CREAT, 0666, 0);
while (1) {
sem_wait(reader1_sem);
std::cout << (*line);
}
我也尝试将其重写为 char *
,但它也不起作用 (char *line = (char *)shmat(...);
)。
在我的另一个问题中,一位用户帮助我通过共享内存发送字符串。但是当我尝试接收此数据时,程序显示 Core dumped
。我试试这个代码:
key_t lineKey = ftok("/tmp", '1');
int sharedLine = shmget(lineKey, sizeof(std::string), IPC_CREAT | 0666);
std::string *line = (std::string *)shmat(sharedLine, NULL, 0);
sem_t *reader1_sem;
reader1_sem = sem_open("reader1_sem", O_CREAT, 0666, 0);
while (1) {
sem_wait(reader1_sem);
std::cout << (*line);
}
我也尝试将其重写为 char *
,但它也不起作用 (char *line = (char *)shmat(...);
)。