当前位置: 技术问答>linux和unix
请问:在驱动中实现非阻塞UDP,用interruptible_sleep_on_timeout行么?
来源: 互联网 发布时间:2015-11-03
本文导语: 比如如下的循环过程,如果我有100个线程做这样的操作,系统资源会不会吃紧呢? while (!G_finish) { doecho(s); interruptible_sleep_on_timeout(&wq, 10); } ========== void doecho(struct socket *s) { struct msghdr msg; struct iovec iov; ...
比如如下的循环过程,如果我有100个线程做这样的操作,系统资源会不会吃紧呢?
while (!G_finish) {
doecho(s);
interruptible_sleep_on_timeout(&wq, 10);
}
==========
void doecho(struct socket *s)
{
struct msghdr msg;
struct iovec iov;
int len;
char *buf;
struct sockaddr_in sin;
mm_segment_t oldfs;
if (skb_queue_empty(&(s ->sk -> receive_queue))) {
printk(".");
return;
}
buf = (char *)kmalloc(4096, GFP_KERNEL);
msg.msg_name = (void *)&sin;
msg.msg_namelen = sizeof(sin);
msg.msg_iov = &iov;
msg.msg_iovlen = 1;
msg.msg_controllen = 0;
msg.msg_flags =0;
msg.msg_iov -> iov_base = buf;
msg.msg_iov -> iov_len = (size_t) 4095;
len = 0;
oldfs = get_fs();
set_fs(get_ds());
len = sock_recvmsg(s, &msg, 4095, MSG_DONTWAIT);
buf[len] = 0;
printk("recv: %s : %dn", buf, len);
set_fs(oldfs);
if ((len 4094))
goto outdo;
oldfs = get_fs();
set_fs(get_ds());
msg.msg_control = NULL;
msg.msg_controllen = 0;
msg.msg_iov -> iov_base = buf;
msg.msg_iov -> iov_len = len;
msg.msg_flags = MSG_NOSIGNAL;
len = sock_sendmsg(s, &msg, len);
set_fs(oldfs);
outdo:
kfree(buf);
}
以前在应用程序里是用select来做的,可是不是select不支持在驱动里使用呢?
因为select调用的头文件是sysselect,我编译的时候会有出错信息.
谢谢指点一二了 :)
while (!G_finish) {
doecho(s);
interruptible_sleep_on_timeout(&wq, 10);
}
==========
void doecho(struct socket *s)
{
struct msghdr msg;
struct iovec iov;
int len;
char *buf;
struct sockaddr_in sin;
mm_segment_t oldfs;
if (skb_queue_empty(&(s ->sk -> receive_queue))) {
printk(".");
return;
}
buf = (char *)kmalloc(4096, GFP_KERNEL);
msg.msg_name = (void *)&sin;
msg.msg_namelen = sizeof(sin);
msg.msg_iov = &iov;
msg.msg_iovlen = 1;
msg.msg_controllen = 0;
msg.msg_flags =0;
msg.msg_iov -> iov_base = buf;
msg.msg_iov -> iov_len = (size_t) 4095;
len = 0;
oldfs = get_fs();
set_fs(get_ds());
len = sock_recvmsg(s, &msg, 4095, MSG_DONTWAIT);
buf[len] = 0;
printk("recv: %s : %dn", buf, len);
set_fs(oldfs);
if ((len 4094))
goto outdo;
oldfs = get_fs();
set_fs(get_ds());
msg.msg_control = NULL;
msg.msg_controllen = 0;
msg.msg_iov -> iov_base = buf;
msg.msg_iov -> iov_len = len;
msg.msg_flags = MSG_NOSIGNAL;
len = sock_sendmsg(s, &msg, len);
set_fs(oldfs);
outdo:
kfree(buf);
}
以前在应用程序里是用select来做的,可是不是select不支持在驱动里使用呢?
因为select调用的头文件是sysselect,我编译的时候会有出错信息.
谢谢指点一二了 :)
|
你要在驱动里实现无阻塞udp?既然是无阻塞那为什么还要sleep呢?