공유메모리 shmget가 말썽입니다.

ratmhun의 이미지

제가 공유메모리에 대해 제대로 이해하지 못하고 있는 것 같습니다. shmget()의 인자에 전혀 문제가 없다고 생각합니다만 프로그램을 실행하면 계속 'shmget: Invalid argument' 라는 메시지만 출력됩니다.

shmget()의 사용법이 잘못 되었나요?

Quote:

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

#define FILENAMELEN 10

struct data
{
char filename[FILENAMELEN];
char mtext[1024];
};

int main(int argc, char **argv)
{
int shmid = 0;
void *shared_memory;
struct data ldata;
int n, source;
char buf[1024];

if((source = open(argv[1], O_RDONLY)) < 0)
{
perror("open readfile");
exit(0);
}

shmid = shmget((key_t)1234, sizeof(ldata), 0666|IPC_CREAT);
if(shmid == -1)
{
perror("shmget");
exit(0);
}

shared_memory = shmat(shmid, (void *)0, 0);
if(shared_memory == -1)
{
perror("shmat");
exit(1);
}

sprintf(ldata.filename, "%s", argv[2]);
while((n = read(source, buf, 1024)) > 0)
{
strncpy(ldata.mtext, buf, n);
printf("%s:\n", ldata.filename);
printf("%s", ldata.mtext);

memcpy((void *)shared_memory, (void *)&ldata, sizeof(ldata));
sleep(1);
}
return 0;
}

익명 사용자의 이미지

실행 플랫폼을 적어주세요.

Quote:
# uname -a

# gcc --version

음 그리고,
#include <fcntl.h>
를 해주셔야 할듯하며,

open(argv[1], O_RDONLY, 0666)

등과 같이 mode를 명시적으로 기술하는 습관을 권장합니다.

cnyld의 이미지

shmget은 key 값으로 sharedmemory를 만들거나 link하죠.
같은 key 값을 가지면서 크기가 다른 shared memory가
이미 존재한다면 그런 에러를 return합니다.
혹은, super user이어야 할지도 모르겠네요...
둘중 하나일것입니다.

IsExist의 이미지

Quote:

$ ipcs

명령어로 할당된 key를 확인할 수 있습니다.
에러 내용으로 봐서는 아래의 두 경우가 아닐까 생각됩니다.

Quote:

EINVAL The size argument is less than the system-imposed
minimum or greater than the system-imposed max-
imum.

EINVAL A shared memory identifier exists for key but the
size of the segment associated with it is less
than size and size is not equal to 0.

---------
간디가 말한 우리를 파괴시키는 7가지 요소

첫째, 노동 없는 부(富)/둘째, 양심 없는 쾌락
셋째, 인격 없는 지! 식/넷째, 윤리 없는 비지니스

이익추구를 위해서라면..

다섯째, 인성(人性)없는 과학
여섯째, 희생 없는 종교/일곱째, 신념 없는 정치

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.