Thread: System Calls - write() & read () int

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    2

    System Calls - write() & read () int

    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

  2. #2
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    I think you're writing to the file just fine. You need to reset the file position indicator to the beginning of the file using lseek() however, if you want to read() after a write() without reopening the file.

  3. #3
    Registered User
    Join Date
    Dec 2011
    Posts
    2
    Quote Originally Posted by Tclausex View Post
    I think you're writing to the file just fine. You need to reset the file position indicator to the beginning of the file using lseek() however, if you want to read() after a write() without reopening the file.
    thank you!!
    i solved my problem

    please mod, close this thread!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. System calls (open, close, write)
    By ollie88r in forum C Programming
    Replies: 7
    Last Post: 11-09-2009, 06:16 PM
  2. write and read system calls
    By nacho4d in forum C Programming
    Replies: 4
    Last Post: 01-28-2008, 10:59 AM
  3. about system calls
    By fnoyan in forum C Programming
    Replies: 1
    Last Post: 02-27-2006, 06:25 AM
  4. Need some help,,system calls
    By kodiak76 in forum Linux Programming
    Replies: 6
    Last Post: 01-25-2005, 12:25 PM
  5. System Calls
    By Jperensky in forum C Programming
    Replies: 6
    Last Post: 03-12-2002, 02:41 PM