- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在开发接受传入 TCP 连接的软件,但遇到了一些我不理解的问题。首先,我将解释该软件的基本功能。请记住,有些部分是临时的,我知道这很可能是一种糟糕的做事方式,但在原型(prototype)设计过程中我遇到了这个问题。
我让主进程为 SIGINT 建立一个信号处理程序。主进程然后启动一个新线程,将其称为“监听器”,使用 pthread_create() 默认值。监听器首先打开一个套接字,绑定(bind)、监听并设置套接字为非阻塞。然后监听器将使用 select() 轮询套接字,等待传入连接。
现在,如果我在主线程中有一个愚蠢的 while(1) 循环,我可以毫无问题地连接到套接字。问题是:如果我用 pause() 替换 while(1) 循环,我将无法再连接到套接字。我知道监听器线程通过日志消息仍然处于事件状态。同样,我不打算使用 pause(),但我只想知道发生了什么。
pause() 是否阻止某个信号到达子线程?
更新:我提供的精简代码似乎没有表现出相同的行为。如果我能确定原因,我会再次更新。
更新 2: 我发现了问题所在。我发布的代码和我的问题代码之间存在一个关键区别。以下是不同之处:
static void* listener_thread(void* arg)
{
int listen_port = *(int *)arg;
int listen_fd;
fd_set readSet;
int fdsMax, status;
struct timeval timeout;
if(open_listen_port(listen_port, &listen_fd) == -1)
...
int start_listener_thread(int port)
{
int status = 0;
if(0 > pthread_create(&thread_id, NULL, listener_thread, (void *)&port))
在 main.c 中:
if(0 == status && -1 == start_listener_thread(3000))
所以你可以看到我将端口号作为指向堆栈位置的指针传递给线程。这不是个好主意。奇怪的是,如果我将 pause() 更改为 while(1) 循环,它就会起作用。使用 pause() 时,端口号恰好是一个有效端口。
在start_listener_thread 中为端口号分配空间解决了这个问题。感谢一路上的帮助!
代码示例(精简):
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <pthread.h>
#include <fcntl.h>
#include <signal.h>
#include <sys/select.h>
#include <unistd.h>
pthread_t thread_id;
void sighandler(int signum)
{
}
int open_listen_port(int listenPort, int* listenFd)
{
struct sockaddr_in listenAddr;
int flags;
memset(&listenAddr, 0, sizeof(listenAddr));
listenAddr.sin_family = AF_INET;
listenAddr.sin_port = htons(listenPort);
listenAddr.sin_addr.s_addr = INADDR_ANY;
if( (*listenFd = socket(AF_INET, SOCK_STREAM, 0)) == -1 )
{
return(-1);
}
if( bind(*listenFd, (struct sockaddr*) &listenAddr,
sizeof(listenAddr)) == -1 )
{
return(-1);
}
if( listen(*listenFd, 16) == -1 )
{
return(-1);
}
// change listener to be non-blocking
flags = fcntl(*listenFd, F_GETFL);
if(fcntl(*listenFd, F_SETFL, flags | O_NONBLOCK) == -1)
{
return(-1);
}
return (0);
}
static void* listener_thread(void* arg)
{
int listen_fd;
fd_set readSet;
int fdsMax, status;
struct timeval timeout;
if(open_listen_port(3000, &listen_fd) == -1)
{
pthread_exit(NULL);
}
while(1)
{
FD_ZERO(&readSet);
fdsMax = 0;
timeout.tv_sec = 0;
timeout.tv_usec = 500000;
FD_SET(listen_fd, &readSet);
if(listen_fd > fdsMax)
{
fdsMax = listen_fd;
}
status = select(fdsMax + 1, &readSet, NULL, NULL, &timeout);
}
return NULL;
}
int start_listener_thread()
{
int status = 0;
if(0 > pthread_create(&thread_id, NULL, listener_thread, NULL))
{
status = -1;
}
return(status);
}
int main(int argc, char *argv[])
{
struct sigaction sigopt;
int status = 0;
memset(&sigopt, 0, sizeof(struct sigaction));
sigopt.sa_handler = sighandler;
if(0 != sigaction(SIGINT, &sigopt, NULL))
{
status = -1;
}
if(0 == status && -1 == start_listener_thread())
{
status = -1;
}
pause();
return(0);
}
最佳答案
来自 OS X 上的 man pause
:
DESCRIPTION
Pause is made obsolete by sigsuspend(2).
The pause() function forces a process to pause until a signal is received
from either the kill(2) function or an interval timer. (See
setitimer(2).) Upon termination of a signal handler started during a
pause(), the pause() call will return.
来自 Linux 上的 man pause
:
DESCRIPTION
pause() causes the calling process (or thread) to sleep until a signal
is delivered that either terminates the process or causes the invoca‐
tion of a signal-catching function.
两个手册页都暗示调用进程 将休眠。这解释了无法调用 accept()
。
你能确定 accept()
是否被调用了吗?您是否在适当的地方检查所有返回状态和 errno
?
我不确定休眠线程的目的是什么。如果您必须保持主线程处于事件状态,为什么不使用类似 sleep()
调用的 while 循环呢? (也许你打算稍后在此处添加代码以轮询某些内容?在这种情况下,使用 usleep()
和你想要检查的任何间隔,或者 sleep(1)
如果一次 -每秒的粒度就足够了吗?)或者只是在主线程上运行你的 select()
。
编辑: 看起来程序正在向我运行。我修改如下:
--- /tmp/foo.c 2014-02-11 16:43:04.000000000 -0800
+++ /tmp/foo.c 2014-02-11 16:46:17.000000000 -0800
@@ -7,6 +7,7 @@
#include <signal.h>
#include <sys/select.h>
#include <unistd.h>
+#include <stdio.h>
pthread_t thread_id;
@@ -76,6 +77,7 @@
}
status = select(fdsMax + 1, &readSet, NULL, NULL, &timeout);
+ printf("select() woke up\n");
}
return NULL;
}
当我进行上述更改时,它会每半秒打印一次 select() woke up
,直到我连接到套接字。然后重复打印它。
你能更好地描述你看到的行为吗?是否有阻塞调用,例如对套接字的读取或写入?
你能附加(或在里面运行)gdb
并找出每个线程在做什么吗?
关于c - 套接字被 pause() 阻塞,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21715804/
我使用下拉菜单提供一些不同的链接,但我希望这些链接在同一选项卡中打开,而不是在新选项卡中打开。这是我找到的代码,但我对 Javascript 非常缺乏知识 var urlmenu = docume
我对 javascript 不太了解。但我需要一个垂直菜单上的下拉菜单,它是纯 JavaScript,所以我从 W3 复制/粘贴脚本:https://www.w3schools.com/howto/t
我已经坐了 4 个小时,试图让我的导航显示下 zipper 接垂直,但它继续水平显示它们。我无法弄清楚为什么会发生这种情况或如何解决它。 如果有人能告诉我我做错了什么,我将不胜感激。我有一个潜移默化的
我正在尝试创建选项卡式 Accordion 样式下拉菜单。我使用 jQuery 有一段时间了,但无法使事件状态达到 100%。 我很确定这是我搞砸的 JS。 $('.service-button').
对于那些从未访问过 Dropbox 的人,这里是链接 https://www.dropbox.com/ 查看“登录”的下拉菜单链接。我如何创建这样的下 zipper 接? 最佳答案 这是 fiddle
我正在制作一个 Liferay 主题,但我在尝试设计导航菜单的样式时遇到了很多麻烦。我已经为那些没有像这样下拉的人改变了导航链接上的经典主题悬停功能: .aui #navigation .nav li
如果您将鼠标悬停在 li 上,则会出现一个下拉菜单。如果您将指针向下移至悬停时出现的 ul,我希望链接仍然带有下划线,直到您将箭头从 ul 或链接移开。这样你就知道当菜单下拉时你悬停在哪个菜单上。 知
我有一个带有多个下拉菜单的导航栏。因此,当我单击第一个链接时,它会打开下拉菜单,但是当我单击第二个链接时,第一个下拉菜单不会关闭。 (所以如果用户点击第二个链接我想关闭下拉菜单) // main.js
我正在尝试制作一个导航下拉菜单(使用 Bootstrap 3),其中链接文本在同一行上有多个不同的对齐方式。 在下面的代码中,下拉列表 A 中的链接在 HTML 中有空格字符来对齐它们,但是空白被忽略
我希望有人能帮我解决这个 Bootstrap 问题,因为我很困惑。 有人要求我在底部垂直对齐图像和其中包含图像的链接。 我面临的问题是他们还希望链接在链接/图像组合上具有 pull-right,这会杀
我正在构建一个 Rails 应用程序,并希望指向我的类的每个实例的“显示”页面的链接显示在“索引”页面的下拉列表中。我目前正在使用带有 options_from_collection_for_sele
我有以下 Bootstrap3 导航菜单 ( fiddle here )。我想设置“突出显示”项及其子链接与下拉列表 1 和 2 链接不同的链接文本(和悬停)的样式。我还希望能够以不同于 Highli
我对导航栏中的下拉菜单有疑问。对于普通的导航链接(无下拉菜单),我将菜单文本放在 H3 中,但是当我尝试对下 zipper 接执行相同操作时,箭头不在标题旁边,而是在标题下方。我决定用 span 替换
我是一名优秀的程序员,十分优秀!