关于线程的创建与使用,前几个博客说的很详细了,那么我们如果在某一个函数线程中调用fork函数有什么需要注意的吗?
一:线程中使用fork函数
在线程中调用 fork 函数,子进程只会启用调用 fork 函数的那条线程,其他线程不会启用。
示例代码:
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
#include<assert.h>
#include<pthread.h>
//测试线程中调用frok的结果
void *fun(void *arg)
{
int i=0;
int n=fork();
if(n==0)
{
for(;i<4;i++)
{
sleep(1);
printf("%d pig heng heng heng\n",getpid());
}
}
else
{
for(;i<3;i++)
{
sleep(1);
printf("%d cat miao miao miao\n",getpid());
}
}
}
int main()
{
pthread_t pthid;
int res=pthread_create(&pthid,0,