Sponsored Content
Top Forums Programming Having some trouble with select() call in C Post 302391899 by Legend986 on Tuesday 2nd of February 2010 09:50:07 PM
Old 02-02-2010
Having some trouble with select() call in C

I have this while loop:

Code:
while (notdone) {

		//Set the timers
		waitd.tv_sec = 5;
		waitd.tv_usec = 0;

		FD_ZERO(&tempreadfds);
		FD_ZERO(&tempwritefds);

		FD_ZERO(&readfds);          /* initialize the read fd set */
		FD_ZERO(&writefds);			/* initialize the write fd set */

		FD_SET(parentfd, &readfds); /* add listener socket fd */
		FD_SET(0, &readfds);        /* add stdin fd (0) */


		tempreadfds = readfds; //make a copy
		tempwritefds = writefds; //make a copy

		rv = select(fdmax+1, &tempreadfds, &tempwritefds, (fd_set*) 0, &waitd);
		if (rv < 0) {
			error("ERROR in select");
		} else if(rv == 0) {
			printf("Timeout occurred! No data after 5 seconds\n");
		}
  
                printf("Select called\n");
		fflush(stdout);
}

I was assuming that "Select called" should be printed every 5 seconds but it gets printed only once. Can someone tell me what mistake I am doing?
 

9 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

how to differentiate system call from library call

Hi, Ho do I differentiate system call from library call? for example if I am using chmod , how do I find out if it is a system call or library call? Thanks Muru (2 Replies)
Discussion started by: muru
2 Replies

2. UNIX for Advanced & Expert Users

how to use exceptfds argument in select system call

Hi, Can any one tell me how to use the fourth argument of select system call.I saw example "port forwarding" on the net,but it was too complex for me to understand.Can any one explain me about the usage of exceptfds argument of select system call with simple example. Thanks. (2 Replies)
Discussion started by: bvijaya
2 Replies

3. Infrastructure Monitoring

diffrence between method call and function call in perl

Hello, I have a problem with package and name space. require "/Mehran/DSGateEngineLib/general.pl"; use strict; sub System_Status_Main_Service_Status_Intrusion_Prevention { my %idpstatus; my @result; &General_ReadHash("/var/dsg/idp/settings",\%idpstatus); #print... (4 Replies)
Discussion started by: Zaxon
4 Replies

4. UNIX for Dummies Questions & Answers

Why do I need to call make if I call gcc ?

Why do I need to call make if I call gcc ? I thought gcc already compiles the sources. thanks (1 Reply)
Discussion started by: aneuryzma
1 Replies

5. Programming

select() system call takes longer than the timeout specified

Below is my code. Every once in a while the select call takes as long as 150 seconds (discovered by printing time before and after this statement) while the timeout specified into it is only 1 second. Any clue why? I can't believe that select call which has been around for centuries can have a bug,... (15 Replies)
Discussion started by: old_as_a_fossil
15 Replies

6. Programming

Trouble with C

Hey, i am having a problem First, i know java well and i have used C++ on occasion so i thought i would be able to deal with a class where they program in C. unfortunately i have hit some speed bumps that i am having problems. Here is my problem: I have a structure cache_t in the sample... (0 Replies)
Discussion started by: zephoid
0 Replies

7. Boot Loaders

Reboot and Select Proper Boot device or insert Boot media in select Boot device and press a key

Hello, I have kubuntu on my laptop and now I decided to switch to Windows 7. I made the bios settings properly (first choice is boot from cd\vd) but I see the error " reboot and select proper Boot device or insert Boot media in select Boot device and press a key " I have tried CD and... (0 Replies)
Discussion started by: rpf
0 Replies

8. Homework & Coursework Questions

program to send messages to parent using pipes and select system call

Write a program using select, which will create some number of child processes that continuously send text messages to the parent process using pipes. Each child has its own pipe that it uses to communicate with the parent. The parent uses select () to decide what pipes should be processed to... (1 Reply)
Discussion started by: ripssingh
1 Replies

9. UNIX for Advanced & Expert Users

Issues with select system call

1. We are using client-server model communication using TCP/IP protocol 2. The TCP socket created with O_NON_BLOCK flag 3. When we make attempt to send large data to other process, the send is partially successful. It means we attempt to send 90K data, OS sent only 40K data successfully. ... (3 Replies)
Discussion started by: MasthanDudekula
3 Replies
SELECT(2)						      BSD System Calls Manual							 SELECT(2)

NAME
select -- synchronous I/O multiplexing SYNOPSIS
#include <sys/select.h> - or - #include <sys/types.h> #include <sys/time.h> #include <unistd.h> int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout); FD_SET(fd, &fdset); FD_CLR(fd, &fdset); FD_ISSET(fd, &fdset); FD_ZERO(&fdset); DESCRIPTION
Select() examines the I/O descriptor sets whose addresses are passed in readfds, writefds, and exceptfds to see if some of their descriptors are ready for reading, are ready for writing, or have an exceptional condition pending, respectively. The first nfds descriptors are checked in each set; i.e., the descriptors from 0 through nfds-1 in the descriptor sets are examined. On return, select() replaces the given descriptor sets with subsets consisting of those descriptors that are ready for the requested operation. Select() returns the total number of ready descriptors in all the sets. The descriptor sets are stored as bit fields in arrays of integers. The following macros are provided for manipulating such descriptor sets: FD_ZERO(&fdset) initializes a descriptor set fdset to the null set. FD_SET(fd, &fdset) includes a particular descriptor fd in fdset. FD_CLR(fd, &fdset) removes fd from fdset. FD_ISSET(fd, &fdset) is non-zero if fd is a member of fdset, zero otherwise. The behavior of these macros is undefined if a descriptor value is less than zero or greater than or equal to FD_SETSIZE, which is normally at least equal to the maximum number of descriptors supported by the system. If timeout is a non-nil pointer, it specifies a maximum interval to wait for the selection to complete. If timeout is a nil pointer, the select blocks indefinitely. To effect a poll, the timeout argument should be non-nil, pointing to a zero-valued timeval structure. Timeout is not changed by select(), and may be reused on subsequent calls, however it is good style to re-initialize it before each invocation of select(). Any of readfds, writefds, and exceptfds may be given as nil pointers if no descriptors are of interest. RETURN VALUES
Select() returns the number of ready descriptors that are contained in the descriptor sets, or -1 if an error occurred. If the time limit expires, select() returns 0. If select() returns with an error, including one due to an interrupted call, the descriptor sets will be unmod- ified. ERRORS
An error return from select() indicates: [EBADF] One of the descriptor sets specified an invalid descriptor. [EINTR] A signal was delivered before the time limit expired and before any of the selected events occurred. [EINVAL] The specified time limit is invalid. One of its components is negative or too large. SEE ALSO
accept(2), connect(2), getdtablesize(2), gettimeofday(2), read(2), recv(2), send(2), write(2) BUGS
Although the provision of getdtablesize(2) was intended to allow user programs to be written independent of the kernel limit on the number of open files, the dimension of a sufficiently large bit field for select remains a problem. The default size FD_SETSIZE (currently 1024) is somewhat smaller than the current kernel limit to the number of open files. However, in order to accommodate programs which might poten- tially use a larger number of open files with select, it is possible to increase this size within a program by providing a larger definition of FD_SETSIZE before the inclusion of <sys/types.h>. Select() should probably have been designed to return the time remaining from the original timeout, if any, by modifying the time value in place. However, it is unlikely this semantic will ever be implemented, as the change would cause source code compatibility problems. In general it is unwise to assume that the timeout value will be unmodified by the select() call, and the caller should reinitialize it on each invocation. HISTORY
The select() function call appeared in 4.2BSD. 4.2 Berkeley Distribution March 25, 1994 4.2 Berkeley Distribution
All times are GMT -4. The time now is 01:45 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy