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

J

J Peterman

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

osmium

J said:
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.
 
R

Richard Heathfield

J said:
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.
 
J

J Peterman

osmium said:
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

J Peterman

Richard said:
J Peterman wrote:




(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
 
J

Joona I Palaste

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 ([email protected]) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"And according to Occam's Toothbrush, we only need to optimise the most frequent
instructions."
- Teemu Kerola
 
O

osmium

J said:
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.
 
M

Mark McIntyre

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top