home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / mach / doc / unpublished / examples / cowtest.c.Z / cowtest.c
Encoding:
C/C++ Source or Header  |  1989-12-27  |  3.6 KB  |  116 lines

  1. /* 
  2.  * This program demonstrates the use of vm_inherit and copy on write
  3.  * memory. A child and parent process will share memory, polling this 
  4.  * memory to see whos turn it is to proceed.  First some memory is allocated,
  5.  * and vm_inherit is called on this memory, the variable 'lock'.  Next more 
  6.  * memory is allocated for the copy on write test. A fork is executed, and
  7.  * The parent then stores new data in the copy on write memory
  8.  * previously allocated, and sets the shared variable signaling to the
  9.  * child that he is now waiting.  The child, polling the shared variable,
  10.  * realizes it is his turn.  The child prints the value of the variable
  11.  * lock and a value of the copy on write memory as the child sees it.
  12.  * You will notice that the value of the lock is what the parent
  13.  * set it to be, but the value of the copy on write memory is the original
  14.  * value and not what the parent changed it to be.
  15.  * The parent then awakes and prints out the two values once more.
  16.  * The program then ends with the parent signaling the child via the
  17.  * shared variable lock.
  18.  ********************************************************/
  19. #include <mach.h>
  20. #include <stdio.h>
  21.  
  22. #define NO_ONE_WAIT 0
  23. #define PARENT_WAIT 1
  24. #define CHILD_WAIT 2
  25. #define COPY_ON_WRITE 0
  26. #define PARENT_CHANGED 1
  27. #define CHILD_CHANGED 2
  28.  
  29. #define MAXDATA 100
  30.  
  31. main(argc, argv)
  32.     int    argc;
  33.     char    *argv[];
  34. {
  35.     int        pid;
  36.     int        *mem;
  37.     int        *lock;
  38.     kern_return_t    ret;
  39.  
  40.     if (argc > 1) {
  41.         printf("cowtest takes no switches.  ");
  42.         printf("This program is an example of copy on write \n");
  43.         printf("memory and of the use of vm_inherit.\n");
  44.         exit();
  45.     }
  46.     if ((ret = vm_allocate(task_self(), &lock, sizeof(int),
  47.         TRUE)) != KERN_SUCCESS) {
  48.         mach_error("vm_allocate returned value of ", ret);
  49.         printf("Exiting with error.\n");
  50.         exit();
  51.     }
  52.     if ((ret = vm_inherit(task_self(), lock, sizeof(int),
  53.         VM_INHERIT_SHARE)) != KERN_SUCCESS) {
  54.         mach_error("vm_inherit returned value of ", ret);
  55.         printf("Exiting with error.\n");
  56.         exit();
  57.     }
  58.     *lock = NO_ONE_WAIT;
  59.     if ((ret = vm_allocate(task_self(), &mem, sizeof(int) * MAXDATA,
  60.         TRUE)) != KERN_SUCCESS) {
  61.         mach_error("vm_allocate returned value of ", ret);
  62.         printf("Exiting with error.\n");
  63.         exit();
  64.     }
  65.     mem[0] = COPY_ON_WRITE;
  66.  
  67.     printf("value of lock before fork: %d\n", *lock);
  68.     pid = fork();
  69.     if (pid) {
  70.         printf("PARENT: copied memory =  %d\n",
  71.             mem[0]);
  72.         printf("PARENT: changing to %d\n", PARENT_CHANGED);
  73.         mem[0] = PARENT_CHANGED;
  74.         printf("\n");
  75.         printf("PARENT: lock = %d\n", *lock);
  76.         printf("PARENT: changing lock to %d\n", PARENT_WAIT);
  77.         printf("\n");
  78.         *lock = PARENT_WAIT;
  79.         while (*lock == PARENT_WAIT);
  80.            /* wait for child to change the value */
  81.            /* beware of optimizing compilers */
  82.         printf("PARENT: copied memory = %d\n", 
  83.             mem[0]);
  84.         printf("PARENT: lock = %d\n", *lock);
  85.         printf("PARENT: Finished.\n");
  86.         *lock = PARENT_WAIT;
  87.         exit();
  88.     }
  89.     while (*lock != PARENT_WAIT);
  90.         /* wait for parent to change lock */ 
  91.         /* beware of optimizing compilers */
  92.     printf("CHILD: copied memory = %d\n", mem[0]);
  93.     printf("CHILD: changing to %d\n", CHILD_CHANGED);
  94.     mem[0] = CHILD_CHANGED;
  95.     printf("\n");
  96.     printf("CHILD: lock = %d\n", *lock);
  97.     printf("CHILD: changing lock to %d\n", CHILD_WAIT);
  98.     printf("\n");
  99.     *lock = CHILD_WAIT;
  100.     while (*lock == CHILD_WAIT);
  101.         /* wait for parent to change lock */ 
  102.     if ((ret = vm_deallocate(task_self(), lock, 
  103.         sizeof(int), TRUE)) != KERN_SUCCESS) {
  104.         mach_error("vm_deallocate returned value of ", ret);
  105.         printf("Exiting.\n");
  106.         exit();
  107.     }
  108.     if ((ret = vm_deallocate(task_self(), mem, 
  109.         MAXDATA * sizeof(char), TRUE)) != KERN_SUCCESS) {
  110.         mach_error("vm_deallocate returned value of ", ret);
  111.         printf("Exiting.\n");
  112.         exit();
  113.     }
  114.     printf("CHILD: Finished.\n");
  115. }
  116.