메세지 큐에 대한 질문.

no1hacker의 이미지

메세지 큐를 이용하고 있는데 문제가 발생했는데 원인을 아무리 봐도 모르겠네요..

if ( msgsnd( msqId,&text,sizeof(text),IPC_NOWAIT ) < 0 )
{
if( errno == EAGAIN ) fprintf( stderr, "EAGAIN (queue is full, and IPC_NOWAIT was asserted)\n");
else if( errno == EACCES ) fprintf( stderr, "EACCES (permission denied, no write permission)\n");
else if( errno == EFAULT ) fprintf( stderr, "EFAULT (msgp address isn't accessable - invalid)\n");
else if( errno == EIDRM ) fprintf( stderr, "EIDRM (The message queue has been removed)\n");
else if( errno == EINTR ) fprintf( stderr, "EINTR (Received a signal while waiting to write)\n");
else if( errno == EINVAL ) fprintf( stderr, "EINVAL (Invalid message queue identifier, nonpositive message type, or invalid message size)\n");
else if( errno == ENOMEM ) fprintf( stderr, "ENOMEM (Not enough memory to copy message buffer )\n");
}

다음과 같은 문장을 반복해서 실행할때
비율이 10번가운데 5번은 성공을 하고 5번은 실패합니다.
(물론 빼가는 프로세스도 있기 때문에 꽉차지는 않습니다.)

당혹스럽게 에러는
EINVAL (Invalid message queue identifier, nonpositive message type, or invalid message size) 입니다.

에러 문장에 따라서 해결해 보려고 했으나 쉽지가 않네요.

1. fprintf를 이용해서 성공했을때와 실패했을때 msqId를 비교 했는데 같구요...흑.
2. 특별히 message type을 정하지 않아도 될것 같구...흑
3. text는 구조체인데.. 구조체에 있는 내용을 메세지 큐에 넣는 것은데... text의 사이즈가 문제인가요??
text는 바이너리라서 크기를 알 수도 없을 것 같고 ..

도와주세요ㅜㅜ

JuEUS-U의 이미지

정답은 3번.... 크기입니다 -_-
구조체의 크기에서 4바이트를 빼줘야합니다.

Quote:
The msgsz argument contains the size of the message in bytes, excluding the length of the message type (4 byte long).

다음은 TLDP에서 인용한 코드입니다.

int send_message( int qid, struct mymsgbuf *qbuf )
{
        int     result, length;
 
        /* The length is essentially the size of the structure minus sizeof(mtype) */
        length = sizeof(struct mymsgbuf) - sizeof(long);        
 
        if((result = msgsnd( qid, qbuf, length, 0)) == -1)
        {
                return(-1);
        }
 
        return(result);
}
no1hacker의 이미지

말씀해주신 것은 구조체를 넣을 경우인 거 아닌가요??
즉 구조체 안에 넣고자 하는 문자열과, 그 문자열의 크기를 나타내는 변수(이 변수의 크기를 빼는 것)
로 구성되어 있는데..

이 문자열의 크기를 나타내는 변수의 크기를 빼는게 아닌가 싶어서요..

중요한점은 되다고 5/5정도로 되는 경우도 있고..안되는 경우(EINVAL 에러 발생)가 발생하네요..

저는 구조체를 넣는게 아닌, 그냥 단순한 문자열(캐릭터배열)을 넣는경우거든요~

슬슬 고민이 되네요..

제가 메세지 큐 개념을 잘못이해하고 있는지...흑.

kslee80의 이미지

msgsnd 의 두번째 아규먼트는 꼭 message type 을 포함하는 structure 여야 합니다.

댓글 달기

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
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.