如何检查是否有数据要写入指定的fd

时间:2015-01-22 20:27:33

标签: c linux unix

我试图查看某人是否正在将数据写入sqlite db。我讨论了sql_hook,但这只能在写入db的进程中使用。在我的情况下,一个进程正在写入db,而其他进程只会等待写入某些内容的通知。因此,我试图使用poll并检查sqlite db文件是否有要写入的事件数据。但民意调查只检查事件:

POLLIN
There is data to read.

POLLPRI
There is urgent data to read (e.g., out-of-band data on TCP socket; pseudoterminal master in packet mode has seen state change in slave).

POLLOUT
Writing now will not block.

所以我试图通过使用POLLOUT系统调用来检查fd上的事件open

第一个问题:如果sqlite使用open系统调用来打开db文件,我使用open (int sqlDBFD = open("/var/log.db", O_RDONLY); )会收到与sqlite相同的fd吗? 第二个问题,这里是一段代码,用于监视fd上的事件:

/* thread method */
void run() {
        int result = 0;
        int cur_fd = -1;
        while(1) {
                result = poll(m_fds, m_curr_m_fds_index, m_timeout * 1000);
                //printf("poll result: %d\n", result);
                if (result > 0) {
                        for(int i =0 ; i< m_curr_m_fds_index; i++){
                                cur_fd = m_fds[i].fd;
                                if (cur_fd > 0) {
                                        if (m_fds[i].revents & POLLIN) {
                                                printf("data to be write on descriptor: %d\n", cur_fd);
                                        }
                                        if (m_fds[i].revents & POLLOUT) {
                                                printf("data to be read on descriptor: %d\n", cur_fd);
                                        }
                                }
                        }
                }else {

                        printf("poll result: %d, errno: %d\n", result, errno);

                }
        }
}

并输出:

data to be write on descriptor: 4
data to be write on descriptor: 4
data to be write on descriptor: 4
data to be write on descriptor: 4
data to be write on descriptor: 4
data to be write on descriptor: 4
data to be write on descriptor: 4
data to be write on descriptor: 4
data to be write on descriptor: 4
data to be write on descriptor: 4
data to be write on descriptor: 4
data to be write on descriptor: 4
data to be write on descriptor: 4
data to be write on descriptor: 4
data to be write on descriptor: 4
data to be write on descriptor: 4
data to be write on descriptor: 4
data to be write on descriptor: 4
data to be write on descriptor: 4
data to be write on descriptor: 4

我将超时设置为3秒,但我上面不断发出消息 - 如何解释?我正朝着正确的方向前进吗?

0 个答案:

没有答案