I don't understand why I'm getting "Bad file descriptor " - Unix

This is a discussion on I don't understand why I'm getting "Bad file descriptor " - Unix ; Given the following: #include /* for openpty and forkpty */ #include #include #include #include int main(void) { int master, blah; char slave[200]; char line[BUFSIZ]; ssize_t n; blah = forkpty(&master, slave, NULL, NULL); if (dup2(master, STDIN_FILENO) == -1) { perror("dup2()"); return ...

+ Reply to Thread
Results 1 to 14 of 14

Thread: I don't understand why I'm getting "Bad file descriptor "

  1. I don't understand why I'm getting "Bad file descriptor "

    Given the following:

    #include /* for openpty and forkpty */
    #include
    #include
    #include
    #include

    int main(void)
    {
    int master, blah;
    char slave[200];
    char line[BUFSIZ];
    ssize_t n;

    blah = forkpty(&master, slave, NULL, NULL);

    if (dup2(master, STDIN_FILENO) == -1) {
    perror("dup2()");
    return -1;
    }

    printf("\n%d\n", blah);

    while ((n = read(master, line, BUFSIZ)) > 0) {
    write(1, line, n);
    }

    return 0;
    }
    [cdalten@localhost oakland]$ gcc -g -Wall forkit.c -o forkit -lutil
    [cdalten@localhost oakland]$ ./forkit

    764
    dup2(): Bad file descriptor
    [cdalten@localhost oakland]$


    Why am I getting "Bad file descriptor" in this case?

  2. Re: I don't understand why I'm getting "Bad file descriptor "

    K-mart Cashier writes:
    > Given the following:
    >
    > #include /* for openpty and forkpty */
    > #include
    > #include
    > #include
    > #include
    >
    > int main(void)
    > {
    > int master, blah;
    > char slave[200];
    > char line[BUFSIZ];
    > ssize_t n;
    >
    > blah = forkpty(&master, slave, NULL, NULL);
    >
    > if (dup2(master, STDIN_FILENO) == -1) {
    > perror("dup2()");
    > return -1;
    > }
    >
    > printf("\n%d\n", blah);
    >
    > while ((n = read(master, line, BUFSIZ)) > 0) {
    > write(1, line, n);
    > }
    >
    > return 0;
    > }
    > [cdalten@localhost oakland]$ gcc -g -Wall forkit.c -o forkit -lutil
    > [cdalten@localhost oakland]$ ./forkit
    >
    > 764
    > dup2(): Bad file descriptor
    > [cdalten@localhost oakland]$


    Everything after forkpty is executed by both processes and master is
    only valid in the parent process.

  3. Re: I don't understand why I'm getting "Bad file descriptor "

    On Aug 17, 10:24 am, Rainer Weikusat wrote:
    > K-mart Cashier writes:
    > > Given the following:

    >
    > > #include /* for openpty and forkpty */
    > > #include
    > > #include
    > > #include
    > > #include

    >
    > > int main(void)
    > > {
    > > int master, blah;
    > > char slave[200];
    > > char line[BUFSIZ];
    > > ssize_t n;

    >
    > > blah = forkpty(&master, slave, NULL, NULL);

    >
    > > if (dup2(master, STDIN_FILENO) == -1) {
    > > perror("dup2()");
    > > return -1;
    > > }

    >
    > > printf("\n%d\n", blah);

    >
    > > while ((n = read(master, line, BUFSIZ)) > 0) {
    > > write(1, line, n);
    > > }

    >
    > > return 0;
    > > }
    > > [cdalten@localhost oakland]$ gcc -g -Wall forkit.c -o forkit -lutil
    > > [cdalten@localhost oakland]$ ./forkit

    >
    > > 764
    > > dup2(): Bad file descriptor
    > > [cdalten@localhost oakland]$

    >
    > Everything after forkpty is executed by both processes and master is
    > only valid in the parent process.



    Let me think about that. I MIGHT have another question in a couple of
    hours.

  4. Re: I don't understand why I'm getting "Bad file descriptor "

    On Aug 17, 9:24 am, K-mart Cashier wrote:
    > Given the following:
    >
    > #include /* for openpty and forkpty */
    > #include
    > #include
    > #include
    > #include
    >
    > int main(void)
    > {
    > int master, blah;
    > char slave[200];
    > char line[BUFSIZ];
    > ssize_t n;
    >
    > blah = forkpty(&master, slave, NULL, NULL);
    >
    > if (dup2(master, STDIN_FILENO) == -1) {
    > perror("dup2()");
    > return -1;
    > }
    >
    > printf("\n%d\n", blah);
    >
    > while ((n = read(master, line, BUFSIZ)) > 0) {
    > write(1, line, n);
    > }
    >
    > return 0;}
    >
    > [cdalten@localhost oakland]$ gcc -g -Wall forkit.c -o forkit -lutil
    > [cdalten@localhost oakland]$ ./forkit
    >
    > 764
    > dup2(): Bad file descriptor
    > [cdalten@localhost oakland]$
    >
    > Why am I getting "Bad file descriptor" in this case?



    I have a totally unrelated question. On page 502 in the book
    "Advanced Programming in the Unix Environment" by Stevens and Rago,
    they have a the following function to help sychronize the parent and
    child.

    static int pfd1[2] , pfd2[2];

    void
    TELL_PARENT(pid_t pid)
    {
    if (write(pfd2[1], "c", 1) != 1)
    err_sys("write error");
    }

    What's the point of using pid if the function TELL_PARENT() doesn't
    actually use it?

    Chad

  5. Re: I don't understand why I'm getting "Bad file descriptor "

    K-mart Cashier wrote:
    > I have a totally unrelated question.


    If it's totally unrelated, why is it here?

  6. Re: I don't understand why I'm getting "Bad file descriptor "

    On Aug 17, 2:13 pm, Harold Shand wrote:
    > K-mart Cashier wrote:
    > > I have a totally unrelated question.

    >
    > If it's totally unrelated, why is it here?


    The question is unrelated to the topic, not to the forum.

  7. Re: I don't understand why I'm getting "Bad file descriptor "

    K-mart Cashier wrote:
    > On Aug 17, 2:13 pm, Harold Shand wrote:
    >> K-mart Cashier wrote:
    >>> I have a totally unrelated question.

    >> If it's totally unrelated, why is it here?

    >
    > The question is unrelated to the topic, not to the forum.


    And when I say "here" I am referring to the topic.

    HS

  8. Re: I don't understand why I'm getting "Bad file descriptor "

    On Aug 17, 3:03 pm, Harold Shand wrote:
    > K-mart Cashier wrote:
    > > On Aug 17, 2:13 pm, Harold Shand wrote:
    > >> K-mart Cashier wrote:
    > >>> I have a totally unrelated question.
    > >> If it's totally unrelated, why is it here?

    >
    > > The question is unrelated to the topic, not to the forum.

    >
    > And when I say "here" I am referring to the topic.
    >
    > HS


    Congratulations. You are now on my filter along with antonius twink
    from comp.lang.c. What can I say. If you can't answer the question,
    then don't respond.

  9. Re: I don't understand why I'm getting "Bad file descriptor "

    K-mart Cashier wrote:
    > On Aug 17, 3:03 pm, Harold Shand wrote:
    > > K-mart Cashier wrote:
    > > > On Aug 17, 2:13 pm, Harold Shand wrote:
    > > >> K-mart Cashier wrote:
    > > >>> I have a totally unrelated question.
    > > >> If it's totally unrelated, why is it here?

    > >
    > > > The question is unrelated to the topic, not to the forum.

    > >
    > > And when I say "here" I am referring to the topic.
    > >
    > > HS

    >
    > Congratulations. You are now on my filter along with antonius twink
    > from comp.lang.c. What can I say. If you can't answer the question,
    > then don't respond.


    Harold was merely pointing out the usenet convention
    that each thread should address one question
    because people are expert on different things.

    If I'm not expert in some aspect of UNIX programming,
    then I would want to ignore that entire thread of discussion.
    But I may participate in a different thread that talks
    about something that I'm familiar with.

    Harold is one of the expert here. You shut him out at your own
    disadvantage.

    By the way, nobody cares who you filter or don't filter.
    You're the one here asking people for (free) help.



  10. Re: I don't understand why I'm getting "Bad file descriptor "

    Kenny Riodan wrote:
    > Harold was merely pointing out the usenet convention
    > that each thread should address one question
    > because people are expert on different things.


    And even more important: nowadays archives are a critical part of the
    experience for Usenet (and most other forums). The ability to search old
    conversations is arguably more powerful, and certainly quicker, than
    asking a new one. And of course archive-searchers are practicing a form
    of recycling and keeping down the signal-to-noise ration, so we all
    ought make it as easy as possible to search archives. Mixing unrelated
    topics within the same thread does damage to that[*]. Just as a
    politician needs to know there's one way you talk with your pals, and a
    different way you talk when the microphone is live. The cameras are
    rolling here - it's not just about you (OP) anymore. To the degree it
    ever was.
    [*] Now, of course many threads wander far from the original point but
    that's often unavoidable because there's no single clear moment when the
    subject changed. If, on the other hand, you *know* you're changing the
    subject, you owe it to the community (from which, as Kenny points out,
    you are asking free advice) to start a new thread.

    HS

  11. Re: I don't understand why I'm getting "Bad file descriptor "

    On 17 Aug, 20:54, K-mart Cashier wrote:

    > I have a totally unrelated question. *On page 502 in the book
    > "Advanced Programming in the Unix Environment" by Stevens and Rago,
    > they have a the following function to help sychronize the parent and
    > child.
    >
    > static int pfd1[2] , pfd2[2];
    >
    > void
    > TELL_PARENT(pid_t pid)
    > {
    > * * if (write(pfd2[1], *"c", 1) != 1)
    > * * * * err_sys("write error");
    >
    > }
    >
    > What's the point of using pid if the function TELL_PARENT() doesn't
    > actually use it?



    It allows future modifications. If a library is written that provides
    this
    function, it is possible to later modify the function to use that
    argument
    without changing the API. A perfectly reasonable scenario is to
    decide that instead of merely writing an arbitrary character to
    the pipe, you might decide to send four bytes which specify
    the pid of the child so that the parent doesn't have to keep track
    of which pipe is associated with each child, and multiple children
    can share the same notification pipe.

  12. Re: I don't understand why I'm getting "Bad file descriptor "

    In article
    <23543d9e-f1ca-4b59-a3f5-d17bd425e31a@2g2000hsn.googlegroups.com>,
    K-mart Cashier wrote:

    > Given the following:
    >
    > #include /* for openpty and forkpty */
    > #include
    > #include
    > #include
    > #include
    >
    > int main(void)
    > {
    > int master, blah;
    > char slave[200];
    > char line[BUFSIZ];
    > ssize_t n;
    >
    > blah = forkpty(&master, slave, NULL, NULL);


    I notice you don't check to see if forkpty() didn't return an error.

    Why not?
    ** Posted from http://www.teranews.com **

  13. Re: I don't understand why I'm getting "Bad file descriptor "

    K-mart Cashier writes:
    > On Aug 18, 5:06 pm, rudolf wrote:
    >> K-mart Cashier wrote:
    >>
    >>
    >>
    >> > Given the following:

    >>
    >> > #include /* for openpty and forkpty */
    >> > #include
    >> > #include
    >> > #include
    >> > #include

    >>
    >> > int main(void)
    >> > {
    >> > int master, blah;
    >> > char slave[200];
    >> > char line[BUFSIZ];
    >> > ssize_t n;

    >>
    >> > blah = forkpty(&master, slave, NULL, NULL);

    >>
    >> I notice you don't check to see if forkpty() didn't return an error.
    >>
    >> Why not?

    >
    > I got sloppy in my programming. And yes, I'm aware that if I was this
    > sloppy out in the workforce, I would either get fired for reassigned
    > to corporate purchasing.


    This makes think that you are

    a) a die-hard optimist
    b) certainly don't own a computer

    :->


  14. Re: I don't understand why I'm getting "Bad file descriptor "

    K-mart Cashier wrote:
    > I got sloppy in my programming. And yes, I'm aware that if I was this
    > sloppy out in the workforce, I would either get fired or reassigned
    > to corporate purchasing.


    Or get a job offer from Microsoft

    Regards,

    Lars

+ Reply to Thread