1

I'm reading in multuple tiny files and outputing them in a single file that is 5 seconds long. I can't control when the input files come in, how large they are, length of time contained inside of each etc. I want to output a full file every 5 seconds with all the data in every file generated for that 5 second interval. I want this to act in a quasi-streaming manner, no buffering files and creating output 5 seconds in the future.

This in a situation like this I have to handle the case when no input file exists by sleeping and waking up to pole for new values. However, I also need to stop my sleep/polling when I hit the 5 second mark so I output my new 5 second block. Which implies I have to be doing some comparisons to a clock to know if 5 seconds have passed and I'm ready to output my results.

I'm wondering if each of these wall clock checks will become system calls to the OS with all the expense tht entails? If possible, without too much effort, I would prefer to minimize CPU calls to get the wall clock (mainly to save CPU, I'll be doing very quick polling and the constant CPU calls for each of dozens of applications may actually burn up some CPU). What I would prefer is a system that works off of cpu cycles and only does a call to the OS on occasion to re-sync it's internal timer. Now for this program it's not worth going out of my way to implement anything complicated, but it made me wonder. Does the c++ language already mave any methods that do what I described, working with cpu time and syncing to wall clock intermittently, or do they always do a system call every time I try to get time?

Thanks

dsollen
  • 6,046
  • 6
  • 43
  • 84
  • Why will you do that very quick polling? Polling is, in most cases, completely inefficient... – Griwes Jun 18 '12 at 18:44
  • What's wrong with waking up every 5 seconds and merging what is available at that time? – jxh Jun 18 '12 at 19:40
  • I want to get the file out as close to the 5 second mark as possible, and don't know how large the files would be or when I will receive them. If I wake up at the 5 minute mark I may find a huge file with all 5 seconds worth of data that takes time to do all the parsing on. which would delay when I can do my outputs. I want to use the five seconds I'm sitting around doing nothing to parse as much as I can. – dsollen Jun 18 '12 at 20:40

2 Answers2

2

Yes, essentially the operating system will provide facility for getting the wall time for the process.

Of course, the operating system too is essentially a program and could be written in C++, and as such it would need to query the time directly from hardware. That is indeed possible, but requires privileges for directly accessing hardware. In modern operating systems this isn't usually possible, so the operating system itself(which has sufficien privileges) does this job and provides an interface for obtaining the wall-clock time for so-called "user space" programs.

zxcdw
  • 1,629
  • 1
  • 10
  • 18
0

That's a platform-dependent question. The Linux syscall you're talking about is gettimeofday(), and it is specifically written to have extremely low overhead. On some machines, as low as 20ns. Here's some detail on how it's done:

faster equivalent of gettimeofday

The basic trick is that not every OS service requires a context switch. In this case, Linux can provide the current time with high resolution, while staying entirely in userspace. Unless you can prove you've created a bottleneck, trust your C library to do the right thing.

Community
  • 1
  • 1
Peter
  • 14,559
  • 35
  • 55
  • this is what I was asking, and what I expected you to say. I figured C would do things for me, but I wanted to ask just to be sure (plus I was curious). Thank you. – dsollen Jun 18 '12 at 20:42