linux线程是否共享进程所有空间

来源:互联网 发布:在线阅读word js插件 编辑:程序博客网 时间:2024/06/05 18:21

linux线程是否共享进程所有空间 [复制链接]
00

wkathy

稍有积蓄

Rank: 1

帖子
75
主题
14
精华
0
可用积分
260
信誉积分
123
专家积分
0
在线时间
4 小时
注册时间
2006-05-14
最后登录
2013-12-05
  • 问答
  • 好友
  • 博客
  • 消息
论坛徽章:
0
跳转到指定楼层
1楼[收藏(0)][报告]
 发表于 2008-07-15 22:20:12 |只看该作者 |倒序浏览
分享到:
unix环境高级编程第11章中说“进程的所有信息对该进程所有的线程都是共享的,包括可执行的程序文本、程序的全局内存和堆内存、栈、以及文件描述符”

栈中不是保存局部变量吗?局部变量其他的线程也能访问吗?我记得做过实验是不能访问的。
#include <stdio.h>
#include <pthread.h>
void* thread(void)
{
    int i,sum=0;
    for(i=1;i<=10;i++)
    {
        sum=sum+i;
        sleep(1);
    }
    printf("The sum is %d\n",sum);
}
int main(void)
{
    pthread_t id;
    int i,ret;
    char buf[80];
    ret=pthread_create(&id,NULL,thread,NULL);
    if(ret!=0)
    {
        printf ("Create pthread error!\n");
        exit (1);
    }
    while(1)
    {
        printf("please input a string\n");
        scanf("%s",buf);
        printf("This string is %s\n",buf);
    }
    pthread_join(id,NULL);
    return (0);
}

线程应该不能访问main局部变量吧?
怎么解释呢?
  • 本版精华
  • 热门专题
  • 文库|博客
  • (初学APUE的进来看看)《UNIX环境高级编程》单个源码编译方法
  • [原创] 用 C 语言编写一个网络蜘蛛来搜索网上出现的电子邮件地址
  • 程序员技术练级攻略
  • Linux下各类TCP网络服务器的实现源代码
  • 分享:原创文档《ELF格式解析》
  • 多线程程序中利用管道控制 select 行为
  • 怎么理解这个 *(void **)(&cosine) ?
  • 结构体里.是什么意思
  • LINUX应用程序开发指南 下载
  • LINUX与UNIX SHELL编程指南 下载
精诚所致,金石为开!
linux dhcp peizhi roc  |  关于Unix文件的软链接  |  求教这个命令什么意思,我是新手,刚刚学习 ...  |  sed -e "/grep/d" 是什么意思?谢谢 ...  |  
 
  
scutan

版主

Rank: 8Rank: 8

帖子
6964
主题
393
精华
12
可用积分
620
信誉积分
535
专家积分
788
在线时间
5364 小时
注册时间
2007-04-13
最后登录
2015-01-14
  • 问答
  • 好友
  • 博客
  • 消息
论坛徽章:
0
2楼[报告]
 发表于 2008-07-16 00:33:08 |只看该作者
是共享进程的所有空间,建议看看APUE第七章。
把自己角色扮演好
全力以赴每一秒!
应届毕业生找工作中!
合格的Linux运维工程师是如何炼成的? | TCP/HTTP 负载均衡利器-- HAProxy 如何检测购买软件是否留有后门? 网络基础建设之绕不开的无线网络
 
  
Sorehead

家境小康

Rank: 2

帖子
1001
主题
5
精华
0
可用积分
1401
信誉积分
108
专家积分
40
在线时间
277 小时
注册时间
2004-05-27
最后登录
2012-05-27
  • 问答
  • 好友
  • 博客
  • 消息
论坛徽章:
0
3楼[报告]
 发表于 2008-07-16 10:49:58 |只看该作者
不同线程使用不同的栈,但是由于在一个地址空间里,所以其它线程的局部变量还是可以通过地址来访问的,关键是你要找对地址。
合格的Linux运维工程师是如何炼成的? | TCP/HTTP 负载均衡利器-- HAProxy 如何检测购买软件是否留有后门? 网络基础建设之绕不开的无线网络
 
  
MMMIX

广告杀手

Rank: 8Rank: 8

帖子
17950
主题
151
精华
2
可用积分
156845
信誉积分
3649
专家积分
55
在线时间
5749 小时
注册时间
2005-11-28
最后登录
2016-04-01
  • 问答
  • 好友
  • 博客
  • 消息
论坛徽章:
87
程序设计版块每日发帖之星日期:2015-08-29 06:20:00程序设计版块每月发帖之星日期:2015-09-11 19:31:44程序设计版块每日发帖之星日期:2015-09-13 06:20:00每日论坛发贴之星日期:2015-09-13 06:20:00程序设计版块每日发帖之星日期:2015-09-14 06:20:00每日论坛发贴之星日期:2015-09-14 06:20:00程序设计版块每日发帖之星日期:2015-09-17 06:20:00程序设计版块每日发帖之星日期:2015-09-18 06:20:002015亚冠之阿尔艾因日期:2015-09-18 10:35:08月度论坛发贴之星日期:2015-09-30 22:25:00程序设计版块每周发帖之星日期:2015-09-11 19:26:04程序设计版块每周发帖之星日期:2015-09-11 19:25:45程序设计版块每日发帖之星日期:2015-08-29 06:20:00程序设计版块每日发帖之星日期:2015-09-01 06:20:00每日论坛发贴之星日期:2015-09-01 06:20:00
4楼[报告]
 发表于 2008-07-17 22:12:09 |只看该作者
原帖由 scutan 于 2008-7-16 00:33 发表 
是共享进程的所有空间,建议看看APUE第七章。

栈空间是独立的,还后 thread local variable 占用的空间也是独立的。
I can explain it for you, but I can’t understand it for you.
合格的Linux运维工程师是如何炼成的? | TCP/HTTP 负载均衡利器-- HAProxy 如何检测购买软件是否留有后门? 网络基础建设之绕不开的无线网络
 
  
scutan

版主

Rank: 8Rank: 8

帖子
6964
主题
393
精华
12
可用积分
620
信誉积分
535
专家积分
788
在线时间
5364 小时
注册时间
2007-04-13
最后登录
2015-01-14
  • 问答
  • 好友
  • 博客
  • 消息
论坛徽章:
0
5楼[报告]
 发表于 2008-07-17 23:34:02 |只看该作者
原帖由 MMMIX 于 2008-7-17 22:12 发表 

栈空间是独立的,还后 thread local variable 占用的空间也是独立的。


内核栈是独立的,但是用户态的栈是在同一个地址段的,不同线程使用相同的页表。所以某个线程仍然可以访问另外一个线程的内部数据。
比如说定义一个全局变量:
int *a
用来保存某个线程局部变量的地址,然后另外一个线程就可以通过这个地址去改那个线程局部变量的值。
把自己角色扮演好
全力以赴每一秒!
应届毕业生找工作中!
合格的Linux运维工程师是如何炼成的? | TCP/HTTP 负载均衡利器-- HAProxy 如何检测购买软件是否留有后门? 网络基础建设之绕不开的无线网络
 
  
ice-pl

白手起家

帖子
27
主题
0
精华
0
可用积分
50
信誉积分
100
专家积分
0
在线时间
1 小时
注册时间
2006-06-06
最后登录
2010-02-07
  • 问答
  • 好友
  • 博客
  • 消息
论坛徽章:
0
6楼[报告]
 发表于 2008-07-17 23:43:08 |只看该作者
下面部分代码摘自glibc2.7
pthread_create.c

int
__pthread_create_2_1 (newthread, attr, start_routine, arg)
     pthread_t *newthread;
     const pthread_attr_t *attr;
     void *(*start_routine) (void *);
     void *arg;
{
  STACK_VARIABLES;

  const struct pthread_attr *iattr = (struct pthread_attr *) attr;
  if (iattr == NULL)
    /* Is this the best idea?  On NUMA machines this could mean
       accessing far-away memory.  */
    iattr = &default_attr;

  struct pthread *pd = NULL;
  int err = ALLOCATE_STACK (iattr, &pd);
  if (__builtin_expect (err != 0, 0))
    /* Something went wrong.  Maybe a parameter of the attributes is
       invalid or we could not allocate memory.  */
    return err;


  /* Initialize the TCB.  All initializations with zero should be
     performed in 'get_cached_stack'.  This way we avoid doing this if
     the stack freshly allocated with 'mmap'.  */

#ifdef TLS_TCB_AT_TP
  /* Reference to the TCB itself.  */
  pd->header.self = pd;

  /* Self-reference for TLS.  */
  pd->header.tcb = pd;
#endif

  /* Store the address of the start routine and the parameter.  Since
     we do not start the function directly the stillborn thread will
     get the information from its thread descriptor.  */
  pd->start_routine = start_routine;
  pd->arg = arg;

  /* Copy the thread attribute flags.  */
  struct pthread *self = THREAD_SELF;
  pd->flags = ((iattr->flags & ~(ATTR_FLAG_SCHED_SET | ATTR_FLAG_POLICY_SET))
               | (self->flags & (ATTR_FLAG_SCHED_SET | ATTR_FLAG_POLICY_SET)));

  /* Initialize the field for the ID of the thread which is waiting
     for us.  This is a self-reference in case the thread is created
     detached.  */
  pd->joinid = iattr->flags & ATTR_FLAG_DETACHSTATE ? pd : NULL;

  /* The debug events are inherited from the parent.  */
  pd->eventbuf = self->eventbuf;


  /* Copy the parent's scheduling parameters.  The flags will say what
     is valid and what is not.  */
  pd->schedpolicy = self->schedpolicy;
  pd->schedparam = self->schedparam;

  /* Copy the stack guard canary.  */
#ifdef THREAD_COPY_STACK_GUARD
  THREAD_COPY_STACK_GUARD (pd);
#endif


可以看出线程创建时要分配stack space

http://blog.chinaunix.net/u2/69492/showart_970922.html
里面有陈述:
Before understanding a thread, one first needs to understand a UNIX process. A process is created by the operating system, and requires a fair amount of "overhead". Processes contain information about program resources and program execution state, including: 
Process ID, process group ID, user ID, and group ID 
Environment 
Working directory. 
Program instructions 
Registers 
Stack 
Heap 
File descriptors 
Signal actions 
Shared libraries 
Inter-process communication tools (such as message queues, pipes, semaphores, or shared memory). 
  
UNIX PROCESS THREADS WITHIN A UNIX PROCESS 



Threads use and exist within these process resources, yet are able to be scheduled by the operating system and run as independent entities largely because they duplicate only the bare essential resources that enable them to exist as executable code. 

This independent flow of control is accomplished because a thread maintains its own: 
Stack pointer 
Registers 
Scheduling properties (such as policy or priority) 
Set of pending and blocked signals 
Thread specific data. 

So, in summary, in the UNIX environment a thread: 
Exists within a process and uses the process resources 
Has its own independent flow of control as long as its parent process exists and the OS supports it 
Duplicates only the essential resources it needs to be independently schedulable 
May share the process resources with other threads that act equally independently (and dependently) 
Dies if the parent process dies - or something similar 
Is "lightweight" because most of the overhead has already been accomplished through the creation of its process. 

Because threads within the same process share resources: 
Changes made by one thread to shared system resources (such as closing a file) will be seen by all other threads. 
Two pointers having the same value point to the same data. 
Reading and writing to the same memory locations is possible, and therefore requires explicit synchronization by the programmer. 

另外我们也可以访问其他进程的stack,gdb用系统调用ptrace来在用户态调试其他进程,访问其他进程的寄存器等资源。
合格的Linux运维工程师是如何炼成的? | TCP/HTTP 负载均衡利器-- HAProxy 如何检测购买软件是否留有后门? 网络基础建设之绕不开的无线网络
 
  
MMMIX

广告杀手

Rank: 8Rank: 8

帖子
17950
主题
151
精华
2
可用积分
156845
信誉积分
3649
专家积分
55
在线时间
5749 小时
注册时间
2005-11-28
最后登录
2016-04-01
  • 问答
  • 好友
  • 博客
  • 消息
论坛徽章:
87
程序设计版块每日发帖之星日期:2015-08-29 06:20:00程序设计版块每月发帖之星日期:2015-09-11 19:31:44程序设计版块每日发帖之星日期:2015-09-13 06:20:00每日论坛发贴之星日期:2015-09-13 06:20:00程序设计版块每日发帖之星日期:2015-09-14 06:20:00每日论坛发贴之星日期:2015-09-14 06:20:00程序设计版块每日发帖之星日期:2015-09-17 06:20:00程序设计版块每日发帖之星日期:2015-09-18 06:20:002015亚冠之阿尔艾因日期:2015-09-18 10:35:08月度论坛发贴之星日期:2015-09-30 22:25:00程序设计版块每周发帖之星日期:2015-09-11 19:26:04程序设计版块每周发帖之星日期:2015-09-11 19:25:45程序设计版块每日发帖之星日期:2015-08-29 06:20:00程序设计版块每日发帖之星日期:2015-09-01 06:20:00每日论坛发贴之星日期:2015-09-01 06:20:00
7楼[报告]
 发表于 2008-07-18 21:52:09 |只看该作者
原帖由 scutan 于 2008-7-17 23:34 发表 


内核栈是独立的,但是用户态的栈是在同一个地址段的,不同线程使用相同的页表。所以某个线程仍然可以访问另外一个线程的内部数据。
比如说定义一个全局变量:
int *a
用来保存某个线程局部变量的地址,然 ...

是否共享和能不能访问没有什么关系。若线程的栈是共享的,那多个线程应该公用一个栈,这样不乱套了?
I can explain it for you, but I can’t understand it for you.
合格的Linux运维工程师是如何炼成的? | TCP/HTTP 负载均衡利器-- HAProxy 如何检测购买软件是否留有后门? 网络基础建设之绕不开的无线网络
 
  
scutan

版主

Rank: 8Rank: 8

帖子
6964
主题
393
精华
12
可用积分
620
信誉积分
535
专家积分
788
在线时间
5364 小时
注册时间
2007-04-13
最后登录
2015-01-14
  • 问答
  • 好友
  • 博客
  • 消息
论坛徽章:
0
8楼[报告]
 发表于 2008-07-18 22:24:07 |只看该作者
原帖由 MMMIX 于 2008-7-18 21:52 发表 

是否共享和能不能访问没有什么关系。若线程的栈是共享的,那多个线程应该公用一个栈,这样不乱套了?


呵呵, 我误解了您之前的意思, 您的意思是说栈共享, 而我的意思是说线程共享地址空间, 是我开始没有理解正确. 确实, 它们不能够共享栈, 只是共享地址空间.
把自己角色扮演好
全力以赴每一秒!
应届毕业生找工作中!
合格的Linux运维工程师是如何炼成的? | TCP/HTTP 负载均衡利器-- HAProxy 如何检测购买软件是否留有后门? 网络基础建设之绕不开的无线网络
 
  
MMMIX

广告杀手

Rank: 8Rank: 8

帖子
17950
主题
151
精华
2
可用积分
156845
信誉积分
3649
专家积分
55
在线时间
5749 小时
注册时间
2005-11-28
最后登录
2016-04-01
  • 问答
  • 好友
  • 博客
  • 消息
论坛徽章:
87
程序设计版块每日发帖之星日期:2015-08-29 06:20:00程序设计版块每月发帖之星日期:2015-09-11 19:31:44程序设计版块每日发帖之星日期:2015-09-13 06:20:00每日论坛发贴之星日期:2015-09-13 06:20:00程序设计版块每日发帖之星日期:2015-09-14 06:20:00每日论坛发贴之星日期:2015-09-14 06:20:00程序设计版块每日发帖之星日期:2015-09-17 06:20:00程序设计版块每日发帖之星日期:2015-09-18 06:20:002015亚冠之阿尔艾因日期:2015-09-18 10:35:08月度论坛发贴之星日期:2015-09-30 22:25:00程序设计版块每周发帖之星日期:2015-09-11 19:26:04程序设计版块每周发帖之星日期:2015-09-11 19:25:45程序设计版块每日发帖之星日期:2015-08-29 06:20:00程序设计版块每日发帖之星日期:2015-09-01 06:20:00每日论坛发贴之星日期:2015-09-01 06:20:00
9楼[报告]
 发表于 2008-07-18 23:09:10 |只看该作者
原帖由 scutan 于 2008-7-18 22:24 发表 


呵呵, 我误解了您之前的意思, 您的意思是说栈共享, 而我的意思是说线程共享地址空间, 是我开始没有理解正确. 确实, 它们不能够共享栈, 只是共享地址空间.

你就别用“您”了,我听者老别扭了。当然,如果你是故意的那另说 
I can explain it for you, but I can’t understand it for you.
合格的Linux运维工程师是如何炼成的? | TCP/HTTP 负载均衡利器-- HAProxy 如何检测购买软件是否留有后门? 网络基础建设之绕不开的无线网络
 
  
scutan

版主

Rank: 8Rank: 8

帖子
6964
主题
393
精华
12
可用积分
620
信誉积分
535
专家积分
788
在线时间
5364 小时
注册时间
2007-04-13
最后登录
2015-01-14
  • 问答
  • 好友
  • 博客
  • 消息
论坛徽章:
0
10楼[报告]
 发表于 2008-07-18 23:15:17 |只看该作者
原帖由 MMMIX 于 2008-7-18 23:09 发表 

你就别用“您”了,我听者老别扭了。当然,如果你是故意的那另说 


呵呵,我一般只对尊敬的人说“您”。因为我刚来CU时从你那儿学到了很多的东西,所以比较尊敬你!
把自己角色扮演好
全力以赴每一秒!
应届毕业生找工作中!
0 0