Geek Pop Quiz.
Apr. 11th, 2008 03:52 pmWhat I really want is "sleep 0.1" or something similar, that I can then adjust up and down to give myself fractions of a second.
Once I can do that, I want to be able to delay a random amount of time - say, between 0 and 1 second, evenly distributed.
And if I have to go to Perl to do it, so be it, let's go to Perl. In that case, how do I do it, and how do I make a call to a command-line script from within my Perl script?
EDIT: Done!
---usleep.c---
int main (int argc, char **argv) {
usleep(atoi(argv[1]));
return 0;
}
---usleep.c---
+ "gcc -o usleep usleep.c"
+ "mv usleep /usr/bin"
= a working usleep implementation for Ubuntu.
Of course, the fact that "sleep" takes floating point arguments in Ubuntu and I just didn't read that part because I'm dumb and made an assumption means this isn't necessary.
Remaining issue:
Shell script, ubuntu linux. I want a way to generate a random number between 0 and 1, so I can loop like this:
Loop {
Do Something
sleep (random number between 0 and 1)
}
Help me, lazyweb!
(no subject)
Date: 2008-04-11 07:56 pm (UTC)(no subject)
Date: 2008-04-11 08:10 pm (UTC)(no subject)
Date: 2008-04-11 08:15 pm (UTC)(no subject)
Date: 2008-04-11 08:18 pm (UTC)(no subject)
Date: 2008-04-11 08:18 pm (UTC)(no subject)
Date: 2008-04-11 08:17 pm (UTC)Leave the first three arguments as undef, and set the fourth to the fraction of a section you want it to sleep. The following sleeps for a quarter second.
select(undef, undef, undef, 0.25);
to execute a command line, you use the system() function (assuming you want the perl script to wait until the system call is done).
so if you wanted to call ls -d (for a random example, I don't even remember if -d is a valid argument on ls) you can do
@args = ("ls", "-d");
system(@args) == 0 or die "system @args failed: $?"
(this catches when your system call fails and prints out an error message, btw).
Each argument of the system call is another item in your @args array.
Your program will be executed in the current working directory.
(no subject)
Date: 2008-04-11 08:18 pm (UTC)(no subject)
Date: 2008-04-12 01:37 am (UTC)(no subject)
Date: 2008-04-11 08:19 pm (UTC)(no subject)
Date: 2008-04-11 08:23 pm (UTC)(no subject)
Date: 2008-04-11 08:23 pm (UTC)I'm SPESHUL today.
Thanks!
(no subject)
Date: 2008-04-11 08:22 pm (UTC)I don't know what profiling tools Ubuntu has because I'm an ignorant dork. But, you could try doing, say, a printout of system time as the LONGINT and then ten thousand of the echoes, and another printout of the system time, and do that fifty times or so, and get an idea of how long it takes to do ten thousand of yer NOP and with a little math, voila.
(no subject)
Date: 2008-04-11 08:32 pm (UTC)All but the first three replies occurred while I was writing mine. Damn.
(no subject)
Date: 2008-04-11 08:35 pm (UTC)(no subject)
Date: 2008-04-11 08:42 pm (UTC)(no subject)
Date: 2008-04-12 12:35 am (UTC)(no subject)
Date: 2008-04-12 01:05 am (UTC)What's the syntax for Rand() in bash or sh, again?
(no subject)
Date: 2008-04-12 01:43 am (UTC)(no subject)
Date: 2008-04-12 01:44 am (UTC)This should do the trick:
sam@alchemist:~$ for var in 1 2 3 4 5; do echo 0.$(( $RANDOM * 100 / 32767 )); done
0.32
0.90
0.93
0.3
0.54
(no subject)
Date: 2008-04-12 03:45 pm (UTC)Comes stock on most modern deadrat implementations as part of the initscripts RPM.
We have a programmer at work that couldn't figure out the usleep(3) call, and instead just did a while loop that watched the system clock -- with two loops running in two threads.. I'll call the app "Loopy."
Loopy happily ran on a 12x300MHz sun box for 6 years, and the programmer was so lauded for the business usefulness of the application that the programmer was promoted to VP, and then to Executive Director in that same time.
Well, after 6 years, the Sun box became unreliable. Loopy generates several million dollars of revenue every week, so, we had to put it on new hardware. The ED decided that since Loopy used 16.6% of the 12x300=3600MHz machine, it would run splendidly if recompiled for Linux, because a 2xDualCorex3000MHz machine would be 12,000MHz.
So, the 6-yr-old code was pulled out of the archive, and compiled fine on Linux. We rolled it out to production, and found that it used 50% of the CPU in the new 12,000MHz machine! Our rule is to double the hardware whenever we use 50% of the capacity.
But, the ED that wrote the code has now decided that Linux is incredibly inefficient and inappropriate for the task. We're switching back to Solaris.
Brilliant!
And I'm not allowed to say anything because this ED controls our budget and currently likes us.