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


     1 #include <stdio.h>
     2 #include <unistd.h>
     3 #include <stdlib.h>
     4 #include <pthread.h>
     5 
     6 
     7 void *thread_foo_func(void *);
     8 void *thread_bar_func(void *);
     9 
    10 
    11 int global = 4;
    12 
    13 
    14 int main(){
    15   int local = 8;
    16   int foo, bar;
    17   pthread_t fthread, bthread;
    18   foo = pthread_create(&fthread, NULL, thread_foo_func, (void *)&local);
    19   bar = pthread_create(&bthread, NULL, thread_bar_func, (void *)&local);
    20   if (foo != 0 || bar != 0){
    21     printf("thread creation failed.
    ");
    22     return -1;
    23   }
    24 
    25 
    26   foo = pthread_join(fthread, NULL);
    27   bar = pthread_join(bthread, NULL);
    28   if (foo != 0 || bar != 0){
    29     printf("thread join failed.
    ");
    30     return -2; 
    31   }
    32 
    33 
    34   printf("In thread main");
    35   printf("address of global %d: %x
    ", global, &global);
    36   printf("address of main local %d: %x
    ", local, &local);
    37 
    38   return 0;
    39 }
    40 
    41 
    42 void *thread_foo_func(void *arg){
    43   int foo_local = 16;
    44   global  ++;
    45   *(int *)arg = 10;
    46   printf("In thread foo_func");
    47   printf("address of global %d: %x
    ", global, &global);
    48   printf("address of main local %d: %x
    ", *(int *)arg, arg);
    49   printf("address of foo local: %x
    ", &foo_local);
    50   printf("
    ");
    51 }
    52 
    53 
    54 void *thread_bar_func(void *arg){
    55   int bar_local = 32;
    56   global  ++;
    57   *(int *)arg = 20;
    58   printf("In thread bar_func");
    59   printf("address of global %d: %x
    ", global, &global);
    60   printf("address of main local %d: %x
    ", *(int *)arg, arg);
    61   printf("address of bar local: %x
    ", &bar_local);
    62   printf("
    ");
    63 }

    打印输出结果

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

    可见:

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

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

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

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

     1 int global = 6;
     2 
     3 int main(int argc,char** argv)
     4 {
     5     int var=10;
     6     int pid=fork();
     7     if(pid==-1){
     8         printf("error!");
     9     }   
    10     else if(pid==0){
    11         global++;
    12         var++;
    13         printf("This is the child process!
    ");
    14     }   
    15     else{
    16         printf("This is the parent process! child processid=%d
    ",pid);
    17     }   
    18     printf("%d, %d, %d 
    ", getpid(), global, var); 
    19 
    20     return 1;                                                                                                                           
    21 }

    打印结果:

    This is the child process!
    10769, 7, 11 
    This is the parent process! child processid=10769
    10768, 6, 10 

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

  • 相关阅读:
    JVM字节码-字节码进阶
    JVM字节码-Class文件结构
    CT03 Contest#10 equation 大力打表+讨论
    2021CT03 Contest#9 降智场
    妙妙题 noi.ac 2323 Connecting
    洛谷 P4774 [NOI2018] 屠龙勇士
    [模板] 扩展中国剩余定理
    洛谷 P1082 [NOIP2012 提高组] 同余方程
    洛谷 P1516 青蛙的约会
    2021牛客暑期多校训练营3 Kuriyama Mirai and Exclusive Or
  • 原文地址:https://www.cnblogs.com/diegodu/p/3867589.html
Copyright © 2020-2023  润新知