2011-01-12 14 views
2

다음 코드 : (-lrt로 컴파일 우분투 9.10) 리눅스에aio_read X

#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; 
} 

작품 좋은,

1 
1 

그러나 OS X에서 실패 인쇄 (10.6

1 
35, readerr 

는이 OS X에 대한 몇 가지 라이브러리 오류 때문이라고이 가능, 아니면 솜을하고있는 중이 야 : 0.6 및 10.6.5, 나는)이 기계에 그것을 테스트했습니다 잘못 됐어?

+0

단지 '35EAGAIN'이며, "시스템 자원 제한으로 인해 요청이 대기열에 없습니다 .." –

답변

5

는 각 비동기 I/O 작업에 대해 정확히 한 번만 aio_return(2)를 호출해야합니다. 그 man 페이지에있는 메모에 따르면 그렇게하지 않으면 자원이 누출되고 문제가 발생할 수도 있습니다. (강조

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) 사람 페이지에서 다음과 같은 중요한 사항을 명심 : 당신이 완료 될 때까지 I/O를 기다리는 aio_suspend를 호출 한 후, 읽은 바이트 수, 예를 얻기 위해 aio_return를 호출해야합니다 광산)

비동기 I/O 제어 블록 구조 aiocbp 의해 지시되고 작업이 완료 될 때까지 그 구조 참조의 aiocbp->aio_buf 부재 이 유효해야 버퍼. 이러한 이유로 이러한 개체에 대한 자동 (스택) 변수 사용은이 아닙니다.

aio_read() 호출 전에 비동기 입출력 제어 버퍼 aiocbp을 0으로 설정하여 가짜 컨텍스트 정보가 커널에 전달되지 않도록해야합니다.

+0

감사합니다. aio_return이 내 하루를 저장합니다. 나는 스택 단점에 대해 알고 있었지만, 시작한 직후 읽기가 끝나기를 항상 기다렸으므로 문제가되지 않았다. (쓸데없는, 나도 알아. 바보 같은 숙제 때문에). – Pyetras

0

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. 
+0

이 aio = malloc (sizeof (struct aiocb))와 같은 것으로 변경되었습니다. memset (aio, 0, sizeof (struct aiocb)); 그것은 도움이되지 않았습니다, 그것은 다른 것임에 틀림 없습니다. – Pyetras