Shared resources of parent and child

. 1 #include <stdio.h>
 2 #include <stdlib.h>
 . 3 #include <unistd.h>
 . 4  int  Global = . 1 ; / * initialize the global variables, data segment is present * / 
. 5  
. 6  int main ( void )
 . 7  {
 . 8      pid_t PID; / * store process ID * / 
. 9      int stack = . 1 ; / * local variables, the presence of the stack * / 
10      int * heap; / * pointer to a heap variable pointer * / 
. 11      heap = ( int *) the malloc (the sizeof ( int ));
 12 is      * = heap . 3 ; / * set the value of the stack is. 3 * / 
13 is      PID = the fork (); / * create a new thread * / 
14      IF (PID < 0 ) {
 15          perror ( " ! Fail to Fock \ n- " );
 16          Exit (- . 1 );
 . 17      }
 18 is      the else  IF (PID == 0 ) {
 . 19          / * child process, changing the value of variable * / 
20 is          Global ++ ;
 21 is          Stack ++;
 22 is          (heap *) ++ ;
 23 is          / * print out the value of the variable * / 
24          the printf ( " the In Sub-Process, Global:% D, Stack:% D, heap:% D \ n- " , Global , Stack, * heap);
 25          Exit ( 0 );
 26 is      }
 27      the else {
 28          / * parent * / 
29          sLEEP ( 2 ); / * sleep 2 seconds, to ensure that the child process is finished, the parent process executed * / 
30          the printf ( " the In the Parent, Global:% D, Stack:% D, heap:% D \ n- " , Global,stack,*heap);
31     }
32     return 0;
33 }

 

Guess you like

Origin www.cnblogs.com/--lr/p/11260932.html