📄 asyn_io_signal.c
字号:
/* aioExSig.c - example code for using signals with asynchronous I/O */
/* includes */
#include "vxWorks.h"
#include "aio.h"
#include "errno.h"
/* defines */
#define BUFFER_SIZE 200
#define LIST_SIZE 1
#define EXAMPLE_SIG_NO 25 /* signal number */
/* forward declarations */
void mySigHandler (int sig, struct siginfo * info, void * pContext);
/************************************************************************
* aioExampleSig - use AIO library.
*
* This example shows the basic functions of the AIO library.
* Note if this is run from the shell it must be spawned. Use:
* -> sp aioExampleSig
*
* RETURNS: OK if successful, otherwise ERROR.
*/
STATUS aioExampleSig (void)
{
int fd;
static char exFile [] = "/pipe/1stPipe";
struct aiocb aiocb_read; /* read aiocb */
static struct aiocb aiocb_write; /* write aiocb */
struct sigaction action; /* signal info */
static char * test_string = "testing 1 2 3";
char buffer [BUFFER_SIZE]; /* aiocb read buffer */
pipeDevCreate (exFile, 50, 100);
if ((fd = open (exFile, O_CREAT | O_TRUNC| O_RDWR, 0666)) == ERROR)
{
printf ("aioExample: cannot open %s errno 0x%x\n", exFile, errno);
return (ERROR);
}
printf ("aioExampleSig: Example file = %s\tFile descriptor = %d\n", exFile, fd);
/* set up signal handler for EXAMPLE_SIG_NO */
action.sa_sigaction = mySigHandler;
action.sa_flags = SA_SIGINFO;
sigemptyset (&action.sa_mask);
sigaction (EXAMPLE_SIG_NO, &action, NULL);
/* initialize read and write aiocbs */
bzero ((char *) &aiocb_read, sizeof (struct aiocb));
bzero ((char *) buffer, sizeof (buffer));
aiocb_read.aio_fildes = fd;
aiocb_read.aio_buf = buffer;
aiocb_read.aio_nbytes = BUFFER_SIZE;
aiocb_read.aio_reqprio = 0;
bzero ((char *) &aiocb_write, sizeof (struct aiocb));
aiocb_write.aio_fildes = fd;
aiocb_write.aio_buf = test_string;
aiocb_write.aio_nbytes = strlen (test_string);
aiocb_write.aio_reqprio = 0;
/* set up signal info */
aiocb_write.aio_sigevent.sigev_signo = EXAMPLE_SIG_NO;
aiocb_write.aio_sigevent.sigev_notify = SIGEV_SIGNAL;
aiocb_write.aio_sigevent.sigev_value.sival_ptr = (void *) &aiocb_write;
/* initiate the read */
if (aio_read (&aiocb_read) == -1)
printf ("aioExampleSig: aio_read failed\n");
/* verify that it is in progress */
if (aio_error (&aiocb_read) == EINPROGRESS)
printf ("aioExampleSig: read is still in progress\n");
/* write to pipe - the read should be able to complete */
printf ("aioExampleSig: getting ready to initiate the write\n");
if (aio_write (&aiocb_write) == -1)
printf ("aioExampleSig: aio_write failed\n");
/* clean up */
if (aio_return (&aiocb_read) == -1)
printf ("aioExampleSig: aio_return for aiocb_read failed\n");
else
printf ("aioExampleSig: aio read message = %s\n", aiocb_read.aio_buf);
close (fd);
return (OK);
}
void mySigHandler
(
int sig,
struct siginfo * info,
void * pContext
)
{
/* print out what was read */
printf ("mySigHandler: Got signal for aio write\n");
/* write is complete so let’s do cleanup for it here */
if (aio_return (info->si_value.sival_ptr) == -1)
{
printf ("mySigHandler: aio_return for aiocb_write failed\n");
printErrno (0);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -