Thread: I need help please...my program is crashing

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

    I need help please...my program is crashing

    Hi I'm a student and my professor want us to make a C program in LINUX in wich the parent process generates two child processes. The parent will read data from stdin and will send it to his children.

    The 1st child will show on screen the text read from his father,to standard output.

    The 2nd child will show the read data in a file. When the parent has as input an "exit", he will send termination signals to his children and will exit.

    So here I let you the code I did and the errors I get when I have to compile. The teacher want us to program it like the way is shown below , with that structure.

    The program is in spanish, so the variable names too,I hope that won't be a problem and you can help me as soon as posible because I'm desperate...THANK YOU!!!!

    Code:
     
    #include <stdio.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <signal.h>
    
    
    int pidHijo1; // global var for 1st child
    int descrTub1[2]; //global var for 1st pipe
    
    int pidHijo2; //global var for 2nd child
    int descrTub2[2]; //global var for 2nd pipe
    
    
    char bufferLectura[256]; //buffer
    
    
    
    void ProcesoHijo1(); //1st child process
    void ProcesoHijo2(); //2nd child process
    void ProcesoPadre(); //parent process
    
    
    int main(int argc, char *argv[]) {
    
    
    pipe(descrTub1); 
    
    pidHijo1 = fork();
    
    if (pidHijo1 == 0) {
    ProcesoHijo1();
    return 0;
    }
    
    
    pidHijo2 = fork();
    
    if (pidHijo2 == 0)
    ProcesoHijo2();
    else
    ProcesoPadre();
    
    
    }
    
    void ProcesoHijo1() { 
    
    close(0); 
    dup(descrTub1[0]); 
    while (1) { 
    fgets(bufferLectura, 255, stdin); 
    printf("%s\n", bufferLectura);
    }
    }
    
    void ProcesoHijo2() { 
    int descrFichero;
    
    close(0); 
    dup(descrTub1[0]); 
    
    descrFichero = open("salida.txt", O_RDWR | O_TRUNC, 0600);
    
    while(1) { 
    fgets(bufferLectura,255,descrTub1[0]); 
    write(descrFichero, bufferLectura, strlen(bufferLectura));
    }
    
    
    }
    
    void ProcesoPadre() { 
    
    
    close(2); 
    dup(descrTub1[1]); 
    
    printf("[Proceso padre] Introduce un texto, o exit para salir> ");
    fgets(bufferLectura, 255);
    
    while(strcmp(bufferLectura, "exit")) { 
    
    fprintf(stderr, bufferLectura); 
    write(descrTub2[0], bufferLectura, strlen(bufferLectura)); 
    
    printf("<[Proceso padre] Introduce un texto, o exit para salir> "); //[Parent Process] Insert text or exit
    fgets(bufferLectura, 255);
    }
    
    
    kill(pidHijo1, SIGTERM);
    kill(pidHijo2, SIGTERM);
    
    
    }
    ERRORS:

    ejercicio.c: In function ‘ProcesoHijo2’:
    ejercicio.c:71: warning: passing argument 3 of ‘fgets’ makes pointer from integer without a cast
    /usr/include/stdio.h:624: note: expected ‘struct FILE * __restrict__’ but argument is of type ‘int’
    ejercicio.c:72: warning: incompatible implicit declaration of built-in function ‘strlen’
    ejercicio.c: In function ‘ProcesoPadre’:
    ejercicio.c:85: error: too few arguments to function ‘fgets’
    ejercicio.c:89: warning: format not a string literal and no format arguments
    ejercicio.c:90: warning: incompatible implicit declaration of built-in function ‘strlen’
    ejercicio.c:93: error: too few arguments to function ‘fgets’

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    This is correct - note that it has the file pointer as the third parameter for fgets(). Your other fgets() calls, do not have that.
    Code:
    fgets(bufferLectura, 255, stdin);
    I can't help you with a lot of this, but this is a few of your errors.

    ejercicio.c:71: warning: passing argument 3 of ‘fgets’ makes pointer from integer without a cast
    /usr/include/stdio.h:624: note: expected ‘struct FILE * __restrict__’ but argument is of type ‘int’
    ejercicio.c:85: error: too few arguments to function ‘fgets’
    ejercicio.c:93: error: too few arguments to function ‘fgets’


    Looks like you have not yet included string.h, if you want to use strlen(), you'll want it.
    Last edited by Adak; 11-19-2011 at 02:35 PM.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    2
    Quote Originally Posted by Adak View Post
    This is correct - note that it has the file pointer as the third parameter for fgets(). Your other fgets() calls, do not have that.
    Code:
    fgets(bufferLectura, 255, stdin);
    I can't help you with a lot of this, but this is a few of your errors.

    ejercicio.c:71: warning: passing argument 3 of ‘fgets’ makes pointer from integer without a cast
    /usr/include/stdio.h:624: note: expected ‘struct FILE * __restrict__’ but argument is of type ‘int’
    ejercicio.c:85: error: too few arguments to function ‘fgets’
    ejercicio.c:93: error: too few arguments to function ‘fgets’


    Looks like you have not yet included string.h, if you want to use strlen(), you'll want it.
    Wow GOD BLESS YOU!!! my errors have decreased because of that (string.h). Yes,I know those others are my errors, but the thing is that the professor told us to program this without knowing C, and without teaching it...so I'm getting a "little bit" crazy because of that... So,I have trouble with fgets and I don't know what to do, but you helped me anyway MANY THANKS.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Is it just me or are we experiencing an epidemic of people who don't know how to indent code?

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Looks like their first time swimming in the pool, instead of practicing their swimming on the deck.

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by CommonTater View Post
    Is it just me or are we experiencing an epidemic of people who don't know how to indent code?
    You noticed too huh?! On the plus side though, he's using fgets, or at least trying to.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Any help on why my program is crashing?
    By brookemay in forum C Programming
    Replies: 2
    Last Post: 11-19-2011, 12:54 PM
  2. Program keeps crashing
    By o.fithcheallaig in forum C Programming
    Replies: 25
    Last Post: 10-20-2011, 03:57 AM
  3. crashing program, help
    By xniinja in forum Windows Programming
    Replies: 1
    Last Post: 07-07-2010, 03:57 PM
  4. Program crashing, Need help!
    By Obsidian_179 in forum C Programming
    Replies: 3
    Last Post: 06-19-2008, 05:26 PM
  5. help plz crashing program
    By mill1000 in forum C++ Programming
    Replies: 3
    Last Post: 08-23-2002, 09:15 AM