gpt4 book ai didi

c - select() 不等待任何变化

转载 作者:太空宇宙 更新时间:2023-11-04 01:13:22 26 4
gpt4 key购买 nike

我不太确定我做错了什么。我需要通过 select() 系统调用获取文件更改事件。所以问题是它不等待,并立即返回 TRUE 结果。看起来我没有将文件位置更改为文件末尾,但我确实这样做了。
这是代码

(defvar *fd-setsize* 1024)

(defvar *fd-bits-size* (/ *fd-setsize* sb-vm:n-machine-word-bits))

(defcstruct ccx-fd-set
(fds-bits :long :count #.*fd-bits-size*))

(defun ccx-strerror (errnum)
"Wrapper for strerror - return string describing error number"
(foreign-funcall "strerror" :int errnum :string))

(defun ccx-test (function-result &optional (caller nil caller-p))
"Wrapper for native functuions. It on succecc it will return function-result, but if not,
it will also report an error from errno."
(labels ((error-signal ()
(if caller-p
(warn "~a ~a" caller (ccx-strerror *errno*))
(warn "~a" (ccx-strerror *errno*)))))
(cond
((and (realp function-result)
(minusp function-result)) (error-signal)))
function-result))

(defun ccx-select (nfds readfds writefds exceptfds timeout)
(ccx-test (foreign-funcall "select" :int nfds :pointer readfds :pointer writefds
:pointer exceptfds :pointer timeout :int) "ccx-select"))

(defun ccx-pselect (nfds readfds writefds exceptfds timeout sigmask)
(ccx-test (foreign-funcall "pselect" :int nfds :pointer readfds :pointer writefds
:pointer exceptfds :pointer timeout :pointer sigmask :int) "ccx-pselect"))

;; #define __FD_SET(d, set) \
;; ((void) (__FDS_BITS (set)[__FDELT (d)] |= __FDMASK (d)))
(defmacro fd-set (offset fd-set)
(with-gensyms (word bit)
`(with-foreign-slots ((fds-bits) ,fd-set ccx-fd-set)
(multiple-value-bind (,word ,bit) (floor ,offset *fd-bits-size*)
(setf (mem-aref fds-bits :long ,word)
;; #define __FDMASK(d) ((__fd_mask) 1 << ((d) % __NFDBITS))
(logior (the (unsigned-byte #.*fd-bits-size*)
(ash 1 ,bit))
;; #define __FDELT(d) ((d) / __NFDBITS)
(mem-aref fds-bits :long ,word)
))))))

;; #define __FD_CLR(d, set) \
;; ((void) (__FDS_BITS (set)[__FDELT (d)] &= ~__FDMASK (d)))
(defmacro fd-clr (offset fd-set)
(with-gensyms (word bit)
`(with-foreign-slots ((fds-bits) ,fd-set ccx-fd-set)
(multiple-value-bind (,word ,bit) (floor ,offset sb-vm:n-machine-word-bits)
(setf (mem-aref fds-bits :long ,word)
(logand (the (unsigned-byte #.*fd-bits-size*)
(ash 1 ,bit))
(mem-ref fds-bits :long ,word)))))))

;; #define __FD_ISSET(d, set) \
;; ((__FDS_BITS (set)[__FDELT (d)] & __FDMASK (d)) != 0)
(defmacro fd-isset (offset fd-set)
(with-gensyms (word bit)
`(with-foreign-slots ((fds-bits) ,fd-set ccx-fd-set)
(multiple-value-bind (,word ,bit) (floor ,offset *fd-bits-size*)
;; (logbitp ,bit (mem-aref fds-bits :long ,word))
(logbitp ,bit (mem-aref fds-bits :long ,word))))))

;; # define __FD_ZERO(set) \
;; do { \
;; unsigned int __i; \
;; fd_set *__arr = (set); \
;; 4 * 32 = 128 / 4
;; for (__i = 0; __i < sizeof (fd_set) / sizeof (__fd_mask); ++__i) \
;; __FDS_BITS (__arr)[__i] = 0; \
;; } while (0)
(defmacro fd-zero (&rest fd-sets)
`(progn
,@(loop for el in fd-sets
collect `(with-foreign-slots ((fds-bits) ,el ccx-fd-set)
,@(loop for index upfrom 0 below *fd-bits-size*
collect `(setf (mem-aref fds-bits :long ,index) 0))))))


(defmacro fd-set-print (&rest fd-sets)
`(progn
,@(loop for el in fd-sets
collect `(progn
(format t "~&~s: " ',el)
,@(loop for index upfrom 0 below *fd-bits-size* collect
`(format t "~d " (mem-aref (foreign-slot-value ,el 'ccx-fd-set 'fds-bits) :long ,index))
finally (format t "~%"))))))

这是我的运行脚本

(with-open-file (fd "/tmp/test.txt")
(with-foreign-objects ((fd-set-r 'ccx-fd-set)
(fd-set-w 'ccx-fd-set)
(fd-set-x 'ccx-fd-set)
(timeout 'ccx-timespec))

(file-position fd (file-length fd))

(setf (foreign-slot-value timeout 'ccx-timespec 'ccx-tv-sec) 10)
(setf (foreign-slot-value timeout 'ccx-timespec 'ccx-tv-nsec) 0)

;; PRINTING && FLUSHING && PRINTING
(fd-set-print fd-set-r fd-set-w fd-set-x)
(fd-zero fd-set-r fd-set-w fd-set-x)
(fd-set-print fd-set-r fd-set-w fd-set-x)

(fd-set (sb-sys:fd-stream-fd fd) fd-set-r)
(fd-set (sb-sys:fd-stream-fd fd) fd-set-w)
(fd-set (sb-sys:fd-stream-fd fd) fd-set-x)
(fd-set-print fd-set-r fd-set-w fd-set-x)

(let ((res (ccx-select *fd-setsize* fd-set-r fd-set-w (null-pointer) timeout)))
(if (< res 0)
(format t "~& select () error")
(loop for index upfrom 0 below *fd-setsize* do
(progn
(when (fd-isset index fd-set-r)
(format t "~& fd ~d read" index))
(when (fd-isset index fd-set-w)
(format t "~& fd ~d write" index))
;; (when (fd-isset index fd-set-x)
;; (format t "~& fd ~d ex" index))
))))))

所以这是我的 C 示例,它对我也不起作用。

#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/select.h>
#include <fcntl.h>
#include <stdio.h>

int main ()
{
FILE * pFile;
long size;

pFile = fopen ("/tmp/test.txt","rb");
if (pFile==NULL) perror ("Error opening file");
else
{
while (1){
fseek (pFile, 0, SEEK_END);
size=ftell (pFile);
printf ("Size of myfile.txt: %ld bytes.\n",size);

fd_set fds;
struct timeval tv;


int fd = fileno(pFile);
FD_ZERO(&fds);
FD_SET(fd,&fds);
tv.tv_sec = 2;
tv.tv_usec = 0;
int rc = select(fd+1, &fds, NULL, NULL, &tv);

if (rc < 0) {
printf("failed\n");
/* continue; */
} else if (rc > 0 && FD_ISSET(fd,&fds)) {
printf("read\n");
} else {
printf("timeout\n");
/* continue; */
}

}
fclose (pFile);
}
return 0;
}

这个程序的结果是:

Size of test.txt: 2267 bytes.
read
Size of test.txt: 2267 bytes.
read

我的代码有什么问题?

最佳答案

你没有做错什么

select()/poll() 和类似的对常规文件不起作用。

“与常规文件关联的文件描述符总是选择 true 以准备读取、准备写入和错误条件。” - 来自 here

如果你想监控文件的变化,你可以使用gamin ,或依赖于操作系统的 API,例如 inotify在 Linux 上,kqueue在 BSD 上。

关于c - select() 不等待任何变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7022810/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com