线 执行顺序是可由调度器独立管理的最小编程指令序列。线程是进程的一个组件,因此一个进程中可以关联多个线程。Linux没有对每个进程单独设置线程数限制,但对系统上的进程总数有限制(因为在Linux上,线程只是具有共享地址空间的进程)。
null
我们的任务是找出单个进程中可以创建的最大线程数(pthread_create可以创建的最大线程数)。使用以下命令可以看到的最大线程数是ubuntu:
cat /proc/sys/kernel/threads-max
linux的线程限制可以在运行时通过将所需的限制写入 /proc/sys/kernel/threads max .
在ubuntu操作系统上编译以下程序,以检查在C中一个进程内可以创建的最大线程数。
cc filename.c -pthread where filename.c is the name with which file is saved.
// C program to find maximum number of thread within // a process #include<stdio.h> #include<pthread.h> // This function demonstrates the work of thread // which is of no use here, So left blank void * thread ( void *vargp){ } int main() { int err = 0, count = 0; pthread_t tid; // on success, pthread_create returns 0 and // on Error, it returns error number // So, while loop is iterated until return value is 0 while (err == 0) { err = pthread_create (&tid, NULL, thread , NULL); count++; } printf ( "Maximum number of thread within a Process" " is : %d" , count); } |
输出:
Maximum number of thread within a Process is : 32754
本文由 阿迪蒂亚·库马尔 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 贡献极客。组织 或者把你的文章寄到contribute@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。
如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END