编写并执行一个C或C++程序,该程序可创建无限数量的进程
Write and execute a C or C++ program that creates an infinite number of processes.
我有一个C++程序,它在LINUX中创建进程线程。如何修改此代码以创建无限数量的进程?我的计数当前设置为5。这是我的代码:
#include <iostream>
#include <cstdlib>
#include <pthread.h>
#include <stdint.h>
#include <inttypes.h>
using namespace std;
#define THREAD_COUNT 5
void *PrintPhrase(void *threadid)
{
long tid;
tid = (long)threadid;
cout << "THis Is A Great Day Thread ID, " << tid << endl;
pthread_exit(NULL);
}
int main ()
{
pthread_t threads[THREAD_COUNT];
int rc;
uintptr_t i;
for( i=0; i < THREAD_COUNT; i++ ){
cout << "main() : creating thread, " << i << endl;
rc = pthread_create(&threads[i], NULL,
PrintPhrase, (void *)i);
if (rc){
cout << "Error:unable to create thread," << rc << endl;
exit(-1);
}
}
pthread_exit(NULL);
}
对于无限,我建议进行以下更改:
用while (true)
替换for
循环。
替换:
pthread_t threads[THREAD_COUNT];
带有:
std::vector<pthread_t> threads;
你的循环看起来像:
while (true)
{
pthread temp;
rc = pthread_create(&temp, NULL,
PrintPhrase, (void *)i);
if (rc){
cout << "Error:unable to create thread," << rc << endl;
return EXIT_FAILURE;
}
threads.push_back(temp);
}
相关文章:
- 如何检查第三个 API 是否在 Linux 中为 c/c++ 程序创建了一个新线程?
- 从头开始为应用程序创建 docker 映像是否有意义?
- 如何查看在程序级别为我的程序创建了多少 vtable 和 vpointer
- 如何在运行时对程序创建的.sys文件设置文件版本
- 从函数应用程序创建 std::vector
- 将 Qml/C++ 应用程序创建为插件
- 如何为带有计时功能的程序创建计时器?
- 使用Visual C ,卡在程序创建分配中
- 从Ubuntu中的CPP程序创建新文件
- 通过编译应用程序创建Windows服务
- 我们可以使用mfc应用程序创建pdf文件吗
- 使用可执行应用程序创建和"Scheduling" pthreads
- 通过 JNI 从应用程序创建 JVM 后C++找不到类
- 如何为多线程应用程序创建全局对象
- 为C++ Linux 应用程序创建隐藏的配置文件
- C 程序创建SIN COS和切线值
- MongoDB使用C 驱动程序创建稀疏索引
- 如何以编程方式为应用程序创建文件夹,以使用 BOX REST API 在其中上传内容
- 有没有办法从 C 程序创建数据收集器集
- 从C++应用程序创建Mac OS X的安装程序