/*
 * Test for multiple threads and shared variables
 *
 * This is a test case for two simoutaneously running threads, accessing the
 * same variable. Of couse this isn't allowed in a protected environment.
 * 
 * Author: Andreas Fugl - 25/03 2005
 * 
 * TODO: Introduce a shared variable that may be shared between the process.
 * Create a simple semaphore for dealing with this
 */

#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
#include <stddef.h>
#include <stdlib.h>
#include <sys/types.h>

signed int sharedvar;

main()
{
    pid_t pid;

    extern signed int sharedvar;
    sharedvar = 0;

    int *pointer;           /* pointer to a int */
    
    pointer = &sharedvar;   /* points to sharedvar */

    /* Fork the new process */
    if ((pid = fork()) == 0)
    {
        while (1)
        {
            printf("child value: %d\n",++*pointer);
            sleep(2);
        }
    }
    else if (pid < 0)
        printf("The fork failed\n");
    else
    {
        while (1)
        {
            printf("mother value: %d\n",--*pointer);
            sleep(1);
        }
    }
}


