第十回スパルタンプログラミング
vimを少し勉強、aioを少し勉強
胃が痛くなる週。誰を恨むべきか、怒るべきか。 ストレスがたまるとなのか疲れるとなのかロレツが回らなくなる。といっしーに言われる。 今回ばかりは自覚があるぜー。
電話越しに言いたいことははっきりしているのに、舌足らずになる。回らなくなっている自覚も当然ある。もどかしい。がんばれポジローな週。
やりたいこと
いいかげんに
- スレッドに入る
or
− aioなんちゃらをテストする
べき、うん。
aio_readのテスト
#include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <stdlib.h> #include <string.h> #include <fcntl.h> #include <aio.h> #include <errno.h> int main(){ unsigned int BUFSIZE = 256; int fd, ret; struct aiocb my_aiocb; fd = open( "file.txt", O_RDONLY ); if (fd < 0) perror("open"); /* Zero out the aiocb structure (recommended) */ bzero( (char *)&my_aiocb, sizeof(struct aiocb) ); /* Allocate a data buffer for the aiocb request */ my_aiocb.aio_buf = malloc(BUFSIZE+1); if (!my_aiocb.aio_buf) perror("malloc"); /* Initialize the necessary fields in the aiocb */ my_aiocb.aio_fildes = fd; my_aiocb.aio_nbytes = BUFSIZE; my_aiocb.aio_offset = 0; ret = aio_read( &my_aiocb ); if (ret < 0) perror("aio_read"); while ( aio_error( &my_aiocb ) == EINPROGRESS ) ; if ((ret = (int)aio_return( &my_aiocb )) > 0) { fprintf(stderr, "%s\n", my_aiocb.aio_buf); } else { perror("aio_return()"); } return 0; }
といいつつUNP vol.1を読む
5章まで読んで書いたサーバ・クライアントのテストの理解を整理した。 整理した内容はiphoneの写真に在るから、あとではっつける。
aioやろう
lio_listio
ibmでの紹介
AIO provides a way to initiate multiple transfers at the same time using the lio_listio API function. This function is important because it means you can start lots of I/Os in the context of a single system call (meaning one kernel context switch). This is great from a performance perspective, so it's worth exploring.
int lio_listio( int mode, struct aiocb *list[], int nent, struct sigevent *sig );
The mode argument can be LIO_WAIT or LIO_NOWAIT. LIO_WAIT blocks the call until all I/O has completed. LIO_NOWAIT returns after the operations have been queued. The list is a list of aiocb references, with the maximum number of elements defined by nent. Note that elements of list may be NULL, which lio_listio ignores. The sigevent reference defines the method for signal notification when all I/O is complete.
超意訳
ようするにlio_listioを使えば、複数個のaiocb(aio call block)を一括して スタートさせられるってこと。callbackや命令(read or write or nop)などは、 それぞれのaiocb構造体で定義することになる。
イカした使い方
struct aiocb aiocb[1024]; int nextoffset[1024] struct aiocb *list[MAX_LIST]; ... bzero( (char *)list, sizeof(list) ); /* Prepare the multiple aiocb */ /* ここでは一括してサーバーがクライアントにお返事を出す ことを考えているため、LIO_WRITE(書き込み命令)にしてある。 offsetは必要か */ for(int i=0;i < 1024; i++){ aiocb[i].aio_fildes = fd; aiocb[i].aio_buf = malloc( BUFSIZE+1 ); aiocb[i].aio_nbytes = BUFSIZE; aiocb[i].aio_offset = next_offset[i]; aiocb[i].aio_lio_opcode = LIO_WRITE; list[i] = &aiocb[i] } ... ret = lio_listio( LIO_WAIT, list, MAX_LIST, NULL );
言葉
FAQはfrequently asked questuionの略だった。
vimの練習
vimgrep
:vim {pattern} {file}
同階層ディレクトリのファイルすべてから検索したいなら{file}を*/とすればよい。
quickfix
:make
vimを開いたままmakeできる。Makefileの引数もとれる。 vimの画面に戻った後は
:cn
でエラーの次の行へ
:cp
でエラーの前の行へ いける。
ちょっち休憩
買うべきか買わざるべきか いっしーがさくっと買った。。