- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在运行代码时遇到问题。我的 shmat 失败并打印权限被拒绝。我在谷歌上搜索了如何解决它,但我不能。我的代码如下:
#include <stdio.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#define ERROR -1
int main ( int argc, char *argv[] ) {
int shmid,key=50;
int *val;
int *x;
int rw = -1;
// 0 for write and 1 for read
shmid = shmget ( key, sizeof( int ), IPC_CREAT );
if ( shmid == -1 ) {
perror ( "Error in shmget\n" );
return ( ERROR );
}
val = ( int * ) shmat ( shmid, NULL, 0 );
if ( val == -1 ) {
perror ( "Error in shmat\n" );
return ( ERROR );
}
scanf ( "%d", &rw);
while ( rw >= 0 ) {
if ( rw == 0 ) {
//write in the shared memory
x = ( int * ) malloc ( sizeof ( int ) );
if ( x == NULL ) {
perror ( "Error in malloc" );
return ( ERROR );
}
scanf ( "%d", x );
val = x;
}
else {
// read from the shared memory
if ( rw == 1 ) {
printf ( "%d\n", *val );
}
}
scanf ( "%d", &rw );
}
return ( 0 );
}
在这段代码中,我想测试共享内存。当我给 rw = 1 时,我在共享内存中写入一个整数,否则我读取共享内存的值,然后打印该值。我找不到问题出在哪里....
最佳答案
您创建了权限设置为 0000
的共享内存段:
shmid = shmget ( key, sizeof( int ), IPC_CREAT );
应该是
shmid = shmget ( key, sizeof( int ), IPC_CREAT | 0660 );
或类似的。
关于c 编程 shmat ( ) 权限被拒绝,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30199805/
因此在这段代码中 puts 无法显示输出。 如果我删除 fgets 行,它会打印 lola 但如果我尝试在 shm 上读写,则不会发生任何事情。我该如何解决这个问题? #include #inclu
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
我有两个进程,每个进程都可以从 shmget() 获取相同的 shmid。 在这两个过程中,shmat()给出了不同的内存地址。 每个进程的地址总是相同的: Process1: 0x41b31000
当我浏览 shmat 的手册页时。它被描述为 API 的原始功能是将与 shmid 关联的内存段附加到调用进程的地址空间。 我的问题如下。 attach 这个词对我来说很普通。我发现很难理解附加所指的
我在运行代码时遇到问题。我的 shmat 失败并打印权限被拒绝。我在谷歌上搜索了如何解决它,但我不能。我的代码如下: #include #include #include #include #
我正在绞尽脑汁地想弄清楚为什么这在我的 Minix 系统上不起作用。 当我尝试编译 C 程序时,出现以下错误: #make link pm/pm program.o: In funct
当我到达routerInfo->areaID行时,我在该部分遇到了段错误:11。看来我没有成功分配内存。我只是不知道为什么。谁能解决这个问题吗? 我的标题是这样的: #include #includ
根据要求包含所有 header 。下面的这段代码可以工作,但问题在于 shmat(seg_id,NULL,0)。第二个参数中的 NULL 意味着操作系统将处理该位置代表用户。但是我们可以给出我们自己的
当尝试执行 shmat 时出现分配错误,告诉我无法访问内存,这在我之前没有发生过,我真的不知道该怎么做。这是错误: 0xffffffffffffffff error: Cannot access me
这个问题不太可能帮助任何 future 的访问者;它只与一个小的地理区域、一个特定的时间点或一个非常狭窄的情况有关,这些情况并不普遍适用于互联网的全局受众。为了帮助使这个问题更广泛地适用,visit
这个问题在这里已经有了答案: shmat() is returning a different "shmaddr" for same "shmkey" (3 个答案) 关闭 8 年前。 在共享内存中
使用mmap(),进程必须从父进程继承映射才能共享内存。有没有办法在不共享父进程的进程之间共享内存? shmat() 似乎是最好的解决方案,但如果进程在退出/死机时没有分离内存,则需要进行清理。 域套
我通过虚拟地址使用 shmat()。 第一个进程调用一个 shmat,shmaddr 为空,当它获得数据 block 的虚拟地址时,它存储在另一个共享内存位置。 第二个进程使用第一个进程存储在共享内存
我试图写入共享内存,但出于某种原因,在我调用 shmat() 和 strcpy 后出现段错误(核心已转储),这是为什么? 这是我的代码: int fd,shmid; key_t shmkey; cha
//TeamSize is an integer int Seg_id = shmget(SHM_KEY,sizeof(Word)*TeamSize,IPC_CREAT); void* Seg_p
shmat(int id , void * addr,int flg) 的第二个参数被推荐/约定为 NULL。 但是如果我想给它一个特定的地址(void* addr),该地址应该来自堆栈还是堆? 我的
我的程序有 2 个线程,我使用共享内存在两者之间进行通信。 每个线程都有一个“init”方法。在 init 方法中,我调用“shmget”和“shmat”来获取共享内存段并附加到局部变量。部分代码如下
在我的简单代码中: #include #include int main() { key_t key = ftok(".", 'b'); int shmid = shmget(ke
在生产者流程中,我有以下内容。 说 MEMKEY 为 key_t 234500,并且 *shared_ring 是指向正在共享的数据结构 (donut_ring) 的指针。 int shmid, se
我需要一些有关此代码的帮助。 我在共享段中创建了一个struct msg_t,并在另一个段中创建了一个简单的字符串。在此方法中,首先附加两个段,然后要初始化字符串。然后我尝试将指向该字符串的指针(在第
我是一名优秀的程序员,十分优秀!