Announcement

Collapse
No announcement yet.

[os] Exam 1/2 - Create 2 subprocesses and wait them

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

  • [os] Exam 1/2 - Create 2 subprocesses and wait them

    PHP Code:
    /*
     *               _            _                          
     *              (_)          (_)                          
     *    __  ____ _ _  _____   ___  ___   ___ ___  _ __ ___  
     *    \ \/ / _` | |/ _ \ \ / / |/ _ \ / __/ _ \| '_ ` _ \
     *     >  < (_| | | (_) \ V /| | (_) | (_| (_) | | | | | |
     *    /_/\_\__, |_|\___/ \_/ |_|\___(_)___\___/|_| |_| |_|
     *          __/ |                                        
     *         |___/
     *
     *         Giovanni Di Grezia - 0512101xxx
     *
     *         ////// PRIMA PROVA INTERCORSO //////
     *
     *         No CHANGES since - Version 1.0
     *
     *         Notes: La traccia non definisce lo scheduling
     *         dei processi figli, quindi Ã¨ probabile che i
     *         numeri stampati non siano ordinati.
     *         Per evitare questo evento, basterebbe aggiungere
     *         un sleep al 2 processo figlio o attendere la
     *         terminazione del primo figlio prima di efettuare
     *         la seconda fork.
     *
     *        
     */

    #include <unistd.h>
    #include <fcntl.h>
    #include <stdlib.h>
    #include <sys/stat.h>
    #include <sys/types.h>
    #include <st***.h>

    int main void ) {
        
        
    int a,b,status,child,i// definizione variabili
        
        
        
    a=fork(); //creazione primo figlio
        
    if ( a<0) {
            
            
    fprintf(stderr,"Fork fallita\n");
            exit(-
    1);
            
        }else{
           if (
    a==0){
            
                
    fprintf(stdout,"Sono il primo figlio, il mio pid Ã¨ %d\n",getpid()); // pid processo corrente
                
                
    for ( i=1i<=i++){
                    
                    
    fprintf(stdout,"Numero %d\n",i); // stamap dei numeri richiesti
                    
                
    }
                
           }else{
                
                
    b=fork(); // secondo processo creato
                
                
    if ( b<){
                    
                   
    fprintf(stderr,"Fork fallita\n");
                   exit(-
    2);
                    
                    
                } else {
                    
                    if ( 
    b== 0){
                        
                        
    fprintf(stdout,"Sono il secondo figlio, il pid di mio padre Ã¨ %d\n",getppid()); // pid padre processo corrente
                
                        
    for ( i=101i<=150 i++){
                    
                            
    fprintf(stdout,"Numero %d\n",i); // stamap dei numeri richiesti
                    
                         
    }  
                        
                        
                    } else {
                        
                        
    // for per attendere il completamento di tutti i processi figli
                        
                        
    for ( child waitpid (-1, &status0); child child waitpid(-1,&status,0)){
                            
                            
                            
    fprintf(stdout,"Figlio con pid %d terminato con errore %d\n"childstatus); // stampa con relativo error code
                            
                        
    }
                
                    }
                    
                }
            
           }
              
        }
        
        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