Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Help me code a small programming exercise? File descriptors...

Reply
Thread Tools

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

 
 
J Peterman
Guest
Posts: n/a
 
      10-12-2003
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...

 
Reply With Quote
 
 
 
 
osmium
Guest
Posts: n/a
 
      10-12-2003
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.


 
Reply With Quote
 
 
 
 
Richard Heathfield
Guest
Posts: n/a
 
      10-12-2003
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 : http://www.velocityreviews.com/forums/(E-Mail Removed)
"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
 
Reply With Quote
 
J Peterman
Guest
Posts: n/a
 
      10-13-2003
> 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.

 
Reply With Quote
 
J Peterman
Guest
Posts: n/a
 
      10-13-2003
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.
>


 
Reply With Quote
 
Joona I Palaste
Guest
Posts: n/a
 
      10-13-2003
J Peterman <(E-Mail Removed)> 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 ((E-Mail Removed)) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"And according to Occam's Toothbrush, we only need to optimise the most frequent
instructions."
- Teemu Kerola
 
Reply With Quote
 
osmium
Guest
Posts: n/a
 
      10-13-2003
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.



 
Reply With Quote
 
Fao, Sean
Guest
Posts: n/a
 
      10-13-2003
This looks like a question for comp.unix.programmer.


 
Reply With Quote
 
Mark McIntyre
Guest
Posts: n/a
 
      10-13-2003
On Mon, 13 Oct 2003 15:46:23 +0800, in comp.lang.c , J Peterman
<(E-Mail Removed)> 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 =---
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can somebody please help me start programming a small UNIX kernel in C++ ? yourchandrashekhar@gmail.com C++ 2 08-19-2007 06:51 PM
how to store a small file into a database table in programming? =?Utf-8?B?ZGF2aWQ=?= ASP .Net 2 02-21-2007 06:14 AM
Small cameras getting too small? GRL Digital Photography 50 02-03-2006 03:12 AM
Small Square with small red X Peter Coddington Computer Support 4 01-03-2006 06:58 AM
[OT] want to hire an expert for a small programming task Peppe Polpo C++ 0 02-20-2005 08:35 PM



Advertisments