Cygwin环境下共享内存 Shmget()调用失败 Bad System Call

yaoyuhang 2005-02-21 02:24:13
在Cygwin 环境下如下代码。调用到 shmid = shmget(key,SHM_SIZE,IPC_CREAT)时 提示 Bad System Call

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>

#define ARRAY_SIZE 40
#define MALLOC_SIZE 1024
#define SHM_SIZE 1024
#define SHM_MODE (SHM_R | SHM_W)

char array[ARRAY_SIZE];

int main(void){
int shmid;
char *ptr,*shmptr;
key_t key;
key = ftok(".",'1');

if((ptr = malloc(MALLOC_SIZE)) == NULL)
printf("malloc error!");
printf("maloc ok!");
if((shmid = shmget(key,SHM_SIZE,IPC_CREAT)) < 0 )
printf("shmget error!!!");
if((shmptr = shmat(shmid,0,0)) == (void *)-1)
printf("shmat error!");
if (shmctl(shmid,IPC_RMID,0) < 0)
printf("shmctl error!");
exit(0);
}
...全文
1375 11 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
sky_cool 2005-09-01
  • 打赏
  • 举报
回复
尽量不要用shmget,shmat系列的库函数,它们是system V的标准,不是每个linux/unix系统都支持的
用posix标准的一系列共享内存的函数:shm_open(),ftruncate(),mmap(),shm_unlink()
功能基本和shmget......相同,你可以去man一下用法,或者参考《unix网络编程》第二版 第二卷W.R.Stevens著
chillming 2005-08-10
  • 打赏
  • 举报
回复
我在FreeBSD下可以编译,原因是内核少了如下选项:
options SYSVSHM # SYSV-style shared memory
options SYSVMSG # SYSV-style message queues
options SYSVSEM # SYSV-style semaphores

我加上去就可以拉,至于Cygwin我就不清楚拉
hundlom 2005-07-22
  • 打赏
  • 举报
回复
以下只是其他建议:
ftok(char*, int);第二个参数为int
malloc之后的的指针你没有free()
关于这个cygwin我没用过。抱歉
chillming 2005-07-22
  • 打赏
  • 举报
回复
man shmget
byte 961SHMGET(2) FreeBSD System Calls Manual SHMGET(2)

NAME
shmget -- obtain a shared memory identifier

LIBRARY
Standard C Library (libc, -lc)

SYNOPSIS
#include <machine/param.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>

int
shmget(key_t key, size_t size, int flag);

DESCRIPTION
Based on the values of key and flag, shmget() returns the identifier of a
newly created or previously existing shared memory segment. The key is
analogous to a filename: it provides a handle that names an IPC object.
There are three ways to specify a key:

o IPC_PRIVATE may be specified, in which case a new IPC object will be
created.

o An integer constant may be specified. If no IPC object corresponding
to key is specified and the IPC_CREAT bit is set in flag, a new one
will be created.

o The ftok(3) may be used to generate a key from a pathname.

The mode of a newly created IPC object is determined by OR'ing the fol-
lowing constants into the flag argument:

SHM_R Read access for user.

SHM_W Write access for user.

(SHM_R>>3) Read access for group.

(SHM_W>>3) Write access for group.

(SHM_R>>6) Read access for other.

(SHM_W>>6) Write access for other.

When creating a new shared memory segment, size indicates the desired
size of the new segment in bytes. The size of the segment may be rounded
up to a multiple convenient to the kernel (i.e., the page size).

RETURN VALUES
Upon successful completion, shmget() returns the positive integer identi-
fier of a shared memory segment. Otherwise, -1 is returned and errno set
to indicate the error.

ERRORS
The shmget() system call will fail if:

[EINVAL] Size specified is greater than the size of the previ-
ously existing segment. Size specified is less than
the system imposed minimum, or greater than the system
imposed maximum.

[ENOENT] No shared memory segment was found matching key, and
IPC_CREAT was not specified.

[ENOSPC] The kernel was unable to allocate enough memory to
satisfy the request.

[EEXIST] IPC_CREAT and IPC_EXCL were specified, and a shared
memory segment corresponding to key already exists.

SEE ALSO
shmat(2), shmctl(2), shmdt(2), ftok(3)

FreeBSD 5.3 July 3, 1995 FreeBSD 5.3
chillming 2005-07-22
  • 打赏
  • 举报
回复
我在FreeBSD里面
gcc后执行也是Bad system call (core dumped)
yaoyuhang 2005-02-23
  • 打赏
  • 举报
回复
我有开了一个贴。分数不是很多
单独问上面的问题。
http://community.csdn.net/Expert/topic/3801/3801425.xml?temp=.38052
yaoyuhang 2005-02-23
  • 打赏
  • 举报
回复
还有一个额外的问题麻烦各位:

在多个没有亲缘关系的进程中,想共享一块内存。不知道有什么办法,可以让其他进程知道共享内存的 shmid

我想到,用一个物理文件来保存。是不是太笨了。

有没有什么其他方法。

请赐教。

yaoyuhang 2005-02-23
  • 打赏
  • 举报
回复
我不太知道Cygwin 是否自持 share memory .
但我按照 kz(kz)的方法试了试。
好像没有效果。不知道我安装的 CygwinServer 是否有问题。
但从CygwinServer的介绍文档看应该是支持Share memory的。
gettext 2005-02-22
  • 打赏
  • 举报
回复
Cygwin 支持share memory吗?
kz 2005-02-21
  • 打赏
  • 举报
回复
/usr/share/doc/Cygwin/cygserver.README
yaoyuhang 2005-02-21
  • 打赏
  • 举报
回复
自己up

23,215

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧