Aio

    技术2022-05-14  2

    #include #include #include #include #include #include #include /*socket address struct*/ #include /*host to network convertion*/ #include #include #include //#include #define MAX_TRANSPORT_LENTH 512 #define MAX_LIST 5 void sig_handler(int signo, siginfo_t *info, void *context ) { int ret; struct aiocb *req; /* Ensure it's our signal */ if (info->si_signo == SIGIO) { req = (struct aiocb *)info->si_value.sival_ptr; if (aio_error( req ) == 0) { ret = aio_return( req ); (char*)(req->aio_buf+ret) == '/0'; puts(req->aio_buf); } } return; } int main() { int skt_fd,ret; struct aiocb my_aiocb; struct sigaction sig_act; struct sockaddr_in addr; memset(&addr,0,sizeof(addr)); addr.sin_family = AF_INET; addr.sin_addr.s_addr = INADDR_ANY; addr.sin_port = htons(50001); skt_fd = socket(AF_INET, SOCK_DGRAM, 0); if(skt_fd == -1) { perror("Create socket failed"); exit(-1); } ret = bind(skt_fd,(struct sockaddr *)&addr,sizeof(addr)); if(-1 == ret) { perror("Bind socket failed"); exit(-1); } memset(&my_aiocb,0,sizeof(my_aiocb)); my_aiocb.aio_buf = malloc(MAX_TRANSPORT_LENTH+1); if (!my_aiocb.aio_buf) perror("malloc"); my_aiocb.aio_fildes = skt_fd; my_aiocb.aio_nbytes = MAX_TRANSPORT_LENTH; my_aiocb.aio_offset = 0; my_aiocb.aio_sigevent.sigev_notify = SIGEV_SIGNAL; my_aiocb.aio_sigevent.sigev_signo = SIGIO; my_aiocb.aio_sigevent.sigev_value.sival_ptr = &my_aiocb; sigemptyset(&sig_act.sa_mask); sig_act.sa_flags = SA_SIGINFO; sig_act.sa_sigaction = sig_handler; const struct aiocb *cblist[MAX_LIST] ={&my_aiocb,NULL,NULL,NULL,NULL}; sigaction( SIGIO, &sig_act, NULL ); ret = aio_read( &my_aiocb ); //程序挂起直到等到某个信号 while ( aio_suspend( cblist, MAX_LIST, NULL ) == 0) { ret = aio_read( &my_aiocb ); }; puts("Interrupted finish."); close(skt_fd); }

    最新回复(0)