LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 03-20-2007, 05:52 AM   #1
mykaitch
Member
 
Registered: Feb 2007
Posts: 30

Rep: Reputation: 15
Tidy exit in C


I have written a small C prog that allows me to get sound samples from a sound card or USB device, process them and then output as fast as poss, almost realtime ( I used ALSA and the C lib).
At the mo to end the prog I use CTL+C, which is nasty.
Is there any way (Callback if I knew how???) to set something up so that 'press any key' could be used instead. I tried a few kbhit() variants from t'net happen, but they all caused too much latency.

Thanks
 
Old 03-20-2007, 06:52 AM   #2
wjevans_7d1@yahoo.co
Member
 
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938

Rep: Reputation: 30
Wait, I don't know what you're really asking.

What's wrong with just reading a line from stdin or something?
 
Old 03-20-2007, 07:23 AM   #3
Nick_Battle
Member
 
Registered: Dec 2006
Location: Bracknell, UK
Distribution: SUSE 13.1
Posts: 159

Rep: Reputation: 32
stdin is line buffered on a tty by default, so just reading it won't show any data until you hit return. You can put the tty in raw mode, but that's presumably what the kbhit() type functions do.

How about creating a separate thread to listen to the keyboard (with a blocking read)?
 
Old 03-20-2007, 10:27 AM   #4
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,287

Rep: Reputation: 173Reputation: 173
Quote:
Originally Posted by Nick_Battle
How about creating a separate thread to listen to the keyboard (with a blocking read)?
I hope you are joking

what's wrong with CTRL-C anyway?
 
Old 03-20-2007, 10:38 AM   #5
Nick_Battle
Member
 
Registered: Dec 2006
Location: Bracknell, UK
Distribution: SUSE 13.1
Posts: 159

Rep: Reputation: 32
I wasn't really joking - I think I've seen systems with listening threads like this - but I agree that it's overkill for a simple case. A signal, with a handler to clean up and terminate gracefully, sounds fine to me.
 
Old 03-20-2007, 11:55 AM   #6
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,395
Blog Entries: 2

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
I think your question is answered in http://www.linuxquestions.org/questi...07#post2570807
If searching elsewhere, I believe this question can be rephrased as 'how do I do non-blocking character-at-a-time terminal IO'. Some other OS's have a function called getch(), which does this, and searching for that can reveal some ansewers, as well.
--- rod.
 
Old 03-21-2007, 02:50 AM   #7
mykaitch
Member
 
Registered: Feb 2007
Posts: 30

Original Poster
Rep: Reputation: 15
kbhit()

Its ok guys, I have fixed it. Yes a thread would be best but although I can use threads in C++ I have yet to try them in C. Anyway, I discovered 'raw' mode yesterday and now have a very fast kbhit() which works fine.
PS I am a Linux newbie, this is week three for me.
 
Old 03-21-2007, 03:12 AM   #8
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,287

Rep: Reputation: 173Reputation: 173
threads are needed in windows beacause it has no decent
process forking. It's Funny this whole threads obsession - I reckon it's come over from windows programmers.
In "Advanced Unix Programming" Richard Stevens himself devotes a few chapters and concludes threads aren't worth the hassle on unix environments (in the vasy majority of cases) because of the cheapness of forking a new process.

right or wrong.
 
Old 03-22-2007, 09:39 AM   #9
mykaitch
Member
 
Registered: Feb 2007
Posts: 30

Original Poster
Rep: Reputation: 15
'threads are needed in windows beacause it has no decent
process forking'.
I always understood a fork as a secondary thread of execution. At some point a fork might rejoin the main thread of execution, indeed the fork might be stalled waiting to rejoin, so I wonder if threads and forks are very much the same ? The biggest pain in the butt with windoze (for me) is the task scheduler which loads (10ms ??) latency into any (hah( real time function. My Penguin does not and I love him for it.
 
Old 03-22-2007, 09:58 AM   #10
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,287

Rep: Reputation: 173Reputation: 173
no, a unix fork is a cloned child of the parent process.
It never comes back.
they are seperate beings - the child does inherit open network sockets and filehandles but no
sharing of other variables.

Granted this means communication may need to be set up between parent and child, but Mr Stevens
argues that this is much less error-prone than sharing areas of memory and variables
with their inherent problems. (and simpler to debug)
 
Old 03-22-2007, 10:09 AM   #11
wjevans_7d1@yahoo.co
Member
 
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938

Rep: Reputation: 30
Just my two cents' worth from personal experience.

Threads are useful only if two things are true:
  1. there is frequent communication among the processes; AND
  2. performance is an issue.

If either one of these is false, use processes and fork() instead.

The main reason is that threads, unlike processes, share their heap. And if you have a thread which is misbehaving because the heap is trashed (typically caused by buffer overrun or use of memory that has already been free()d), it is usually useless to find out when in that thread the heap got trashed, because it's quite likely that some other heap, anywhere in its code, trashed the heap. These bugs are quite expensive to diagnose and fix.

Just not worth it unless absolutely necessary. And if it becomes necessary, keep that sucker as simple as possible.
 
Old 03-23-2007, 03:18 AM   #12
mykaitch
Member
 
Registered: Feb 2007
Posts: 30

Original Poster
Rep: Reputation: 15
Thanks for that, its nice to get good explanations if things. Seems UNIX is going to upset my little world but it should all be worth it. I am going to investigate fork() and see what I can do. Its a shame that this (subtley?) wasnt pointed out by my tutors, but heigh ho, everyday something new.
Cheers
 
  


Reply

Tags
exit, key


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Tidy? Which version to get and how? the_gripmaster Linux - Software 2 03-02-2007 01:37 AM
DISCUSSION: Tidy Up Those Tags jeremy LinuxAnswers Discussion 0 02-05-2007 05:39 PM
DISCUSSION: Tidy Up With HTML Tidy jeremy LinuxAnswers Discussion 0 10-10-2005 07:25 PM
the output is not tidy ust Linux - Software 1 10-04-2004 01:30 AM
HTML Tidy and PHP 4 atom Programming 0 10-01-2004 10:06 AM


All times are GMT -5. The time now is 05:53 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration