线程和进程是面试中最常遇见的问题。有一个问题就是线程之间哪些东西是共享的。线程共享进程的整个地址空间,共享打开的文件,建立的socket等。线程有独立的栈以及一些寄存器,用来进行调度。堆,数据区和代码区是共享的。
地址空间
Linux下32位系统进程地址空间有4G,1G是内核地址,3G属于自己,这3G内存由所有线程共享。这3G内存也包含了栈,那么为什么说线程有自己独立的栈呢?
看下面的程序
- #include <stdio.h>
- #include <pthread.h>
- #include <sys/types.h>
- #include <unistd.h>
- int global;
- int *heap;
- int *stack_address; //线程1将自己栈中的变量地址赋给stack_address,然后线程2访问这个地址
- void * run1(void *param)
- {
- int stack_v = 123;
- global = 5;
- heap = malloc(12);
- heap[0] = 2;
- heap[1] = -3;
- stack_address = &stack_v;
- while(1) {}
- }
- void * run2(void *param)
- {
- printf("global %d\n",global);
- printf("heap[1] %d\n",heap[1]);
- printf("stack_v %d\n",*stack_address);
- }
- int main()
- {
- pthread_t tid1,tid2;
- pthread_attr_t attr;
- pthread_attr_init(&attr);
- pthread_create(&tid1,&attr,run1,NULL);
- pthread_create(&tid2,&attr,run2,NULL);
- pthread_join(tid1,NULL);
- pthread_join(tid2,NULL);
- return 0;
- }
运行结果:
可以看出,线程2和线程1的栈是位于同一个地址空间的,都是进程的地址空间。
线程的栈实际上是在进程的栈里的,每个都是独立的。
可以使用pthread_attr_getstacksize获取线程栈的大小。
- #include <stdio.h>
- #include <pthread.h>
- #include <sys/types.h>
- #include <unistd.h>
- int global;
- int *heap;
- int *stack_address;
- pthread_attr_t attr1,attr2;
- void * run1(void *param)
- {
- int stack_v = 123;
- size_t stacksize;
- pthread_attr_getstacksize(&attr1,&stacksize);
- printf("stacksize of thread1 is %d MB\n",stacksize/(1024*1024));
- global = 5;
- heap = malloc(12);
- heap[0] = 2;
- heap[1] = -3;
- stack_address = &stack_v;
- sleep(3);
- }
- void * run2(void *param)
- {
- size_t stacksize;
- printf("global %d\n",global);
- printf("heap[1] %d\n",heap[1]);
- printf("stack_v %d\n",*stack_address);
- pthread_attr_getstacksize(&attr2,&stacksize);
- printf("stacksize of thread2 is %d MB\n",stacksize/(1024*1024));
- }
- void fun()
- {
- static calltime = 1;
- char dummy[1024*1024];
- printf("calltime=%d\n",calltime);
- calltime ++;
- fun();
- }
- int main()
- {
- pthread_t tid1,tid2;
- pthread_attr_init(&attr1);
- pthread_attr_init(&attr2);
- pthread_create(&tid1,&attr1,run1,NULL);
- pthread_create(&tid2,&attr2,run2,NULL);
- pthread_join(tid1,NULL);
- pthread_join(tid2,NULL);
- fun();
- return 0;
- }
运行结果:
可以看到线程1,2和主线程的栈的大小都是10M,调用了11次才崩溃是因为有一些对齐以及段与段之间有一些gap。
我当前的ulimit将栈的大小设置成了10M。可以使用pthread_attr_setstacksize在创建线程的时候设置线程的栈大小。
因此,对于线程有自己独立的栈空间的真正理解应该是:线程的栈位于同一个地址空间里,但是互相不重叠,线程属性里有它的栈的起止地址,一个线程可以读写其它线程的栈,只要它知道地址。
打开的文件(包括socket)
对于两个进程,它们分别打开了一个文件,进程1得到的文件描述符是fd1,进程2得到的是fd2。即使进程1通过某种手段将fd1传到进程2中,进程2也访问不了fd1对应的文件,因为进程间文件描述符是独立的。fd1可能等于fd2,但是它们不对应同一个文件。
对于线程就不一样了。线程1得到了描述符fd1,那么线程2就可以通过fd1访问对应的文件,即使线程1不告诉线程2,线程2自己通过某种手段知道了,它也能访问。
每个进程有一个文件描述符空间,就类似于地址空间,线程也是共享这个文件描述符空间的。
信号
如果向一个进程发送信号,而这个进程有多个线程。那么发给哪个呢?还是所有的线程都能收到?
实际上,进程维护一个统一的信号队列,给这个进程发送一个信号,信号被存到信号队列里了,所有线程都可以看到。当调度到某个线程时,它发现队列里有信号,它拿出来处理了,这样其它线程进入被调度时已经看不到这个信号了。
看下面这个例子:
- #include <stdio.h>
- #include <pthread.h>
- #include <sys/types.h>
- #include <unistd.h>
- #include <signal.h>
- void sigroutine1(int no)
- {
- printf("thread1 received a signal\n");
- }
- void sigroutine2(int no)
- {
- printf("thread2 received a signal\n");
- }
- void * run1(void *param)
- {
- while(1)
- {
- signal(SIGHUP,sigroutine1);
- }
- }
- void * run2(void *param)
- {
- while(1)
- {
- signal(SIGHUP,sigroutine2);
- }
- }
- int main()
- {
- pthread_t tid1,tid2;
- pthread_attr_t attr;
- pthread_attr_init(&attr);
- pthread_attr_init(&attr);
- pthread_create(&tid1,&attr,run1,NULL);
- pthread_create(&tid2,&attr,run2,NULL);
- pthread_join(tid1,NULL);
- pthread_join(tid2,NULL);
- return 0;
- }
两个线程都在等待一个信号,我们向这个进程发送信号。
每发送一个信号,只有一个线程收到了,而且不一定是哪个线程。
(上述内容仅仅是我自己实验得到的结论,不一定正确)
本文转自nxlhero 51CTO博客,原文链接:http://blog.51cto.com/nxlhero/1179189,如需转载请自行联系原作者
