pthread 学习系列 case1-- 共享进程数据 VS 进程

时间:2022-09-17 18:32:04
 #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&lowbar;mutex&lowbar;t

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

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

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

  5. Caffe学习系列&lpar;14&rpar;:初识数据可视化

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

  6. Echarts 学习系列&lpar;3&rpar;-Echarts动态数据交互

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

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

    最近除了工作以外,业余在参加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. ajax与后台交互传输数据的工具类

    public class Result<T> implements Serializable { private static final long serialVersionUID = ...

  2. iOS - UIView

    前言 NS_CLASS_AVAILABLE_IOS(2_0) @interface UIView : UIResponder <NSCoding, UIAppearance, UIAppeara ...

  3. 转:使用Android API最佳实践

    原文来自于:http://blog.jobbole.com/65170/ 写在前面 现在,Android应用程序中集成第三方API已十分流行.应用程序都有自己的网络操作和缓存处理机制,但是大部分比较脆 ...

  4. android开发用无线网络进行Android开发中的调试

    1.手机具有root权限 2.安装adbWireless1.5.4.apk (下面有下载地址) 3.敲入命令:adb connect 192.168.1.127  后面是手机的IP地址 打开eclip ...

  5. headfirst设计模式(7)—命令模式

    一.前言 什么是命令模式? 在软件系统中,“行为请求者”与“行为实现者”通常呈现一种“紧耦合”.但在某些场合,比如要对行为进行“记录.撤销/重做.事务”等处理,这种无法抵御变化的紧耦合是不合适的.在这 ...

  6. swagger结合dubbo的rest服务测试

    swagger结合dubbo的rest服务测试 背景介绍 我们应用的dubbo服务导出,可能没有直接的触发点去发起调用测试,除非自己手写controller和test类,缺乏一个动态工具,类似流行的s ...

  7. Python爬虫——Request模块

    # 使用 Requests 发送网络请求# 1.导入 Requests 模块import requests# 2.尝试获取某个网页 # HTTP 请求类型r = requests.get('https ...

  8. mysql主从复制报错(一主一从):从库报错Last&lowbar;SQL&lowbar;Errno&colon; 1008

    配置完主从复制后(一主一从),在从库删了一个测试库,结果在从库上使用show slave status\G查看主从同步状态报如下错误:Last_Errno: 1008,经过排查得知:主从环境删除要先在 ...

  9. Vue之常用语法

    变量的定义: var定义的变量:只有全局作用域和函数作用域.有变量提升,先打印后定义变量不会报错,打印结果为undefined let定义的变量:没有变量提升             ——>有局 ...

  10. Search in Rotated Sorted Array II leetcode java

    题目: Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would ...