pthread 学习系列 case1-- 共享进程数据 VS 进程
#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 进程的更多相关文章
- pthread 学习系列 case2-- 使用互斥锁
ref http://www.ibm.com/developerworks/cn/linux/thread/posix_thread1/index.html #include <pthread. ...
- Kettle学习系列之数据仓库、数据整合、ETL、ELT和EII之间的区别?
不多说,直接上干货! 在数据仓库领域里,的一个重要概念就是数据整合(data intergration).数据整合它就是把不同数据库中的数据整合到一起,对外提供统一的数据视图. 数据整合最典型的案例就 ...
- pthread 学习系列 case2-- pthread_mutex_t
许多互斥对象 如果放置了过多的互斥对象,代码就没有什么并发性可言,运行起来也比单线程解决方案慢.如果放置了过少的互斥对象,代码将出现奇怪和令人尴尬的错误.幸运的是,有一个中间立场.首先,互斥对象是用于 ...
- 【深度学习系列】PaddlePaddle之数据预处理
上篇文章讲了卷积神经网络的基本知识,本来这篇文章准备继续深入讲CNN的相关知识和手写CNN,但是有很多同学跟我发邮件或私信问我关于PaddlePaddle如何读取数据.做数据预处理相关的内容.网上看的 ...
- Caffe学习系列(14):初识数据可视化
// 首先将caffe的根目录作为当前目录,然后加载caffe程序自带的小猫图片,并显示. 图片大小为360x480,三通道 In [1]: import numpy as np import m ...
- Echarts 学习系列(3)-Echarts动态数据交互
写在前面 上一小节,我们总结了折线(面积)图.柱状(条形)图.饼(圆环)图类型的图表. 但是,都是静态的.接下来的,这一小节,总结的是Echarts 动态数据的交换. 前置条件 开发环境:win10 ...
- 【深度学习系列】关于PaddlePaddle的一些避“坑”技巧
最近除了工作以外,业余在参加Paddle的AI比赛,在用Paddle训练的过程中遇到了一些问题,并找到了解决方法,跟大家分享一下: PaddlePaddle的Anaconda的兼容问题 之前我是在服务 ...
- 【深度学习系列】PaddlePaddle垃圾邮件处理实战(二)
PaddlePaddle垃圾邮件处理实战(二) 前文回顾 在上篇文章中我们讲了如何用支持向量机对垃圾邮件进行分类,auc为73.3%,本篇讲继续讲如何用PaddlePaddle实现邮件分类,将深度 ...
- Caffe 学习系列
学习列表: Google protocol buffer在windows下的编译 caffe windows 学习第一步:编译和安装(vs2012+win 64) caffe windows学习:第一 ...
随机推荐
- 【OpenGL】查看显卡对OpenGL的支持程度
由于开发工作中要用到OpenGL的API进行渲染,公司配的电脑又是集成显卡,所以想知道显卡对OpenGL的支持程度. 下面介绍的方法就解决了这一点. 1.下载安装EVEREST Ultimate Ed ...
- [CareerCup] 5.8 Draw Horizonatal Line 画横线
5.8 A monochrome screen is stored as a single array of bytes, allowing eight consecutive pixels to b ...
- [CareerCup] 9.1 Climbing Staircase 爬楼梯
9.1 A child is running up a staircase with n steps, and can hop either 1 step, 2 steps, or 3 steps a ...
- MVC5 + EF6 + Bootstrap3 (13) 查看详情、编辑数据、删除数据
Slark.NET-博客园 http://www.cnblogs.com/slark/p/mvc5-ef6-bs3-get-started-rud.html 系列教程:MVC5 + EF6 + Boo ...
- 关于UITextView / String的尺寸
关于UITextView以及String的尺寸动态获取 iOS7开始,UITextView设置text后不会立即反映到contentSize属性,而是在父容器layoutSubviews时进行cont ...
- canvas学习笔记:小小滴公式,大大滴乐趣
声明:本文为原创文章,如需转载,请注明来源WAxes,谢谢! 最近想弄一个网页,把自己学HTML5过程中做的部分DEMO放上去做集合,但是,如果就仅仅做个网页把所有DEMO一个一个排列又觉得太难看了. ...
- 解决angular2页面刷新后报404错误
如果你的angular项目部署到一个tomcat容器里面,localhost:8080是JavaWeb的主页,localhost:8080/driver/login是你angular2项目的登陆地址. ...
- WCF 入门 (18)
前言 感冒了呀...but 不忌油炸,不忌辛辣,o(∩_∩)o . 第18集 WCF服务应该抛出fault 异常 Throwing fault exceptions from a WCF servic ...
- hdu2222 字典树
要注意二点 . 这组数据 1 6 she he he say shr her yasherhs出现重复的,也要算.所以这里答案为4: 这一组 1 6 she he he say shr her yas ...
- 【POJ 2096】Collecting Bugs 概率期望dp
题意 有s个系统,n种bug,小明每天找出一个bug,可能是任意一个系统的,可能是任意一种bug,即是某一系统的bug概率是1/s,是某一种bug概率是1/n. 求他找到s个系统的bug,n种bug, ...