LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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 07-17-2010, 06:33 PM   #16
raedbenz
Member
 
Registered: Dec 2007
Posts: 93

Original Poster
Rep: Reputation: 15

could u plz specify an example code ?
cheers
 
Old 07-17-2010, 06:37 PM   #17
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by raedbenz View Post
could u plz specify an example code ?
cheers
No. That's for you to learn. Start from here: https://computing.llnl.gov/tutorials/pthreads/ .
 
Old 07-17-2010, 07:23 PM   #18
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: FreeBSD,Debian wheezy
Posts: 811

Rep: Reputation: 179Reputation: 179
Any time I want to write a program which doesn't hang waiting for input, but rather is free to do other things if input is not available, I use select(). I highly recommend that you investigate it.
 
Old 07-17-2010, 07:44 PM   #19
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by wje_lq View Post
Any time I want to write a program which doesn't hang waiting for input, but rather is free to do other things if input is not available, I use select(). I highly recommend that you investigate it.
You still need to see somehow that the input you want is there. I.e. you will have to organize some polling. So that's why I'm suggesting threads - using them one thread just polls a flag signifying the input is ready; the flag is set by the thread stuck in waiting for input.

Looks like a more generic solution to me, and the percentage of CPU time needed to poll the flag can be set by the corresponding thread priority.
 
Old 07-18-2010, 02:35 AM   #20
raedbenz
Member
 
Registered: Dec 2007
Posts: 93

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by wje_lq View Post
Any time I want to write a program which doesn't hang waiting for input, but rather is free to do other things if input is not available, I use select(). I highly recommend that you investigate it.
hello,
i have already used select() with open,, read and write, and i tried to used it with fopen, fwrite, fread an it didn't work, like that:
Code:
    fd_set rfds;
    struct timeval tv;
    int retval, nfd;

    nfd = fileno( rfcomm ); //converts the FILE pointer to integer
    FD_ZERO(&rfds);
    FD_SET(nfd, &rfds);
    tv.tv_sec = 1;
    tv.tv_usec = 0;
    retval = select(1+nfd, &rfds, NULL, NULL, &tv);

    if (retval == -1)
        perror("select()");
    else if (retval)
        printf("Data is available now.\n");
    else
        printf("No data within 1 seconds.\n");
an ii kept getting No data available.
cheers
 
Old 07-19-2010, 01:41 PM   #21
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,187

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
Quote:
Originally Posted by raedbenz View Post
could u plz specify an example code ?
Have you looked at the standard method for setting up interrupt driven systems? Specifically, the use of call-back functions triggered by hardware events. Examples abound, and shouldn't be hard to find. (Do recall that almost all GUI systems use interrupt-driven call-back functions.)

The analogy between what you're trying to accomplish and what the OS does to read the keyboard made, above, by Sergei Steshenko should, I believe, have suggested using event-driven call-back functions. (Which, as Sergei also noted, can be implemented as a thread processing asynchronous events. Did you look at the tutorial on POSIX threads to which he pointed you?)
 
  


Reply



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
[SOLVED] ftell always returns 0 10110111 Programming 3 12-31-2009 04:47 PM
Strange fseek issue ? C programming exvor Programming 3 10-05-2006 11:20 AM
fseek problem tokernizer Programming 0 04-27-2006 07:21 PM
fseek on a socket descriptor to discard socket buffer? Thinking Programming 1 12-06-2005 09:15 PM
fseek cross NFS is really slow chiuyaa Linux - Networking 0 06-23-2004 08:14 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:33 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration