#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h> void *thread_foo_func(void *);
void *thread_bar_func(void *); int global = ; int main(){
int local = ;
int foo, bar;
pthread_t fthread, bthread;
foo = pthread_create(&fthread, NULL, thread_foo_func, (void *)&local);
bar = pthread_create(&bthread, NULL, thread_bar_func, (void *)&local);
if (foo != || bar != ){
printf("thread creation failed.\n");
return -;
} foo = pthread_join(fthread, NULL);
bar = pthread_join(bthread, NULL);
if (foo != || bar != ){
printf("thread join failed.\n");
return -;
} printf("In thread main");
printf("address of global %d: %x\n", global, &global);
printf("address of main local %d: %x\n", local, &local); return ;
} void *thread_foo_func(void *arg){
int foo_local = ;
global ++;
*(int *)arg = ;
printf("In thread foo_func");
printf("address of global %d: %x\n", global, &global);
printf("address of main local %d: %x\n", *(int *)arg, arg);
printf("address of foo local: %x\n", &foo_local);
printf("\n");
} void *thread_bar_func(void *arg){
int bar_local = ;
global ++;
*(int *)arg = ;
printf("In thread bar_func");
printf("address of global %d: %x\n", global, &global);
printf("address of main local %d: %x\n", *(int *)arg, arg);
printf("address of bar local: %x\n", &bar_local);
printf("\n");
}

打印输出结果

In thread foo_funcaddress of global : 8049a48
address of main local : bfc567b8
address of foo local: b7f553c4 In thread bar_funcaddress of global : 8049a48
address of main local : bfc567b8
address of bar local: b75543c4 In thread mainaddress of global : 8049a48
address of main local : bfc567b8

可见:

1 global 在线程中可见,而且共享,

2 main中的loca通过指针传给了进程,进程可以改变

3 两个线程中的私有变量是不同的,是线程私有的。

这和fork出的进程就完全不同

 int global = ;

 int main(int argc,char** argv)
{
int var=;
int pid=fork();
if(pid==-){
printf("error!");
}
else if(pid==){
global++;
var++;
printf("This is the child process!\n");
}
else{
printf("This is the parent process! child processid=%d\n",pid);
}
printf("%d, %d, %d \n", getpid(), global, var); return ;
}

打印结果:

This is the child process!
, ,
This is the parent process! child processid=
, ,

可见,调用fork,会有两次返回,一次是父进程、一次是子进程,因为子进程是父进程的副本,所以它拥有父进程数据空间、栈和堆的副本,它们并没有共享这些存储空间,它们只共享正文段。

pthread 学习系列 case1-- 共享进程数据 VS 进程的更多相关文章

  1. pthread 学习系列 case2-- 使用互斥锁

    ref http://www.ibm.com/developerworks/cn/linux/thread/posix_thread1/index.html #include <pthread. ...

  2. Kettle学习系列之数据仓库、数据整合、ETL、ELT和EII之间的区别?

    不多说,直接上干货! 在数据仓库领域里,的一个重要概念就是数据整合(data intergration).数据整合它就是把不同数据库中的数据整合到一起,对外提供统一的数据视图. 数据整合最典型的案例就 ...

  3. pthread 学习系列 case2-- pthread_mutex_t

    许多互斥对象 如果放置了过多的互斥对象,代码就没有什么并发性可言,运行起来也比单线程解决方案慢.如果放置了过少的互斥对象,代码将出现奇怪和令人尴尬的错误.幸运的是,有一个中间立场.首先,互斥对象是用于 ...

  4. 【深度学习系列】PaddlePaddle之数据预处理

    上篇文章讲了卷积神经网络的基本知识,本来这篇文章准备继续深入讲CNN的相关知识和手写CNN,但是有很多同学跟我发邮件或私信问我关于PaddlePaddle如何读取数据.做数据预处理相关的内容.网上看的 ...

  5. Caffe学习系列(14):初识数据可视化

    //   首先将caffe的根目录作为当前目录,然后加载caffe程序自带的小猫图片,并显示. 图片大小为360x480,三通道 In [1]: import numpy as np import m ...

  6. Echarts 学习系列(3)-Echarts动态数据交互

    写在前面 上一小节,我们总结了折线(面积)图.柱状(条形)图.饼(圆环)图类型的图表. 但是,都是静态的.接下来的,这一小节,总结的是Echarts 动态数据的交换. 前置条件 开发环境:win10 ...

  7. 【深度学习系列】关于PaddlePaddle的一些避“坑”技巧

    最近除了工作以外,业余在参加Paddle的AI比赛,在用Paddle训练的过程中遇到了一些问题,并找到了解决方法,跟大家分享一下: PaddlePaddle的Anaconda的兼容问题 之前我是在服务 ...

  8. 【深度学习系列】PaddlePaddle垃圾邮件处理实战(二)

    PaddlePaddle垃圾邮件处理实战(二) 前文回顾   在上篇文章中我们讲了如何用支持向量机对垃圾邮件进行分类,auc为73.3%,本篇讲继续讲如何用PaddlePaddle实现邮件分类,将深度 ...

  9. Caffe 学习系列

    学习列表: Google protocol buffer在windows下的编译 caffe windows 学习第一步:编译和安装(vs2012+win 64) caffe windows学习:第一 ...

随机推荐

  1. Android启停调试

    环境配置 java jdk android sdk eclipse + adt 参考资料: http://tools.android-studio.org/#userconsent# android ...

  2. Jsoup开发简单网站客户端之读取本地html文件

    用jsoup解析网页,相比于那些返回api数据来说 肯定耗流量,加载慢,所以程序assts中预先放了一个最新的html文件,第一次进来不走网络,直接从本地取,以后会加上wifi离线功能. 首先离线网站 ...

  3. [CareerCup] 2.6 Linked List Cycle 单链表中的环

    2.6 Given a circular linked list, implement an algorithm which returns the node at the beginning of ...

  4. [CareerCup] 8.7 Chat Server 聊天服务器

    8.7 Explain how you would design a chat server. In particular, provide details about the various bac ...

  5. Linux及安全——程序破解

    Linux及安全——程序破解 由于我的Ubuntu的vi有故障,所以用kaili做. 运行原程序 1.反汇编代码,查看 objdump -d login 2.修改代码 vi login 转换为16进制 ...

  6. iOS:界面适配(三)--iPhone不同机型或设备不同尺寸适配(屏幕适配)和系统适配

    对于不同苹果设备,各个参数查看<iOS:机型参数.sdk.xcode各版本>.        机型变化 坐标:表示屏幕物理尺寸大小,坐标变大了,表示机器屏幕尺寸变大了: 像素:表示屏幕图片 ...

  7. 20145222黄亚奇《Java程序设计》第1周学习总结

    教材学习内容总结 BJVM是Java程序唯一认识的操作系统,其可执行文件为.class文档 Java的三大平台为Java SE,Java EE,Java ME. Java SE的四个部分为:JVM,J ...

  8. BASE64Decoder 编码(sun.jar)

    Base64 是网络上最常见的用于传输8Bit 字节代码的编码方式之一,大家可以查看RFC2045 -RFC2049 ,上面有MIME 的详细规范.  Base64 要求把每三个8Bit 的字节转换为

  9. node不懂的方法的使用

    1. 学习的时候注意,过滤器,上传文件sftp,读取excel,还有cookie的操作,sql的操作.node的框架express koa hapi 还有引擎ejs,hbs,jade,日志管理等,并发 ...

  10. Pell方程及其一般形式

    一.Pell方程 形如x^2-dy^2=1的不定方程叫做Pell方程,其中d为正整数,则易得当d是完全平方数的时候这方程无正整数解,所以下面讨论d不是完全平方数的情况. 设Pell方程的最小正整数解为 ...