ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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");
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?)
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.