469,568 Members | 1,371 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,568 developers. It's quick & easy.

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

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...

Nov 13 '05 #1
8 3380
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.
Nov 13 '05 #2
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 : bi****@eton.powernet.co.uk
"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
Nov 13 '05 #3
> 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.

Nov 13 '05 #4
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.


Nov 13 '05 #5
J Peterman <J@nospam.please> 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 (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"And according to Occam's Toothbrush, we only need to optimise the most frequent
instructions."
- Teemu Kerola
Nov 13 '05 #6
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.

Nov 13 '05 #7
This looks like a question for comp.unix.programmer.
Nov 13 '05 #8
On Mon, 13 Oct 2003 15:46:23 +0800, in comp.lang.c , J Peterman
<J@Nospam.please> 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 =---
Nov 13 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Tomislav Lepusic | last post: by
20 posts views Thread by titi | last post: by
11 posts views Thread by Bonj | last post: by
7 posts views Thread by Alan Bashy | last post: by
7 posts views Thread by Trvl Orm | last post: by
9 posts views Thread by MrG{DRGN} | last post: by
2 posts views Thread by DJ Dharme | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.