gpt4 book ai didi

c - 连接共享内存段时出错

转载 作者:行者123 更新时间:2023-11-30 15:03:54 24 4
gpt4 key购买 nike

我有以下代码,我想通过共享内存段在两个进程之间进行通信。我的问题是我在附加内存段时出错,但我不知道为什么。

#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
#include <stdlib.h>
#include "process.h"

int main(int argc, char** argv)
{
/*- Check the command-line arguments -*/
if(argc != 2)
{
printf("\n--- Wrong input at command-line arguments ---\n\n");
printf("--- The program terminate --- \n");
return 1;
}

int N = atoi(argv[1]); // The amount of total threads to be created
int status; // for the wait() function
char* data; // a string to use for shared memory segment

/*- Variables for the shared memory segment -*/
key_t key = 1003; // for the shmget() key argument
int shmid; // to store the returned value from shmget()
int size = 1024; // for the shmget() size argument


/* ************************************************************************************/

pid_t pid = fork(); // create second process

if(pid < 0) // if something going wrong
{
printf("\n\n---- Error in function fork!----\n");
exit(1);
}
else if(pid == 0) // the child process (P)
{
// create the shared memory segment for the P process
shmid = CreateShmSegment(key, size, IPC_CREAT);

if(shmid == -1) // check if creating shared memory return an error
{
printf("\n---Error at creating the memory segment! ---\n");
exit(1);
}

// attach the shared memory segment
data = AttachShmSegment(shmid);

if(data == (char*)-1) // check if attached the shared memory return an error
{
printf("\n---Error at attaching the memory segment! ---\n");
exit(1);
}

char* str = data;
sprintf(str, "testing");

// ChildProcess();
printf("\n** Child process! ** \n");
printf("N = %d\n", N);
printf("write: %s\n",str);

// detach the shared memory segment
if(shmdt(data) == -1) //check for error
{
printf("\n---Error at detaching memory segment! ---\n");
exit(1);
}
}
else // the parent process (C)
{
// create the shared memory segment for the C process
shmid = CreateShmSegment(key, size, IPC_CREAT);

if(shmid == -1) // check if creating shared memory return an error
{
printf("\n---Error at creating the memory segment! ---\n");
exit(1);
}

// attach the shared memory segment
data = AttachShmSegment(shmid);
if(data == (char*)-1) // check if attached the shared memory return an error
{
printf("\n---Error at attaching the memory segment! ---\n");
exit(1);
}

// ParentProcess();
wait(&status);
printf("\n** Parent process! **\n");
printf("N = %d\n",N);
printf("read from segment: %s\n", data);

// detach the shared memory segment
if(shmdt(data) == -1) //check for error
{
printf("\n---Error at detaching memory segment! ---\n");
exit(1);
}

// deallocate the shared memory segment
if(DeallocateShmSegment(shmid) == -1) //check for error
{
printf("\n---Error at destroy memory segment! ---\n");
exit(1);
}
}

return 0;
}

我引用了另外两个文件进行编译。

进程.h:

#ifndef __Processes_H_
#define __Processes_H_
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>

void ChildProcess();
void ParentProcess();
int CreateShmSegment(key_t, size_t, int);
void* AttachShmSegment(int);
int DetachShmSegment(const void*);
int DeallocateShmSegment(int);

#endif

和process.c:

#include "process.h"

void ChildProcess()
{

}


/***************************************************************/

void ParentProcess()
{

}

/****************************************************************/

int CreateShmSegment(key_t key, size_t size, int flag)
{
int id;
id = shmget(key, size, flag);
return id;
}

/*****************************************************************/

void* AttachShmSegment(int id)
{
char* data = NULL;
data = (char*) shmat(id, NULL, 0);
return data;
}

/*****************************************************************/

int DetachShmSegment(const void* addr)
{
return shmdt(addr);
}

/*****************************************************************/

int DeallocateShmSegment(int id)
{
return shmctl(id, IPC_RMID, NULL);
}

我不知道连接内存出了什么问题。我在网上搜索并对 shmat() 的参数进行了一些更改,但我无法解决它。

最佳答案

创建段时没有指定任何模式标志。因此,我希望它的创建对任何人(包括其创建者)都没有读或写访问权限。这本身并没有错误,但非特权进程在具有该模式时附加该段的所有尝试都应该失败,并显示 EACCES

大概,您希望在创建段时在标志中至少包含 S_IRWXU,或者在创建段后使用 shmctl() 来修改段的模式允许所有者进行读写访问。 (请注意,执行权限对于此目的没有意义,因此它相当于使用 S_IRUSR | S_IWUSR。)例如,

    shmid = CreateShmSegment(key, size, IPC_CREAT | S_IRWXU);

您可能想授予更广泛的访问权限。

还要注意的是,先 fork 然后让父进程和子进程创建并附加共享内存段,这很奇怪。附加的共享内存段是通过 fork() 继承的,因此在 fork 之前创建和附加段会更干净。

关于c - 连接共享内存段时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40488594/

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