Announcement

Collapse
No announcement yet.

[os] sincronizzazione di 2 processi ( fork) usando shared memory e Peterson

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • [os] sincronizzazione di 2 processi ( fork) usando shared memory e Peterson

    PHP Code:
    #include <sys/ipc.h>
    #include <sys/shm.h>
    #include <st***.h>
    #include <sys/types.h>
    #include <stdlib.h>
    #include <errno.h>

    /***
     *               _            _                          
     *              (_)          (_)                          
     *    __  ____ _ _  _____   ___  ___   ___ ___  _ __ ___  
     *    \ \/ / _` | |/ _ \ \ / / |/ _ \ / __/ _ \| '_ ` _ \
     *     >  < (_| | | (_) \ V /| | (_) | (_| (_) | | | | | |
     *    /_/\_\__, |_|\___/ \_/ |_|\___(_)___\___/|_| |_| |_|
     *          __/ |                                        
     *         |___/
     *
     *
     *          Process syncronization between parent and child - fork()
     *                  Shared Memory and Critical Sections + Peterson Solution ( 2 processes)
     *        
     */

    #define SHMSZ 100
    #define KEY 9999

    int main void ) {

        
    int shmidstatusshmdetshmcontrol,err;
        
    void shmatt;
        
    int new_area;
        
        
    /////////////creazione memoria condivisa
        
    shmid shmget KEYSHMSZIPC_CREAT IPC_EXCL 660);
            if (
    shmid == -1) { fprintf (stderr"Impossibile creare segmento condiviso\n");
                
    errerrno;
                
    fprintf (stderr"Errore %d\n",err);
                exit (-
    1);}
        
    //////////// attacco memoria condivisa a spazio processo
        
    shmatt shmat shmid, (void *),0);
            if (
    shmatt == (void *) -1) { fprintf (stderr"Impossibile modificare spazio indirizzi processo\n");
                
    errerrno;
                
    fprintf (stderr"Errore %d\n",err);
                exit (-
    2);}
                
        
    new_area = (int *) shmatt;
        
    int *flag/// signal that process wants to go into critical section
            
    flag new_area;
        
    int *turn;
            
    turn new_area 2;
        
        
    ///////////////////////////////////// begin fork /////////////////////////////////////////////////////////
        
        
    int pid;
        
    pid fork();
        
        
        
        if ( 
    pid == 0) {
            
                                    
    //precritical section
                                        
    *(flag 1)=1// processo figlio vuole andare in sezione critica
                                        
    *turn=0// turno del processo padre
                                        
                                        
    for (;(*turn) == && *(flag)==1;) {sleep(1);} // attendo che il processo padre esca dalla sezione critica
                                    
                ////////////////////////////// begin critical section child process
                
                                    //critical section
                                    
                                    
                
                ///////////////////////////// end critical section child process
                
                                    // post critical section
                                        
    *(flag 1)=0;
                                        
                                    
                
    shmdet shmdt (shmatt);
                    if (
    shmatt == (void *)-1) { fprintf (stderr"Impossibile detachare memoria condivisa\n");
                        
    errerrno;
                        
    fprintf (stderr"Errore %d\n",err);
                        exit (-
    3);}
                
        } else {
            
                                    
    //precritical section
                                    
    *flag=1// processo padre vuole andare in sezione critica
                                    
    *turn=1// turno del processo figlio
                                    
                                    
    for (;(*turn) == && *(flag 1)==1;) {sleep(1);} // attendo che il processo figlio esca dalla sezione critica
                
                ////////////////////////////// begin critical section parent process
                
                                    //critical section
                
                ///////////////////////////// end critical section parent process
                
                                    // post critical section
                                    
    *(flag)=0;
                                    
                
                
    waitpid(-1,&status,0);
                
                
    shmdet shmdt (shmatt);
                    if (
    shmatt == (void *)-1) { fprintf (stderr"Impossibile detachare memoria condivisa\n");
                    
    errerrno;
                    
    fprintf (stderr"Errore %d\n",err);
                    exit (-
    4);}
                
    shmcontrol shmctl (shmid,IPC_RMID,NULL );
                    if (
    shmcontrol == -1) { fprintf (stderr"Impossibile settare memoria condivisa per eliminazione\n");
                    
    errerrno;
                    
    fprintf (stderr"Errore %d\n",err);
                    exit (-
    5);}
        }

        return 
    0;

    | VFX Artist, C++ Programmer, HW Overclocker | Web: xgiovio.com Email: xgiovio@gmail.com Twitter: @xgiovio
Working...
X

Google Profile


My name is Giovanni Di Grezia, but people call me xgiovio.

Here is my homepage:.

I'm a VFX Artist and Software Developer.

Giovanni Di Grezia