首页 新闻 会员 周边

linux下fork, pthread_create问题

0
悬赏园豆:10 [待解决问题]

int main(int argc, char** argv)
{
pid_t pid;
long i, j;
long process_count = atol(argv[1]);
long thread_count = atol(argv[2]);
long loop_count = atol(argv[3]);

count = 10;
printf("process_count = %lu, thread_count = %lu, loop_count = %lu\n", \
process_count, thread_count, loop_count);

if (argc!=4) exit(1);

for (int i = 0; i<process_count; i++)
{
pid = fork();
if (pid <0)
{
printf("fork fail\n");
}
else if (pid == 0)
{
printf("fork pid == 0\n");
pthread_t tid[thread_count];
for (j = 0; j<thread_count; j++)
{
pthread_create(&tid[j],NULL,work, NULL);
printf("create thread %d succ!\n", tid[j]);
}

for (j = 0; j<thread_count; j++)
{
pthread_join(tid[j],NULL);
}
return 0; //必须加这个,否则fork次数错误。这是问什么呢?

}
}

for (i = 0; i<process_count; i++)
{
pid = wait(NULL);
printf("wait\n");
}

return 0;
}

清心朗静的主页 清心朗静 | 初学一级 | 园豆:187
提问于:2014-02-14 17:10
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册