You are not logged in.
Hi,
I was trying a program with async IO.
according to the man page(open 2M) when a IO is done on a file descriptor which was created with flags O_ASYNC, a signal ISGIO should be delivered to the process. But for My program its not happening.
Could somebdy please help? Is there somthing wrong with this program. ?
#define _GNU_SOURCE
#include<stdio.h>
#include<sys/fcntl.h>
#include<signal.h>
void sig_handler(int signum)
{
printf("Signal received\n");
}
int main()
{
int fd, rv, flags;
char buff[100];
signal(SIGIO,sig_handler);
fd = open("/dev/pts/4",O_NONBLOCK|O_ASYNC|O_RDWR);
if(fd < 0)
{
printf("open failed\n");
perror("open");
}
rv = fcntl(fd, F_SETOWN, getpid());
if(rv < 0 )
{
perror("F_SETOWN");
}
rv = fcntl(fd, F_GETOWN );
if(rv < 0 )
{
perror("F_SETOWN");
}
printf("getown retuened %d\n",rv);
flags = fcntl(fd, F_GETFL) ;
rv = fcntl(fd,F_SETSIG,SIGIO);
rv = fcntl(fd, F_SETFL, flags | O_ASYNC|O_RDWR | O_NONBLOCK);
if(rv < 0 )
{
perror("F_SETFL");
}
//pause();
while(1)
{
sleep(3);
read(fd,buff,4);
printf("read \n");
}
return(0);
}
strace output
[root@localhost smalik]# strace ./a.out
execve("./a.out", ["./a.out"], [/* 27 vars */]) = 0
brk(0) = 0x9f1c000
access("/etc/ld.so.preload", R_OK) = -1 ENOENT (No such file or directory)
open("/etc/ld.so.cache", O_RDONLY) = 3
fstat64(3, {st_mode=S_IFREG|0644, st_size=102370, ...}) = 0
mmap2(NULL, 102370, PROT_READ, MAP_PRIVATE, 3, 0) = 0xb7f16000
close(3) = 0
open("/lib/libc.so.6", O_RDONLY) = 3
read(3, "\177ELF\1\1\1\0\0\0\0\0\0\0\0\0\3\0\3\0\1\0\0\0\360D\233\0004\0\0\0"..., 512) = 512
fstat64(3, {st_mode=S_IFREG|0755, st_size=1692524, ...}) = 0
mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0xb7f15000
mmap2(0x99e000, 1410608, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x99e000
mmap2(0xaf1000, 12288, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x153) = 0xaf1000
mmap2(0xaf4000, 9776, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0xaf4000
close(3) = 0
mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0xb7f14000
set_thread_area({entry_number:-1 -> 6, base_addr:0xb7f146c0, limit:1048575, seg_32bit:1, contents:0, read_exec_only:0, limit_in_pages:1, seg_not_present:0, useable:1}) = 0
mprotect(0xaf1000, 8192, PROT_READ) = 0
mprotect(0x99a000, 4096, PROT_READ) = 0
munmap(0xb7f16000, 102370) = 0
rt_sigaction(SIGIO, {0x8048544, [IO], SA_RESTART}, {SIG_DFL}, 8) = 0
open("/dev/pts/4", O_RDWR|O_NONBLOCK|O_ASYNC) = 3
getpid() = 6291
fcntl64(3, F_SETOWN, 6291) = 0
fcntl64(3, F_GETOWN) = 6291
fstat64(1, {st_mode=S_IFCHR|0620, st_rdev=makedev(136, 2), ...}) = 0
mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0xb7f2e000
write(1, "getown retuened 6291\n", 21getown retuened 6291
) = 21
fcntl64(3, F_GETFL) = 0x2802 (flags O_RDWR|O_NONBLOCK|O_ASYNC)
fcntl64(3, F_SETSIG, 0x1d) = 0
fcntl64(3, F_SETFL, O_RDWR|O_NONBLOCK|O_ASYNC) = 0
rt_sigprocmask(SIG_BLOCK, [CHLD], [], 8) = 0
rt_sigaction(SIGCHLD, NULL, {SIG_DFL}, 8) = 0
rt_sigprocmask(SIG_SETMASK, [], NULL, 8) = 0
nanosleep({3, 0}, {3, 0}) = 0
read(3, 0xbfe8e604, 4) = -1 EAGAIN (Resource temporarily unavailable)
write(1, "read \n", 6read
) = 6
rt_sigprocmask(SIG_BLOCK, [CHLD], [], 8) = 0
rt_sigaction(SIGCHLD, NULL, {SIG_DFL}, 8) = 0
rt_sigprocmask(SIG_SETMASK, [], NULL, 8) = 0
nanosleep({3, 0}, {3, 0}) = 0
read(3, 0xbfe8e604, 4) = -1 EAGAIN (Resource temporarily unavailable)
write(1, "read \n", 6read
) = 6
rt_sigprocmask(SIG_BLOCK, [CHLD], [], 8) = 0
rt_sigaction(SIGCHLD, NULL, {SIG_DFL}, 8) = 0
rt_sigprocmask(SIG_SETMASK, [], NULL, 8) = 0
nanosleep({3, 0}, {3, 0}) = 0
read(3, 0xbfe8e604, 4) = -1 EAGAIN (Resource temporarily unavailable)
write(1, "read \n", 6read
) = 6
rt_sigprocmask(SIG_BLOCK, [CHLD], [], 8) = 0
rt_sigaction(SIGCHLD, NULL, {SIG_DFL}, 8) = 0
rt_sigprocmask(SIG_SETMASK, [], NULL, 8) = 0
nanosleep({3, 0}, {3, 0}) = 0
read(3, 0xbfe8e604, 4) = -1 EAGAIN (Resource temporarily unavailable)
write(1, "read \n", 6read
) = 6
rt_sigprocmask(SIG_BLOCK, [CHLD], [], 8) = 0
rt_sigaction(SIGCHLD, NULL, {SIG_DFL}, 8) = 0
rt_sigprocmask(SIG_SETMASK, [], NULL, 8) = 0
nanosleep({3, 0}, {3, 0}) = 0
read(3, 0xbfe8e604, 4) = -1 EAGAIN (Resource temporarily unavailable)
write(1, "read \n", 6read
) = 6
rt_sigprocmask(SIG_BLOCK, [CHLD], [], 8) = 0
rt_sigaction(SIGCHLD, NULL, {SIG_DFL}, 8) = 0
rt_sigprocmask(SIG_SETMASK, [], NULL, 8) = 0
nanosleep({3, 0}, <unfinished ...>
[root@localhost smalik]#
Offline
Similar program with sockets worked? but the man page clearly says that it should work with sockets, FIFOs, terminals and pseudu terminals.
Offline
did you see this at the end of the man page?
BUGS
Currently, it is not possible to enable signal-driven I/O by specifying
O_ASYNC when calling open(); use fcntl(2) to enable this flag.
so try leaving out O_ASYNC in your open() command but add and extra line below it for fcntl:
fcntl(fd, F_SETFL, O_ASYNC);
Offline
I tried setting it using fcntl but my program disn't work. BTW follwoing program worked
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <signal.h>
int h;
void async(int s)
{
char c;
int x = 0;
printf("signal\n"); fflush(stdout);
/*while (read(h, &c, 1) == 1) x++; */
read(h, &c, 1);
printf("read: %c\n", c); /* fflush(stdout); */
if (fcntl(h, F_SETFL, O_NONBLOCK) < 0) perror("fcntl"), exit(1);
if (fcntl(h, F_SETFL, O_ASYNC | O_NONBLOCK) < 0) perror("fcntl"), exit(1);
sleep(1);
}
int main(void)
{
h = open("/dev/tty", O_RDONLY | O_NONBLOCK);
if (h < 0) perror("open"), exit(1);
signal(SIGIO, async);
if (fcntl(h, F_SETFL, O_ASYNC | O_NONBLOCK) < 0) perror("fcntl"), exit(1);
while (1) sleep(1);
}
Offline