Thread: write() with pipe()

  1. #1
    Registered User
    Join Date
    May 2003
    Posts
    15

    Unhappy write() with pipe()

    I'm trying to put a buffer to the pipe but the PARSER() can't find the fd[] array? Do I have a scope issue? Any help.. BTW my first post

    Code:
    #include <errno.h>
    #include <string.h>
    #include <stdlib.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <unistd.h>
    #include <fcntl.h>
    #include <stdio.h>
    #include <signal.h>
    #include <sys/wait.h>
    #include <time.h>
    
    #define MAX_LENGTH 80
    
    int main( int argc, char **argv )
    {
            pid_t c_pid;
            int fd[2];
    
            if ( argc != 2 ) {
                    fprintf(stderr, "Usage: %s time_unit\n", argv[0]);
                    exit(EXIT_FAILURE);
            }
    
            /* you have to write this routine */
            if ( STRING_TO_POS_INT(argv[1]) <= 0 ) {
                    fprintf(stderr, "Invalid Round Robin time quantum \n");
                    exit(EXIT_FAILURE);
            }
    
            if ( pipe(fd) == -1 ) { /*create a pipe*/
                    perror("Cannot create a pipe\n");
                    exit(EXIT_FAILURE);
            }
    
            if ( (c_pid = fork()) == -1 ) {
                    perror("Cannot spawn a child process\n");
                    exit(EXIT_FAILURE);
            }
            else if ( c_pid == 0 ) { /* child process */
                    char pipestring[10];
                    int flag = fcntl(fd[0], F_GETFL, 0);
                    close(fd[1]);
                    fcntl(fd[0], F_SETFL, flag|O_NONBLOCK);  /*child reads pipe asynchronously*/
                    sprintf(pipestring, "%d", fd[0]);
                    execl("./scheduler", "./scheduler", argv[1], pipestring, NULL);
                    exit(EXIT_SUCCESS);
            }
            else { /* parent process */
                    close(fd[0]);
                    PARSER();
                    exit(EXIT_SUCCESS);
            }
    }
    PARSER(void)
    {
        char inputbuf[MAX_LENGTH];
        for ( ; ; )
        {
            gets(inputbuf);
            if (strncmp(inputbuf, "run", 3) == 0)
            {
                                                               // pipe a run command
                 write(fd[1], inputbuf)
            }
            else if (strncmp(inputbuf, "pause", 5) == 0)
            {
                                                          // call the sleep function
            int num;
            sscanf(inputbuf,"%*s %d", num);
            sleep(num);
            }
            else
                    break;
                    
                
        }
        wait(NULL);
        exit(0);
    }
    Last edited by und3rdog; 05-02-2003 at 07:20 PM.
    I'm with
    <----------- newb!

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Of course it has scope issues. Your variables are local ones, and you aren't passing them to the function that you're trying to use them in.

    On a side note, stop using gets unless you like having people crash your program.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    May 2003
    Posts
    15
    So can I just move the fd[] outside of main? or will I have to pass it in order to write to the pipe? Or like could I say write(1,inputbuf);?

    Thanks,

    -Luke
    I'm with
    <----------- newb!

  4. #4
    Registered User
    Join Date
    May 2003
    Posts
    15
    thanks, what does dupe() do again? I thought it made an exact copy of the file descriptor? if I'm using the write(fd[1],inputbufer) i don't need it right?
    I'm with
    <----------- newb!

  5. #5
    Registered User
    Join Date
    May 2003
    Posts
    15
    Ohh that would make sense that way no matter who I stick in the exec it will have access to the pipe? for this case it doesn't matter, but thanks for the tip!

    -Luke
    I'm with
    <----------- newb!

  6. #6
    Registered User
    Join Date
    May 2003
    Posts
    15
    NEXT question. I'm in my scheduler program, how do I get the string I just stuffed into the pipe out without knowing the size?

    -Luke
    Last edited by und3rdog; 05-02-2003 at 08:18 PM.
    I'm with
    <----------- newb!

  7. #7
    Registered User
    Join Date
    May 2003
    Posts
    15
    Well yeah I had to change the write, but I did write(fd[1], inputbuffer, strlen(inputbuffer)+1) won't that give me the string plus the NULL written to the pipe? BTW where did you pick this stuff up so I don't keep pestering you!

    -Luke
    Last edited by und3rdog; 05-02-2003 at 08:26 PM.
    I'm with
    <----------- newb!

  8. #8
    Registered User
    Join Date
    May 2003
    Posts
    15
    so if I try and read(stream, buffer, buffersize) from a pipe where the string in the pipe is of size 4 and the buffersize is 128? What happens, won't the read fail?

    -Luke
    I'm with
    <----------- newb!

  9. #9
    Registered User
    Join Date
    May 2003
    Posts
    15
    Geez no need to get fiesty , I'm just looking at your code! There is no good reference on READ() that I've found.

    -Luke
    I'm with
    <----------- newb!

  10. #10
    Registered User
    Join Date
    May 2003
    Posts
    15
    Real problems or problems you consider real. A real problem is reading from a pipe! esp when there is Nilch on the man pages (BTW I'm not that new)! Help or don't, but don't be an ass!

    -Luke
    I'm with
    <----------- newb!

  11. #11
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  12. #12
    Registered User
    Join Date
    May 2003
    Posts
    15
    Wow, really I wish I could be as awesome a contributor as you!(I read your *nix stuff in the FAQ section) *sigh* Now come on, I'm not whining about anything other than your manners. Hammer was nice enough to provide me with some decent help (Love to know where you got that page from!) Using google is one thing but getting information from a "c community" about an interesting topic such as IPC is another. I'm trying to elimante a problem here NOT asking for a handout! read() isn't exactly the most documented thing (and YES I did look at the man page). There was plenty on it's usage, but what really happens when reading into a buffer array of specified length. Will it read up to it's limit, will it only read until the end of the array. Stuff like matters for some people! And I was only asking because some of you "more expirenced" programers might have already fought through the trials and tribulations of funky read syntax. While I thank you for your code example I'd much rather have some more information on using read with a pipe in an async fashion. Thanks

    -Luke

    ** Man, you unix guys hafta chill! **
    Last edited by und3rdog; 05-03-2003 at 11:48 PM.
    I'm with
    <----------- newb!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. read write lock in C#
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 04-16-2008, 08:49 AM
  2. Reroute where programs write to
    By willc0de4food in forum C Programming
    Replies: 7
    Last Post: 09-21-2005, 04:48 PM
  3. Having trouble with a named pipe
    By crazeinc in forum C Programming
    Replies: 2
    Last Post: 05-13-2005, 01:00 AM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. write in c
    By PutoAmo in forum C Programming
    Replies: 6
    Last Post: 04-03-2002, 07:53 PM