Help me code a small programming exercise? File descriptors...

Discussion in 'C Programming' started by J Peterman, Oct 12, 2003.

  1. J Peterman

    J Peterman Guest

    I need to do this exercise, but am having problems.

    I need to write a program that firstly, sleeps for 5 seconds, then reads
    a line of input from file descriptor 0 and then writes the line back to
    file descriptor 1.

    Apparrantly, this program would block forever unless you type a line of
    text on the keyboard, but I can't get a working program to try.

    Next, this program needs to be modified so that it would write its input
    from file descriptor 0 to file descriptor 1 IF there is an input line
    from the keyboard. Otherwise it should report no input and terminate.

    Can anyone help me with this please? I've spent far too long on it
    already, and it's holding me up from moving to the next exercise...
     
    J Peterman, Oct 12, 2003
    #1
    1. Advertising

  2. J Peterman

    osmium Guest

    J Peterman writes:

    > I need to do this exercise, but am having problems.
    >
    > I need to write a program that firstly, sleeps for 5 seconds, then reads
    > a line of input from file descriptor 0 and then writes the line back to
    > file descriptor 1.
    >
    > Apparrantly, this program would block forever unless you type a line of
    > text on the keyboard, but I can't get a working program to try.
    >
    > Next, this program needs to be modified so that it would write its input
    > from file descriptor 0 to file descriptor 1 IF there is an input line
    > from the keyboard. Otherwise it should report no input and terminate.
    >
    > Can anyone help me with this please? I've spent far too long on it
    > already, and it's holding me up from moving to the next exercise...


    Try the following buzzwords on google and also on google advanced groups:

    conio curses ncurses kbhit

    If that doesn't work post to a group that talks about *your* compiler, not
    the generic language which is what this group is.
     
    osmium, Oct 12, 2003
    #2
    1. Advertising

  3. J Peterman wrote:

    > I need to do this exercise, but am having problems.

    ^^^^^^^^^^^^^^^^^^^^^^^^^^
    >
    > I need to write a program that firstly, sleeps for 5 seconds, then reads

    ^^^^^^^^^^^^^^^^^^^^^^^^^
    > a line of input from file descriptor 0 and then writes the line back to
    > file descriptor 1.


    (Note the marked text, above.)

    This is not a do-my-homework forum. If you have a specific question about
    ISO C, feel free to ask it here. If your ISO C code doesn't work, show it
    to us and perhaps we can help you fix it.

    Note that C provides no idle wait routine as standard, nor does it deal with
    file descriptors. The closest you can get in standard C is a busy loop with
    time() for the waiting, and stream pointers (FILE *) for the I/O.

    --
    Richard Heathfield :
    "Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
    C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
    K&R answers, C books, etc: http://users.powernet.co.uk/eton
     
    Richard Heathfield, Oct 12, 2003
    #3
  4. J Peterman

    J Peterman Guest

    > osmium wrote:
    > Try the following buzzwords on google and also on google advanced groups:
    >
    > conio curses ncurses kbhit
    >
    > If that doesn't work post to a group that talks about *your* compiler, not
    > the generic language which is what this group is.


    Sorry, I thought this group was for programming ic C....which is what I
    am trying to write this program in. I'm afraid I don't know enough of
    what is going on to know where I *should* be posting.
     
    J Peterman, Oct 13, 2003
    #4
  5. J Peterman

    J Peterman Guest

    Richard Heathfield wrote:
    > J Peterman wrote:
    >
    >
    >>I need to do this exercise, but am having problems.

    >
    > ^^^^^^^^^^^^^^^^^^^^^^^^^^
    >
    >>I need to write a program that firstly, sleeps for 5 seconds, then reads

    >
    > ^^^^^^^^^^^^^^^^^^^^^^^^^
    >
    >>a line of input from file descriptor 0 and then writes the line back to
    >>file descriptor 1.

    >
    >
    > (Note the marked text, above.)
    >
    > This is not a do-my-homework forum. If you have a specific question about
    > ISO C, feel free to ask it here. If your ISO C code doesn't work, show it
    > to us and perhaps we can help you fix it.



    While I don't expect anyone to do my homework for me, I figured it
    wasn't worth posting my own code, mainly because I have so little of it.
    I just figured it was an easy enough exercise for someone to do in about
    2min...That way, I could take note and move on.

    For what it's worth, here is what I have...

    /* begin code */

    #include <stdio.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <unistd.h>


    main()
    {
    int nread;
    char buf[100];


    pid_t pid;
    pid = fork();


    if(pid ==0)
    {
    sleep(5);
    nread = read(0, buf, 100);
    // write(1, buf, nread);
    printf("DEBUG: Value of nread = %d\n",nread);
    }



    return(0);
    }


    /* end code */

    I'm trying to implement read, but it keeps giving me an error...Once I
    get that sorted, I think I need to implement the write line.

    Any help is appreciated...Thanks

    > Note that C provides no idle wait routine as standard, nor does it deal with
    > file descriptors. The closest you can get in standard C is a busy loop with
    > time() for the waiting, and stream pointers (FILE *) for the I/O.
    >
     
    J Peterman, Oct 13, 2003
    #5
  6. J Peterman <> scribbled the following:
    > > osmium wrote:
    >> Try the following buzzwords on google and also on google advanced groups:
    >>
    >> conio curses ncurses kbhit
    >>
    >> If that doesn't work post to a group that talks about *your* compiler, not
    >> the generic language which is what this group is.


    > Sorry, I thought this group was for programming ic C....which is what I
    > am trying to write this program in. I'm afraid I don't know enough of
    > what is going on to know where I *should* be posting.


    This group IS for programming C. C is a programming language, not a Unix
    or a Window OS interface. C has been standardised by ISO, the
    International Organisation for Standardisation, and conio, curses,
    ncurses and kbhit, amongst a lot of other things, fall outside the scope
    of that standard. Why? Because they are not even needed on all
    platforms, and to give implementors more freedom in how they design the
    interface to their OS.
    Come to think of it, it's possible to write an industry-strength
    application costing millions of dollars without ever needing to clear
    the screen, or read directly from the keyboard! How does this sound?
    As to which groups you should be posting in, that depends on your OS.
    Is it Unix? Then post to comp.unix.programmer. Is it Windows? Then post
    to comp.os.ms-windows.programmer.misc or
    comp.os.ms-windows.programmer.win32.

    --
    /-- Joona Palaste () ------------- Finland --------\
    \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
    "And according to Occam's Toothbrush, we only need to optimise the most frequent
    instructions."
    - Teemu Kerola
     
    Joona I Palaste, Oct 13, 2003
    #6
  7. J Peterman

    osmium Guest

    J Peterman wrote:

    > While I don't expect anyone to do my homework for me, I figured it
    > wasn't worth posting my own code, mainly because I have so little of it.
    > I just figured it was an easy enough exercise for someone to do in about
    > 2min...That way, I could take note and move on.


    The main problem is that to do what you want to do will require extensions
    to the base language, those extensions will be provided by *your* compiler,
    not a generic ISO C compiler. Thus my hint to post to a newsgroup that
    matches your compiler.
    ..
    >
    > For what it's worth, here is what I have...
    >
    > /* begin code */
    >
    > #include <stdio.h>
    > #include <sys/types.h>
    > #include <sys/stat.h>
    > #include <fcntl.h>
    > #include <unistd.h>


    Sevrral of those are not standard C headers. So even if it worked, it falls
    outside the domain of this newsgroup.
     
    osmium, Oct 13, 2003
    #7
  8. J Peterman

    Fao, Sean Guest

    This looks like a question for comp.unix.programmer.
     
    Fao, Sean, Oct 13, 2003
    #8
  9. On Mon, 13 Oct 2003 15:46:23 +0800, in comp.lang.c , J Peterman
    <> wrote:

    >While I don't expect anyone to do my homework for me, I figured it
    >wasn't worth posting my own code, mainly because I have so little of it.
    >I just figured it was an easy enough exercise for someone to do in about
    >2min...That way, I could take note and move on.
    >
    >For what it's worth, here is what I have...
    >
    >/* begin code */
    >
    >#include <stdio.h>
    >#include <sys/types.h>
    >#include <sys/stat.h>
    >#include <fcntl.h>
    >#include <unistd.h>


    Now we know you're doing this on a variant of unix. So you need to ask
    in comp.unix.programmer. read(), fork() and pids are offtopic here
    since tehy're platform-specific.
    --
    Mark McIntyre
    CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
    CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>


    ----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
    http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
    ---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
     
    Mark McIntyre, Oct 14, 2003
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jonathan Turkanis

    Small C++ exercise

    Jonathan Turkanis, Mar 6, 2005, in forum: C++
    Replies:
    8
    Views:
    704
    beerrock
    Mar 9, 2005
  2. Mike S
    Replies:
    8
    Views:
    431
    Netocrat
    May 21, 2006
  3. mathieu
    Replies:
    1
    Views:
    406
    mathieu
    Apr 2, 2006
  4. ros

    small java exercise

    ros, Apr 3, 2007, in forum: Java
    Replies:
    35
    Views:
    1,374
    Chris Smith
    May 18, 2007
  5. mathieu
    Replies:
    5
    Views:
    371
    mathieu
    Jan 8, 2010
Loading...

Share This Page