# Help with non-blocking IO



## soundstep (Jan 12, 2011)

Hi,

I'm trying to find an answer to a problem I have and someone told that I could get an answer here as I'm using something OS specific (Mac).

I'm trying to write a non-blocking IO in C, I said trying because I'm new to C programming (2 days..).

It seems that I almost have a solution with the select() method.

The idea is that I'm writing a wrapper in C (for libspotify) that will interact with other parts of a project. The library is using a thread to update with some events but I still need to enable a user IO capability.

So as I said I have something very close but I get a "Interrupted system call" error with the select() method.

I'd like to understand if this error is normal, can be avoided, and if I'm going to miss some input because of it.

This error happen when I call a "processEvent" method on the library.

I pasted the code below if someone can enlight me with that... hoping I'm in the right place.

Thanks in advance.

Romu


```
#include <stdio.h>
		#include <stdlib.h>
		#include <stdint.h>
		#include <string.h>
		#include <signal.h>
		#include <pthread.h>
		
		#include <string>
		#include <sstream>
		#include <iostream>
		
		#include "appkey.h"
		#include "libspotify/api.h"
		#include "spotify.h"
		
		#include <sys/socket.h>
		#include <termios.h>
		
		#define BUFFER_SIZE 8195
		#define FD_NUM 0
		
		fd_set rfds;
		struct timeval tv;
		int retval;
		int cnt;
		int readResult;
		char buffer[BUFFER_SIZE];
		pthread_t mainthread = 0;
		
		static void sigIgn(int signo) { }
		
		int timed() {
			
			/* Watch stdin (fd 0) to see when it has input. */
		    FD_ZERO(&rfds);
		    FD_SET(FD_NUM, &rfds);
			
			/* Wait up to five seconds. */
		    tv.tv_sec = 1;
		    tv.tv_usec = 0;
			
			retval = select(1, &rfds, NULL, NULL, &tv);
		    /* Don't rely on the value of tv now! */
			
			if (retval == -1) {
		        perror("select()");
				return 0;
			}
		    else if (retval) {
				/* FD_ISSET(0, &rfds) will be true. */
				readResult = read(FD_NUM, buffer, BUFFER_SIZE);
		        printf("Data is available now: %s\n", buffer);
				
				//write(0, buffer, cnt);
				
				return 1;
			}
		    else {
		        printf("No data within five seconds.\n");
				return 2;
			}
		}
		
		int main(void) {
			
			mainthread = pthread_self();
			signal(SIGIO, &sigIgn);
			
			if(spotify->InitSession())
				printf("valid session.\n");
			else
				printf("invalid session.\n");
			
			spotify->SetCredentials("breellondon", "spotifybreel1234");
			spotify->Login();
			
			sigset_t sigset;
			sigemptyset(&sigset);
			sigaddset(&sigset, SIGIO);
			
			while (timed() != 0) {
			
				std::cout << "looping true\n";
		
				int timeout = -1;
				pthread_sigmask(SIG_BLOCK, &sigset, NULL);
				spotify->ProcessEvents(&timeout);
				pthread_sigmask(SIG_UNBLOCK, &sigset, NULL);
				
			}
			
			return 0;
		}
```


----------



## SirDice (Jan 12, 2011)

soundstep said:
			
		

> I'm trying to find an answer to a problem I have and someone told that I could get an answer here as I'm using something OS specific (Mac).



OS-X != FreeBSD.

If it's OS specific perhaps you should ask your question on an OS specific board?

Also note that libspotify is NOT available on FreeBSD. 

Try Mac OS-X Hints; Forum.


----------

