Linux 2.6.30 uClibc 0.9.29 pthread_创建两个线程而不是一个线程
创建线程时出现了非常奇怪的情况,下面是测试代码:Linux 2.6.30 uClibc 0.9.29 pthread_创建两个线程而不是一个线程,c,linux,pthreads,C,Linux,Pthreads,创建线程时出现了非常奇怪的情况,下面是测试代码: #include <stdint.h> /* C99 types */ #include <stdbool.h> /* bool type */ #include <stdio.h> /* printf, fprintf, snprintf, fopen, fputs */ #include <string.h> /* memset
#include <stdint.h> /* C99 types */
#include <stdbool.h> /* bool type */
#include <stdio.h> /* printf, fprintf, snprintf, fopen, fputs */
#include <string.h> /* memset */
#include <signal.h> /* sigaction */
#include <time.h> /* time, clock_gettime, strftime, gmtime */
#include <sys/time.h> /* timeval */
#include <unistd.h> /* getopt, access */
#include <stdlib.h> /* atoi, exit */
#include <errno.h> /* error messages */
#include <math.h> /* modf */
#include <assert.h>
#include <sys/socket.h> /* socket specific definitions */
#include <netinet/in.h> /* INET constants and stuff */
#include <arpa/inet.h> /* IP address conversion stuff */
#include <netdb.h> /* gai_strerror */
#include <pthread.h>
void thread_up(void) {
printf("thread ....\n");
int loop=0;
while(loop<=7)
{
printf("loop...\n");
sleep(10);
loop++;
}
printf("Exit loop\n");
}
int main()
{
pthread_t thrid_up=0;
int i = pthread_create( &thrid_up, NULL, (void * (*)(void *))thread_up, NULL);
if (i != 0) {
printf("ERROR: [main] impossible to create upstream thread\n");
exit(1);
}
while(1)
{
sleep(10);
}
}
有什么提示吗?
ps
显示的是进程,而不是线程
您可能只使用/test&
启动了3次应用程序
要测试它,请使用以下命令终止所有进程:killall test
因此,再次启动您的流程,并查看以下输出:ps-A | grep test
它将向您显示一个test
然后使用:ps-eLf | grep test
您将看到test
的线程
编辑 与@Serhio answer相关,这可能是正确的,通过查看uClibc源代码,您可以在libpthread目录中找到使用了LinuxThreads,因此添加了一个线程管理器。只是一个注释 LinuxThreads 此实现的显著特点如下:
- 除了主(初始)线程和
程序使用
创建,实现 创建一个“管理器”线程。此线程处理线程创建 和终止。(如果此线程不可用,可能会导致问题。) 意外死亡。)pthread\u create(3)
- 线程不共享进程ID。(实际上,LinuxThreads
线程被实现为共享更多信息的进程
比通常情况下,但不共享公共进程ID。)
LinuxThreads线程(包括管理器线程)显示为
使用
分离流程ps(1)
ps
打印线程,而不仅仅是进程,并在探测期间仔细检查您的程序是否只有一个已启动实例
另外,您应该检查您的uClibc是否真正使用LinuxThreads而不是NPTL。因此,我认为观察三条线索的机会是存在的,但非常小。是的。。。对(@Serhio和@LPs)。。。我的uClibc使用我刚刚检查过的管理器
不幸的是,由于tyny linux嵌入式环境,我的ps非常差
ps
默认情况下不显示线程,AFAIK。你在用什么特殊的标志运行它吗?你确定你不只是运行了三个程序实例吗?如果放弃对pthread\u create()
的调用,会发生什么?我看不出有什么理由结束这个问题。这是一个完全正确的问题。有人欠这家伙一张赞成票。也许你是对的,我用来源链接更新了我的答案+1你为什么不一直阅读(并复制/粘贴)该页面?!线程不共享进程ID。(实际上,LinuxThreads线程被实现为比通常共享更多信息的进程,但不共享公共进程ID。)LinuxThreads线程(包括管理器线程)使用ps(1)作为单独的进程可见。“ps
显示进程,而不是线程。”,对LinuxThreads的情况并非如此。每个线程都有自己的PID(是的,PID),这就是OP看到输出的原因。@JonathonReinhart很高兴知道。谢谢我今天学到了一些东西;)
30214 root 704 S ./test
30215 root 704 S ./test
30216 root 704 S ./test