linux下非同步IO的簡單例子

來源:互聯網
上載者:User

首先,貼一下非同步io中用的的一些結構體,因為平常很少用,整理起來方便查看。

aio.h中的struct aiocb

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

siginfo.h中的struct sigevent和union sigval

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

 

typedef union sigval
  {
    int sival_int;
    void *sival_ptr;
  } sigval_t;

例子1:

#include <aio.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <signal.h>

void async_read(int s, siginfo_t * info, void * context)
{
    struct aiocb *ptr =
        (struct aiocb *)info->si_value.sival_ptr;
    printf("read=%s", (char *)ptr->aio_buf);    
}

int main(void)
{
    struct aiocb cb;
    char sbuf[100];
    int ret;
    struct sigaction act;
    sigemptyset(&act.sa_mask);
    act.sa_flags = sa_restart | sa_siginfo;
    act.sa_sigaction = async_read;

    sigaction(sigusr1, &act, null);

    bzero(&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 = &cb;
    cb.aio_sigevent.sigev_notify = sigev_signal;
    cb.aio_sigevent.sigev_signo = sigusr1;
    ret = aio_read(&cb);
    if (ret == -1) {
        perror("aio_read");
        exit(1);
    }
    int i = 0;
    while (1) {
        printf("%d/n",i++);
        sleep(3);
    }

    return 0;
}

運行結果:
注意:要加相應的庫,-lrt


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

$ ./test
0
1
h2
ell3
o
read=hello
4
^c

例子2:

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

void async_read(sigval_t val)
{
    struct aiocb *ptr =
        (struct aiocb *)val.sival_ptr;
    printf("read=%s", (char *)ptr->aio_buf);    
}

int main(void)
{
    struct aiocb cb;
    char sbuf[100];
    int ret;

    bzero(&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 = &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(&cb);
    if (ret == -1) {
        perror("aio_read");
        exit(1);
    }
    
    int i = 0;
    while (1) {
        printf("%d/n",i++);
        sleep(1);
    }

    return 0;
}

運行結果同上。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.