Hello there, i'm new here, and need a real support to complete this exercize.

So basically i need to write an integer to a file (using write System Call) and the with a child process read that int (using read System Call) and create other n child process (where n is the int i've read from the file)

but i'm not able to write that int to the file using this system call, nor even to read any kind of number from the file using read.. so i'm stuck


if anyone can help, i'l be very gratefull..
so this is the code:
Code:
/* Scrivere un programma C in cui un processo crea un file nipoti.txt e viscrive un intero n. A questo punto il processo attiva un processo figlio
che legge dal file n e crea a sua volta n figli e ne attende la
terminazione (di tutti). */


#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>


int main() {
   /* Variabili da utilizzare */
   int n, pid, fd, status;
   int i, buf;


   n = 5;


   /* il processo crea il file. In questo modo il file sarà disponibile
   per tutti i figli, a patto che non vengano eseguiti Context-Switch */
   fd = open("./nipoti.txt", O_CREAT | O_RDWR | O_TRUNC, 0777);
   
   /* Viene quindi scritto il numero di processi da creare.
   N.B. la traccia chiede solamente di scrivere un numero, quindi in questo
   caso mi sono preso la libertà di definirlo direttamente nel codice */
   buf = write(fd, &n, sizeof(n));
   
   /* poniamo n = 0 solo per assicurarci che il suo valore iniziale 5
   non venga successivamente utlizzato. il suo valore deve essere
   infatti letto dal file nipoti.txt */
   n = 0;
   
   /* Quindi si crea il Figlio */
   pid = fork();
   
   /* Si controlla che tutto sia andato secondo i piani */
   if(pid < 0)
      printf("Errore durante la creazione del processo figlio\n");
   else if(pid == 0) { /* eseguito dal figlio */
      printf("Figlio(%d) con padre(%d) creato!\n",getpid(),getppid());
      
      read(fd, &n, buf);
      printf("DEBUG: n vale %d \n", n);
      printf("DEBUG: buf vale %d \n", buf);


      if(n <= 0) {
         printf("Bisogna specificare un numero > 0\n");
         return 1;
      }


      for(i = 0; i < n; i++) {
         /* Ora creiamo i nipoti */
         pid = fork();
         
         if(pid < 0)
            printf("Errore durante la creazione del processo nipote\n");
         else if(pid == 0) {
            printf("Nipote(%d) con padre(%d) creato!\n",getpid(),getppid());
            i = n;
         }
      }
      
      if(pid > 0) { /* viene eseguito dal figlio */
         for(i = 0; i < n; i++) {
         /* Ora creiamo i nipoti */
         pid = wait(&status);
         
         if(pid > 0)
            printf("Nipote(%d) terminato!\n", pid);
         }
      }  
   } else { /* eseguito dal padre */
      pid = waitpid(pid, &status, 0);
      
      if(pid > 0)
         printf("Figlio(%d) terminato!\n", pid);
   }


   close(fd);
   return 0;
}
all the comments are in italian.. so sorry if u dont' understand them ^^'... also sorry for my bad english

hope to get some good answer to this,
easly