天天看點

linux下異步IO的簡單例子【轉】

首先,貼一下異步IO中用的的一些結構體,因為平常很少用,整理起來友善檢視。

aio.h中的struct aiocb

<code>struct aiocb {   int aio_fildes;        /* File desriptor. */   int aio_lio_opcode;        /* Operation to be performed. */   int aio_reqprio;        /* Request priority offset. */   volatile void *aio_buf;    /* Location of buffer. */   size_t aio_nbytes;        /* Length of transfer. */   struct sigevent aio_sigevent;    /* Signal number and value. */   /* Internal members. */   struct aiocb *__next_prio;   int __abs_prio;   int __policy;   int __error_code;   __ssize_t __return_value; };</code>

siginfo.h中的struct sigevent和<code>union sigval</code><code></code>

<code>typedef struct sigevent   {     sigval_t sigev_value;     int sigev_signo;     int sigev_notify;     union       {     int _pad[__SIGEV_PAD_SIZE];     /* When SIGEV_SIGNAL and SIGEV_THREAD_ID set, LWP ID of the      thread to receive the signal. */     __pid_t _tid;     struct      {      void (*_function) (sigval_t);    /* Function to start. */      void *_attribute;            /* Really pthread_attr_t. */      } _sigev_thread;       } _sigev_un;   } sigevent_t; /* POSIX names to access some of the members. */ # define sigev_notify_function _sigev_un._sigev_thread._function # define sigev_notify_attributes _sigev_un._sigev_thread._attribute</code>

<code>typedef union sigval   {     int sival_int;     void *sival_ptr;   } sigval_t;</code>

例子1:<code></code>

<code>#include &lt;aio.h&gt; #include &lt;fcntl.h&gt; #include &lt;unistd.h&gt; #include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include &lt;errno.h&gt; #include &lt;string.h&gt; #include &lt;signal.h&gt; void async_read(int s, siginfo_t * info, void * context) {     struct aiocb *ptr =          (struct aiocb *)info-&gt;si_value.sival_ptr;     printf("read=%s", (char *)ptr-&gt;aio_buf);     } int main(void) {     struct aiocb cb;     char sbuf[100];     int ret;     struct sigaction act;     sigemptyset(&amp;act.sa_mask);     act.sa_flags = SA_RESTART | SA_SIGINFO;     act.sa_sigaction = async_read;     sigaction(SIGUSR1, &amp;act, NULL);     bzero(&amp;cb, sizeof(cb));     cb.aio_fildes = 0;     cb.aio_buf = sbuf;     cb.aio_nbytes = 100;     cb.aio_offset = 0;     cb.aio_sigevent.sigev_value.sival_ptr = &amp;cb;     cb.aio_sigevent.sigev_notify = SIGEV_SIGNAL;     cb.aio_sigevent.sigev_signo = SIGUSR1;     ret = aio_read(&amp;cb);     if (ret == -1) {         perror("aio_read");         exit(1);     }     int i = 0;     while (1) {         printf("%d\n",i++);         sleep(3);     }     return 0; }</code>

運作結果:

注意:要加相應的庫,-lrt

 $ ./gcc -o test aio_signal.c -lrt 

<code>$ ./test 0 1 h2 ell3 o read=hello 4 ^C</code>

例子2:

<code>#include &lt;aio.h&gt; #include &lt;fcntl.h&gt; #include &lt;unistd.h&gt; #include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include &lt;errno.h&gt; #include &lt;string.h&gt; void async_read(sigval_t val) {     struct aiocb *ptr =          (struct aiocb *)val.sival_ptr;     printf("read=%s", (char *)ptr-&gt;aio_buf);     } int main(void) {     struct aiocb cb;     char sbuf[100];     int ret;     bzero(&amp;cb, sizeof(cb));     cb.aio_fildes = 0;     cb.aio_buf = sbuf;     cb.aio_nbytes = 100;     cb.aio_offset = 0;     cb.aio_sigevent.sigev_value.sival_ptr = &amp;cb;     cb.aio_sigevent.sigev_notify = SIGEV_THREAD;     cb.aio_sigevent.sigev_notify_function =          async_read;     cb.aio_sigevent.sigev_notify_attributes = NULL;         ret = aio_read(&amp;cb);     if (ret == -1) {         perror("aio_read");         exit(1);     }          int i = 0;     while (1) {         printf("%d\n",i++);         sleep(1);     }     return 0; }</code>

運作結果同上。

【新浪微網誌】 張昺華--sky

【twitter】 @sky2030_

【facebook】 張昺華 zhangbinghua

本文版權歸作者和部落格園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接配接,否則保留追究法律責任的權利.

繼續閱讀