質問

次のコード:

#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <aio.h>
#include <errno.h>

int main (int argc, char const *argv[])
{
  char name[] = "abc";
  int fdes;
  if ((fdes = open(name, O_RDWR | O_CREAT, 0600 )) < 0)
    printf("%d, create file", errno);

  int buffer[] = {0, 1, 2, 3, 4, 5};
  if (write(fdes, &buffer, sizeof(buffer)) == 0){
    printf("writerr\n");
  }

  struct aiocb aio;
  int n = 2;
  while (n--){
    aio.aio_reqprio = 0;
    aio.aio_fildes = fdes;
    aio.aio_offset = sizeof(int);
    aio.aio_sigevent.sigev_notify = SIGEV_NONE; 

    int buffer2;
    aio.aio_buf = &buffer2;
    aio.aio_nbytes = sizeof(buffer2);

    if (aio_read(&aio) != 0){
      printf("%d, readerr\n", errno);
    }else{
      const struct aiocb *aio_l[] = {&aio};
      if (aio_suspend(aio_l, 1, 0) != 0){
         printf("%d, suspenderr\n", errno);
      }else{
        printf("%d\n", *(int *)aio.aio_buf);
      }
    }
  }

  return 0;
}

Linux(Ubuntu 9.10、-LRTでコンパイル)で正常に動作し、印刷

1
1

しかし、OS Xでは失敗します(10.6.6および10.6.5では、2つのマシンでテストしました):

1
35, readerr

これは、OS Xのライブラリエラーによるものである可能性がありますか、それとも私は何か間違ったことをしていますか?

役に立ちましたか?

解決

あなたは電話する必要があります aio_return(2) 非同期I/O操作ごとに1回だけ。その男のページのメモによると、そうしないとリソースが漏れ、それは明らかにあなたの問題を引き起こします。電話した後 aio_suspend I/Oが完了するのを待つには、必ず電話してください aio_return バイト数を読み取るには、例:

const struct aiocb *aio_l[] = {&aio};
if (aio_suspend(aio_l, 1, 0) != 0)
{
  printf("aio_suspend: %s\n", strerror(errno));
}
else
{
  printf("successfully read %d bytes\n", (int)aio_return(&aio));
  printf("%d\n", *(int *)aio.aio_buf);
}

また、これらの重要なメモを心に留めてください aio_read(2) Man Page(Emphasis Mine):

非同期のI/O制御ブロック構造は aiocbp とバッファー aiocbp->aio_buf その構造のメンバーは、操作が完了するまで有効なままでなければなりません。このために、 これらのオブジェクトの自動(スタック)変数の使用は落胆します.

非同期I/Oコントロールバッファー aiocbp 前にゼロにする必要があります aio_read() 偽のコンテキスト情報をカーネルに渡すことを避けるために電話してください。

他のヒント

ゼロを試してみてください struct aiocb aio?

マニュアルは読みます:

RESTRICTIONS
[...]
     The asynchronous I/O control buffer aiocbp should be zeroed before the
     aio_read() call to avoid passing bogus context information to the kernel.
[...]
BUGS
     Invalid information in aiocbp->_aiocb_private may confuse the kernel.
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top