/* Copyright (C) 2009 by Mattia Monga <mattia.monga@unimi.it> */
/* $Id$ */

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <sched.h>
#include <assert.h>

int run(void* s)
{
        int* shared = (int*)s; /* alias per comodita` */
        while (shared[0] < 10) {
                sleep(1);
                printf("Processo figlio (%d). s = %d\n", 
                       getpid(), shared[0]);
                if (!(shared[0] < 10)){
                        printf("Corsa critica!!!!\n");
                        abort();
                }
                shared[0] += 1;
        }
        return 0;
}


int main(void){
 
        int shared[2] = {0 , 0};
        if (clone(run, malloc(4096)+4096,  SIGCHLD, shared) < 0){
                perror("Errore nella creazione");
                exit(1);
        } 

        if (clone(run, malloc(4096)+4096,  SIGCHLD, shared) < 0){
                perror("Errore nella creazione");
                exit(1);
        } 

        while(1) {
                sleep(1);
                printf("Processo padre. s = %d\n", shared[0]);
        }
        return 0;
}

    
        
  



/* Local Variables: */
/* compile-command: "make -k " */
/* End: */
