创建子进程内螺纹 [英] creating thread inside child process
本文介绍了创建子进程内螺纹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
#include<pthread.h>
#include<stdio.h>
int value=0;
void *runner(void *param);
int main(int argc,char *argv[])
{
int pid;
pthread_t tid;
pthread_attr_t attr;
pid=fork();
if(pid==0){
pthread_attr_init(&attr);
pthread_create(&tid,&attr,runner,NULL);
pthread_join(tid,NULL);
printf("CHILD VALUE=%d",value);
}
else if(pid>0){
wait(NULL);
printf("PARENT VALUE=%d",value);
}
}
void *runner(void *param){
value=5;
pthread_exit(0);
}
什么是儿童和家长的价值?
将儿童和由它创建线程共享数据?所以输出将是5和0?
what is the value of child and parent?? will the child and the thread created by it share the data? so output will be 5 and 0?
推荐答案
什么情况是这样的:
- 子进程是通过复制父的存储器空间中创建。它会看到相同的数据为母公司,但它只是一个副本,因此它们不能与海誓山盟干涉。 (或其任何线程!)的子进程进行的任何后续更改将只看到自己。
- 由儿童催生了线程股与子进程的主线程的内存空间,并再次看到了值的副本在父进程。
因此,由于在由子衍生线程分配值将只子进程内可见,但不是父进程内,结果是5和0
Hence, since the value assigned in the thread spawned by the child will only be visible inside the child process, but not inside the parent process, the results are 5 and 0.
这篇关于创建子进程内螺纹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文