[Develope]/Network

RTS를 이용한 Asynchronous IO

하늘을닮은호수M 2005. 5. 20. 13:49
반응형

// aiocb_ex.c, v 0.1 2005/05/19
// ============================================================================
//
// = LIBRARY
//
//
// = FILENAME
// aiocb_ex.c
//
// = DESCRIPTION
// aiocb_ex.c which use RT Signal.
//
// struct aiocb
// int aio_fildes; /*file descriptor*/
// volatile void *aio_buf; /*buffer allocation*/
// size_t aio_nbytes; /*length of transfer*/
// off_t aio_offet; /*file offset. starting position*/
// int aio_reqprio; /*request priority offset(_POSIX_PRIORITIZED_IO and _POSIX_PRORITY_SCHEDULING*/
// struct sigevent aio_sigevent; /*signal number and offset*/
// int aio_lio_opcode; /*listio operation*/
//
// = COMPILE and RUN
// % gcc -g -o aiocb_ex -lrt aiocb_ex.c
// % ./aiocb_ex
//
// = AUTHOR
// sunsson <sunsson@varovision.com>
//
// ============================================================================

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include

#define BLKSIZE 1024

volatile char buf1[BLKSIZE];
volatile char buf2[BLKSIZE];
int fd_1 = 0;
int fd_2 = 0;
int fd1_error = 0;
int fd2_error = 0;
struct aiocb my_aiocb1;
struct aiocb my_aiocb2;

void my_aio_handler(int signo, siginfo_t *info, void *context)
{
int my_errno;
int my_status;
struct aiocb *my_aiocbp;
int *errorp;

my_aiocbp = info->si_value.sival_ptr; //pointer signal value(union). void *형 callback data
if(signo == SIGRTMAX)
errorp = &fd1_error;
else
errorp = &fd2_error;

if((my_errno = aio_error(my_aiocbp)) != EINPROGRESS ){
my_status = aio_return(my_aiocbp); //return statsus of before operation
if(my_status >= 0){
write(STDERR_FILENO, (char *)my_aiocbp->aio_buf, my_status); //읽은 데이터를 쓴다
*errorp = aio_read(my_aiocbp); //다음 데이터를 읽도록..
}
else
*errorp = 1;
}
}

void main(int argc, char *argv[])
{
sigset_t old_mask;
struct sigaction newact;

/*file open*/
if(argc != 3){
fprintf(stderr, "Usage : %s filename1 filename2n", argv[0]);
exit(1);
}
if((fd_1 = open(argv[1], O_RDONLY)) == -1){
fprintf(stderr, "Could not open %s: %sn", argv[1], strerror(errno));
exit(1);
}
if((fd_2 = open(argv[2], O_RDONLY)) == -1){
fprintf(stderr, "Could not open %s: %sn", argv[2], strerror(errno));
exit(1);
}

/*setup handler for SIGRTMAX and SIGRTMAX -1*/
sigemptyset(&newact.sa_mask);
sigaddset(&newact.sa_mask, SIGRTMAX);
sigaddset(&newact.sa_mask, SIGRTMAX-1);
/*block the signal*/
if(sigprocmask(SIG_BLOCK, &newact.sa_mask, &old_mask) == -1) {
perror("Could not block SIGRTMAX or SIGRTMAX-1");
exit(1);
}
newact.sa_sigaction = my_aio_handler; //set up sig_hanlder
newact.sa_flags = SA_SIGINFO;
if(sigaction(SIGRTMAX, &newact, NULL) == -1){
perror("Could not set SIGRTMAX handler");
exit(1);
}
if(sigaction(SIGRTMAX-1, &newact, NULL) == -1){
perror("Could not set SIGRTMAX-1 handler");
exit(1);
}
/*unblock the signal*/
if(sigprocmask(SIG_UNBLOCK, &newact.sa_mask, NULL) == -1){
perror("Could not unblock SIGRTMAX or SIGRTMAX-1");
exit(1);
}
my_aiocb1.aio_fildes = fd_1;
my_aiocb1.aio_offset = 0;
my_aiocb1.aio_buf = (void *)buf1;
my_aiocb1.aio_nbytes = BLKSIZE;
my_aiocb1.aio_sigevent.sigev_notify = SIGEV_SIGNAL;
my_aiocb1.aio_sigevent.sigev_signo = SIGRTMAX;
my_aiocb1.aio_sigevent.sigev_value.sival_ptr = &my_aiocb1; //시그널이 발생했을 때 사용하는 void* 형 callback data
fd1_error = aio_read(&my_aiocb1);
if(fd1_error == -1){
if(errno == ENOSYS)
fprintf(stderr, "!!!!!Not supported yesn");
else
perror("The aio_read failed");
exit(1);
}
my_aiocb2.aio_fildes = fd_2;
my_aiocb2.aio_offset = 0;
my_aiocb2.aio_buf = (void *)buf2;
my_aiocb2.aio_nbytes = BLKSIZE;
my_aiocb2.aio_sigevent.sigev_notify = SIGEV_SIGNAL;
my_aiocb2.aio_sigevent.sigev_signo = SIGRTMAX-1;
my_aiocb2.aio_sigevent.sigev_value.sival_ptr = &my_aiocb2; //시그널이 발생했을 때 사용하는 void* 형 callback data
fd2_error = aio_read(&my_aiocb2);
if(fd2_error == -1){
perror("The aio_read failed");
exit(1);
}

/*preceed with overlapping computations*/
while(!fd1_error || !fd2_error)
/*do whatever*/;
exit(0);
}

반응형

'[Develope] > Network' 카테고리의 다른 글

per-process timer :: timer_create  (0) 2005.06.13
per-process timers :: timer_getoverrun, timer_gettime, timer_settime  (0) 2005.06.13
[펌] autoconf, automake  (0) 2005.06.02
IOCP[출처:CodeProject]  (0) 2005.05.20
Asynchronous IO  (0) 2005.02.21