c编程的shmat()权限被拒绝 [英] c programming shmat ( ) permission denied
本文介绍了c编程的shmat()权限被拒绝的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个问题,当我运行我的code。我的shmat失败,并打印权限被拒绝。我搜索在谷歌如何解决这个问题,但我不能。我的code是以下内容:
I have a problem when I run my code. My shmat fails and prints permission denied. I searched on google how to solve it but I can't. My code is the following:
#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 );
}
在此code我要测试的共享内存。我写在共享内存中的一个整数,当我给RW = 1否则我读共享内存的值,然后我打印此值。我找不到哪里出了问题......
In this code I want to test the shared memory. I write an integer in the shared memory when I give rw = 1 else I read the value of the shared memory and then I print this value. I can't find where is the problem....
推荐答案
您创建的共享内存段设置权限 0000
:
You created the shared memory segment with permissions set to 0000
:
shmid = shmget ( key, sizeof( int ), IPC_CREAT );
应
shmid = shmget ( key, sizeof( int ), IPC_CREAT | 0660 );
或相似的。
这篇关于c编程的shmat()权限被拒绝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文