<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: linux code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sun, 07 Sep 2008 09:32:06 GMT</pubDate>
    <description>DZone Snippets: linux code</description>
    <item>
      <title>Change Ubuntu's default text editor</title>
      <link>http://snippets.dzone.com/posts/show/6012</link>
      <description>Tired with using nano as the default text editor in Ubuntu? To change the text editor permanently to vi type the following command from the command line.&lt;br /&gt;&lt;code&gt;sudo update-alternatives --config editor&lt;/code&gt;</description>
      <pubDate>Tue, 02 Sep 2008 15:40:32 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/6012</guid>
      <author>jrobertson (James Robertson)</author>
    </item>
    <item>
      <title>Convert an MP3 to WAV</title>
      <link>http://snippets.dzone.com/posts/show/5990</link>
      <description>Using Ubuntu you will first need to apt-get install mpg123.&lt;br /&gt;&lt;code&gt;mpg123 -w record1.wav Record1.mp3&lt;/code&gt;&lt;br /&gt;Reference: &lt;a href="http://www.linuxquestions.org/questions/linux-software-2/using-mpg123-to-convert-.mp3-to-.wav-files-332570/"&gt;using mpg123 to convert .mp3 to .wav files&lt;/a&gt; [LinuxQuestions.org]&lt;br /&gt;&lt;br /&gt;See also: &lt;a href="http://snippets.dzone.com/posts/show/5868"&gt;Convert a WAV to OGG&lt;/a&gt; [dzone.com]</description>
      <pubDate>Tue, 26 Aug 2008 17:30:42 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5990</guid>
      <author>jrobertson (James Robertson)</author>
    </item>
    <item>
      <title>Handling conflicting config files after emerging</title>
      <link>http://snippets.dzone.com/posts/show/5966</link>
      <description>When emerging a program on Gentoo it's not uncommon for a configuration conflict to occur when a new config file contains new settings. To summon the program to interactively choose which config files to update type the following:&lt;br /&gt;&lt;code&gt;etc-update&lt;/code&gt;</description>
      <pubDate>Thu, 21 Aug 2008 20:30:32 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5966</guid>
      <author>jrobertson (James Robertson)</author>
    </item>
    <item>
      <title>Putting on your Ruby shoes</title>
      <link>http://snippets.dzone.com/posts/show/5933</link>
      <description>Source: &lt;a href="http://shoooes.net/tutorial/"&gt;Shoes &#8226; The Tutorial Walkthrough&lt;/a&gt; [shoooes.net] via &lt;a href="http://www.rubyinside.com/whys-shoes-grows-up-1014.html"&gt;Shoes - Ruby&#8217;s Cross Platform GUI App Toolkit - Grows Up&lt;/a&gt; [RubyInside.com]&lt;br /&gt;&lt;br /&gt;Installation (Ubuntu):&lt;br /&gt;Prior to installation ensure the following packages are installed:&lt;br /&gt;libcairo2-dev libpixman-1-dev libpango1.0-dev libungif4-dev libjpeg62-dev libgtk2.0-dev vlc libvlc0-dev libsqlite3-dev libcurl4-openssl-dev ruby1.8-dev rake&lt;br /&gt;&lt;br /&gt; - 1) Download the source code from http://shoooes.net/downloads/&lt;br /&gt; - 2) tar zxvf shoes* &lt;br /&gt; - 3) cd shoes*&lt;br /&gt; - 4) make&lt;br /&gt; - 5) sudo make install&lt;br /&gt;&lt;br /&gt;file: firstapp.rb&lt;br /&gt;&lt;code&gt;&lt;br /&gt;Shoes.app {&lt;br /&gt;  @push = button "Push me"&lt;br /&gt;  @note = para "Nothing pushed so far"&lt;br /&gt;&lt;br /&gt;  @push.click {&lt;br /&gt;    @note.replace "Aha! Click!"&lt;br /&gt;  }&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;running the code: type shoes firstapp.rb&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;output: &lt;a href="http://twitxr.com/image/88938/"&gt;firstapp.rb application&lt;/a&gt; [twitxr.com]&lt;br /&gt;&lt;br /&gt;Reference: &lt;a href="http://www.juixe.com/techknow/index.php/2007/08/27/running-with-shoes-a-mini-gui/"&gt;Running with Shoes - A Mini GUI Toolkit&lt;/a&gt; [juixe.com]&lt;br /&gt; - Shoes reference manual (from the command line type shoes -m) &lt;a href="http://twitxr.com/image/88950/"&gt;Shoes Reference Manual screenshot&lt;/a&gt; [twitxr.com]</description>
      <pubDate>Sun, 17 Aug 2008 14:38:27 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5933</guid>
      <author>jrobertson (James Robertson)</author>
    </item>
    <item>
      <title>upgrade python 2.4 to 2.5 on debian etch</title>
      <link>http://snippets.dzone.com/posts/show/5911</link>
      <description>upgrade python 2.4 to 2.5 on debian etch&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;aptitude install python2.5&lt;br /&gt;&lt;br /&gt;# which python&lt;br /&gt;/usr/bin/python&lt;br /&gt;&lt;br /&gt;# which python2.5&lt;br /&gt;/usr/bin/python2.5&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;..create symbolic link from /python to /python2.5 to force debian to default to 2.5&lt;br /&gt;ln -sf /usr/bin/python2.5 /usr/bin/python&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 13 Aug 2008 09:45:21 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5911</guid>
      <author>indiehead (John)</author>
    </item>
    <item>
      <title>Edit WAV files from the command line using SOX</title>
      <link>http://snippets.dzone.com/posts/show/5870</link>
      <description>Source: &lt;a href="http://www.ling.upenn.edu/phonetics/sox.html"&gt;Using SOX&lt;/a&gt; [upenn.edu]&lt;br /&gt;&lt;br /&gt;Converting a raw to wav&lt;br /&gt;&lt;code&gt;sox -r 441000 -s -w foo.raw foo.wav&lt;/code&gt;&lt;br /&gt;Increase the volume&lt;br /&gt;&lt;code&gt;sox -v 2.0 foo.wav bar.wav&lt;/code&gt;&lt;br /&gt;Check the maximum volume before increasing the volume causes clipping&lt;br /&gt;&lt;code&gt;sox foo.wav -e stat -v&lt;/code&gt;&lt;br /&gt;Extract a part of the wav file&lt;br /&gt;&lt;code&gt;sox Input.wav  Half2.wav trim 30:00 60:00&lt;/code&gt;&lt;br /&gt;Synthesizing sound for fun&lt;br /&gt;&lt;code&gt;sox -t nul /dev/null sine.wav synth 1.0 sine  1000.0&lt;/code&gt;&lt;br /&gt;types of sound can be sine, square, triangle, sawtooth, trapetz (trapezoidal), exp (exponential), whitenoise, pinknoise, and brownnoise.&lt;br /&gt;&lt;br /&gt;Combining wav files&lt;br /&gt;&lt;code&gt;soxmix sine100.wav sine250.wav sine100-250.wav&lt;/code&gt;&lt;br /&gt;</description>
      <pubDate>Sun, 03 Aug 2008 23:17:16 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5870</guid>
      <author>jrobertson (James Robertson)</author>
    </item>
    <item>
      <title>Convert a WAV to OGG</title>
      <link>http://snippets.dzone.com/posts/show/5868</link>
      <description>&lt;code&gt;&lt;br /&gt;oggenc -o asterisk-caller.ogg prompt03.wav&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;Note: To install oggenc on Ubuntu: apt-get vorbis-tools</description>
      <pubDate>Sun, 03 Aug 2008 11:55:24 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5868</guid>
      <author>jrobertson (James Robertson)</author>
    </item>
    <item>
      <title>Disk/flash benchmark</title>
      <link>http://snippets.dzone.com/posts/show/5821</link>
      <description>Adapted from a LKML posted program. I think I added write support, this for benchmarking random access writes on flash.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;/* Simple multi-threaded I/O benchmark program.&lt;br /&gt; * Copyright (C) Michael Tokarev, mjt@tls.msk.ru&lt;br /&gt; * Public domain.&lt;br /&gt; *&lt;br /&gt; * To compile:&lt;br /&gt; *   gcc -o iot iot.c -lpthread&lt;br /&gt; * To run:&lt;br /&gt; *   Either with disk device or with pre-existing file.&lt;br /&gt; *    ./iot [options] filename&lt;br /&gt; *   Filename is the file or device to test on.&lt;br /&gt; *   By default it uses 8Kb I/O blocks and does sequential read test&lt;br /&gt; *   until interrupted.&lt;br /&gt; *   To indicate when to stop:&lt;br /&gt; *     -t sec - run for this many seconds, say, 30, to eliminate random&lt;br /&gt; *       noise.&lt;br /&gt; *     -i num - perform this many I/O operations&lt;br /&gt; *   To indicate R/W mode:&lt;br /&gt; *     -wn, -Wn, -rn, -Rn --&lt;br /&gt; *       perform linear or random write (note: all data will be lost!),&lt;br /&gt; *       or linear or random read, using given number of threads (n).&lt;br /&gt; *   I/O modes:&lt;br /&gt; *    -s - syncronous write (O_SYNC)&lt;br /&gt; *    -d - direct I/O (O_DIRECT)&lt;br /&gt; *    -b bs - block size in bytes&lt;br /&gt; *   And finally:&lt;br /&gt; *    -h - display usage.&lt;br /&gt; * Example:&lt;br /&gt; *   ./iot -t30 -W4 -R4 -d -b8192 /dev/sdb&lt;br /&gt; * perform random read/write test (4 readers and 4 writers)&lt;br /&gt; * for 30 seconds using direct I/O and block size of 8Kb.&lt;br /&gt; *&lt;br /&gt; * Note: for small blocksize (&lt;64Kb at least) and using direct random I/O,&lt;br /&gt; * nowadays drives sometimes gives transfer rates below 1Mb/sec - this is&lt;br /&gt; * expectable, don't be afraid of so low numbers.  The reason is simple:&lt;br /&gt; * in order to access a given block of data, a disk drive has to seek to the&lt;br /&gt; * right track (average seek time) and wait for the right sector to be near&lt;br /&gt; * the head (rotation latency).  Sum up the two, and divide 1 sec to the&lt;br /&gt; * result -- you'll have max number of requests/sec a drive can perform,&lt;br /&gt; * not counting the actual data transfer (which reduces this number further).&lt;br /&gt; * With, say, 5ms seek time + rotation latency, we'll have 200 requests/sec,&lt;br /&gt; * which, with 4Kb request size, will be about 800Kb/sec - which is below&lt;br /&gt; * 1Mb/sec, not counting the actual transfer...&lt;br /&gt; *&lt;br /&gt; */&lt;br /&gt;&lt;br /&gt;#define _GNU_SOURCE&lt;br /&gt;#define _BSD_SOURCE&lt;br /&gt;#define _LARGEFILE_SOURCE&lt;br /&gt;#define _FILE_OFFSET_BITS 64&lt;br /&gt;&lt;br /&gt;//#define USE_DEV_URANDOM       /* was not a good idea */&lt;br /&gt;&lt;br /&gt;#include &lt;sys/types.h&gt;&lt;br /&gt;#include &lt;unistd.h&gt;&lt;br /&gt;#include &lt;fcntl.h&gt;&lt;br /&gt;#include &lt;errno.h&gt;&lt;br /&gt;#include &lt;stdlib.h&gt;&lt;br /&gt;#include &lt;stdio.h&gt;&lt;br /&gt;#include &lt;sys/time.h&gt;&lt;br /&gt;#include &lt;sys/ioctl.h&gt;&lt;br /&gt;#include &lt;signal.h&gt;&lt;br /&gt;#include &lt;pthread.h&gt;&lt;br /&gt;#include &lt;string.h&gt;&lt;br /&gt;&lt;br /&gt;#ifndef BLKGETSIZE64&lt;br /&gt;#define BLKGETSIZE64 _IOR(0x12,114,size_t)&lt;br /&gt; /* linux-specific. return device size in bytes (u64 *arg) */&lt;br /&gt;#endif&lt;br /&gt;&lt;br /&gt;static void edie(const char *what) {&lt;br /&gt;  fprintf(stderr, "%s: %m \n", what);&lt;br /&gt;  exit(1);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;#ifdef USE_DEV_URANDOM&lt;br /&gt;static int randfd;&lt;br /&gt;#endif&lt;br /&gt;static int oflags;              // open flags&lt;br /&gt;static char *fn;                // filename&lt;br /&gt;static unsigned bs = 8192;      // block size&lt;br /&gt;static unsigned bc;             // block count (device size in blocks)&lt;br /&gt;static unsigned bm;             // blocks to do&lt;br /&gt;&lt;br /&gt;#define MFrnd   1&lt;br /&gt;#define MFwrt   2&lt;br /&gt;#define LinRd   0&lt;br /&gt;#define RndRd   MFrnd&lt;br /&gt;#define LinWr   MFwrt&lt;br /&gt;#define RndWr   (MFrnd|MFwrt)&lt;br /&gt;&lt;br /&gt;struct state {&lt;br /&gt;  int fd;&lt;br /&gt;  char *buf;&lt;br /&gt;  unsigned ioc;         // I/O count&lt;br /&gt;  int (*workfn)(struct state *, unsigned blocknr);&lt;br /&gt;  unsigned (*posfn)(struct state *s);&lt;br /&gt;  unsigned opi;         // operation index&lt;br /&gt;  unsigned i;           // curidx&lt;br /&gt;  double stime;         // start time&lt;br /&gt;  unsigned bn;          // current block number for linear i/o&lt;br /&gt;};&lt;br /&gt;static unsigned int alternate;&lt;br /&gt;static unsigned tioc;   // total i/o count&lt;br /&gt;static struct state *states;&lt;br /&gt;static unsigned nt[4];&lt;br /&gt;static unsigned ntt;&lt;br /&gt;static volatile unsigned running;&lt;br /&gt;static const char *const ion[4] = { "LinRd", "RndRd", "LinWr", "RndWr" };&lt;br /&gt;&lt;br /&gt;static pthread_mutex_t rnmtx = PTHREAD_MUTEX_INITIALIZER;&lt;br /&gt;static pthread_cond_t rncond = PTHREAD_COND_INITIALIZER;&lt;br /&gt;&lt;br /&gt;static double curtime(void) {&lt;br /&gt;  struct timeval tv;&lt;br /&gt;  gettimeofday(&amp;tv, NULL);&lt;br /&gt;  return tv.tv_sec + tv.tv_usec / 1000000.0;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;static unsigned randpos(struct state *s) {&lt;br /&gt;  unsigned n;&lt;br /&gt;#ifdef USE_DEV_URANDOM&lt;br /&gt;  read(randfd, &amp;n, sizeof(n));&lt;br /&gt;#else&lt;br /&gt;  n = lrand48();&lt;br /&gt;#endif&lt;br /&gt;  s = s;&lt;br /&gt;  if(alternate == 1) {&lt;br /&gt;    if(n &gt; bc/2) {&lt;br /&gt;      return bc-1;&lt;br /&gt;    } else {&lt;br /&gt;      return 0;&lt;br /&gt;    }&lt;br /&gt;  }&lt;br /&gt;  return n % bc;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;static unsigned linpos(struct state *s) {&lt;br /&gt;  if (s-&gt;bn &gt;= bc)&lt;br /&gt;    s-&gt;bn = 0;&lt;br /&gt;  return s-&gt;bn++;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;static int wwriter(struct state *s, unsigned b) {&lt;br /&gt;  return pwrite(s-&gt;fd, s-&gt;buf, bs, (off_t)b * bs);&lt;br /&gt;}&lt;br /&gt;static int wreader(struct state *s, unsigned b) {&lt;br /&gt;  return pread(s-&gt;fd, s-&gt;buf, bs, (off_t)b * bs);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;static void pst(FILE *f) {&lt;br /&gt;  double ct = curtime();&lt;br /&gt;  double r[4] = { 0, 0, 0, 0 };&lt;br /&gt;  unsigned c[4] = { 0, 0, 0, 0 };&lt;br /&gt;  unsigned i;&lt;br /&gt;  double d;&lt;br /&gt;  for(i = 0; i &lt; ntt; ++i) {&lt;br /&gt;    d = ct - states[i].stime;&lt;br /&gt;    r[states[i].opi] += states[i].ioc / d;&lt;br /&gt;    c[states[i].opi] += states[i].ioc;&lt;br /&gt;  }&lt;br /&gt;#if 1&lt;br /&gt;  for(i = 0; i &lt; 4; ++i)&lt;br /&gt;    if (c[i])&lt;br /&gt;      fprintf(f, " %s %u %.2f", ion[i], c[i], r[i] * bs / 1024 / 1024);&lt;br /&gt;#endif&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;static void incc() {&lt;br /&gt;  if (!(++tioc % 1000))&lt;br /&gt;    pthread_cond_signal(&amp;rncond);&lt;br /&gt;}&lt;br /&gt;static void decnr() {&lt;br /&gt;  pthread_mutex_lock(&amp;rnmtx);&lt;br /&gt;  --running;&lt;br /&gt;  pthread_mutex_unlock(&amp;rnmtx);&lt;br /&gt;  pthread_cond_broadcast(&amp;rncond);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;static volatile int term;&lt;br /&gt;&lt;br /&gt;void *worker(void *arg) {&lt;br /&gt;  struct state *s = arg;&lt;br /&gt;  s-&gt;workfn = s-&gt;opi &amp; MFwrt ? wwriter : wreader;&lt;br /&gt;  s-&gt;posfn  = s-&gt;opi &amp; MFrnd ? randpos : linpos;&lt;br /&gt;  s-&gt;fd = open(fn, (s-&gt;opi &amp; MFwrt ? O_WRONLY : O_RDONLY) | oflags);&lt;br /&gt;  if (s-&gt;fd &lt; 0) {&lt;br /&gt;    int e = errno;&lt;br /&gt;    decnr();&lt;br /&gt;    errno = e;&lt;br /&gt;    edie(fn);&lt;br /&gt;  }&lt;br /&gt;  s-&gt;stime = curtime();&lt;br /&gt;  for(;;) {&lt;br /&gt;    if (term) break;&lt;br /&gt;    if (s-&gt;workfn(s, s-&gt;posfn(s)) &lt; 0) {&lt;br /&gt;      perror(ion[s-&gt;opi]);&lt;br /&gt;      break;&lt;br /&gt;    }&lt;br /&gt;    ++s-&gt;ioc;&lt;br /&gt;    incc();&lt;br /&gt;    if (bm &amp;&amp; s-&gt;ioc &gt;= bm) break;&lt;br /&gt;  }&lt;br /&gt;  decnr();&lt;br /&gt;  return 0;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;static void sig(int s) {&lt;br /&gt;  term = s;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;int main(int argc, char **argv) {&lt;br /&gt;  int c;&lt;br /&gt;  unsigned i, j;&lt;br /&gt;  unsigned tm = 0;&lt;br /&gt;  struct state *s;&lt;br /&gt;  char *buf;&lt;br /&gt;  struct timeval  first,&lt;br /&gt;                second,&lt;br /&gt;                lapsed;&lt;br /&gt;&lt;br /&gt;  while((c = getopt(argc, argv, "r::R::w::W::adsb:n:i:t:h")) != EOF) switch(c) {&lt;br /&gt;  case 'r': nt[LinRd] = optarg ? atoi(optarg) : 1; break;&lt;br /&gt;  case 'R': nt[RndRd] = optarg ? atoi(optarg) : 1; break;&lt;br /&gt;  case 'w': nt[LinWr] = optarg ? atoi(optarg) : 1; break;&lt;br /&gt;  case 'W': nt[RndWr] = optarg ? atoi(optarg) : 1; break;&lt;br /&gt;  case 'd': oflags |= O_DIRECT; break;&lt;br /&gt;  case 's': oflags |= O_SYNC; break;&lt;br /&gt;  case 'b': bs = atoi(optarg); break;&lt;br /&gt;  case 'n': bc = atoi(optarg); break;&lt;br /&gt;  case 'i': bm = atoi(optarg); break;&lt;br /&gt;  case 't': tm = atoi(optarg); break;&lt;br /&gt;  case 'a': alternate = 1; break;&lt;br /&gt;  case 'h':&lt;br /&gt;    puts(&lt;br /&gt;"iotest: perform I/O speed test\n"&lt;br /&gt;"Usage is: iotest [options] device-or-file\n"&lt;br /&gt;"options:\n"&lt;br /&gt;" -r[n] - linear read test (n readers)\n"&lt;br /&gt;" -R[n] - random read test (n readers)\n"&lt;br /&gt;" -w[n] - linear write test (n writers)\n"&lt;br /&gt;" -W[n] - random write test (n writers)\n"&lt;br /&gt;" -d - use direct I/O (O_DIRECT)\n"&lt;br /&gt;" -s - use syncronous I/O (O_SYNC)\n"&lt;br /&gt;" -b bs - blocksize (default is 8192)\n"&lt;br /&gt;" -n bc - block count (default is whole device/file)\n"&lt;br /&gt;" -i nb - number of I/O iterations to perform\n"&lt;br /&gt;" -t sec - time to spend on all I/O\n"&lt;br /&gt;" -h - this help\n"&lt;br /&gt;"It's ok to specify all, one or some of -r,-R,-w and -W\n"&lt;br /&gt;);&lt;br /&gt;    return 0;&lt;br /&gt;  default: fprintf(stderr, "try `iotest -h' for help\n"); exit(1);&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  if (optind + 1 != argc) {&lt;br /&gt;    fprintf(stderr, "exactly one device/file argument expected\n");&lt;br /&gt;    return 1;&lt;br /&gt;  }&lt;br /&gt;  fn = argv[optind];&lt;br /&gt;&lt;br /&gt;  ntt = nt[0] + nt[1] + nt[2] + nt[3];&lt;br /&gt;  if (!ntt)&lt;br /&gt;    nt[LinRd] = ntt = 1;&lt;br /&gt;&lt;br /&gt;  c = open(fn, (nt[LinWr] + nt[RndWr] ? O_RDWR : O_RDONLY) | oflags);&lt;br /&gt;  if (c &lt; 0) edie(fn);&lt;br /&gt;  if (!bc) {&lt;br /&gt;    unsigned long long sz;&lt;br /&gt;    struct stat st;&lt;br /&gt;    fstat(c, &amp;st);&lt;br /&gt;    if (st.st_size) sz = st.st_size;&lt;br /&gt;    else ioctl(c, BLKGETSIZE64, &amp;sz);&lt;br /&gt;    bc = sz / bs;&lt;br /&gt;    fprintf(stderr, "size = %lld (%u blocks)\n", sz, bc);&lt;br /&gt;  }&lt;br /&gt;  close(c);&lt;br /&gt;  if (nt[RndRd] || nt[RndWr]) {&lt;br /&gt;#ifdef USE_DEV_URANDOM&lt;br /&gt;    randfd = open("/dev/urandom", O_RDONLY);&lt;br /&gt;    if (randfd &lt; 0) edie("/dev/urandom");&lt;br /&gt;#else&lt;br /&gt;#if 0&lt;br /&gt;    struct timeval tv;&lt;br /&gt;    gettimeofday(&amp;tv, NULL);&lt;br /&gt;    srand48(tv.tv_usec ^ getpid());&lt;br /&gt;#else&lt;br /&gt;    srand48(0xfeda432); // arbitrary, to get repeated values on repeated runs&lt;br /&gt;#endif&lt;br /&gt;#endif&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  gettimeofday(&amp;first, NULL);&lt;br /&gt;&lt;br /&gt;  states = calloc(ntt, sizeof(*states));&lt;br /&gt;  s = states;&lt;br /&gt;&lt;br /&gt;  buf = valloc(ntt * bs);&lt;br /&gt;  if (tm) {&lt;br /&gt;    signal(SIGALRM, sig);&lt;br /&gt;    alarm(tm);&lt;br /&gt;  }&lt;br /&gt;  running = ntt;&lt;br /&gt;  for(j = 0; j &lt; 4; ++j)&lt;br /&gt;    for(i = 0; i &lt; nt[j]; ++i) {&lt;br /&gt;      pthread_t t;&lt;br /&gt;      s-&gt;buf = buf; buf += bs;&lt;br /&gt;      s-&gt;opi = j;&lt;br /&gt;      s-&gt;i = i;&lt;br /&gt;      pthread_create(&amp;t, NULL, worker, s++);&lt;br /&gt;    }&lt;br /&gt;  while(running) {&lt;br /&gt;    pthread_cond_wait(&amp;rncond, &amp;rnmtx);&lt;br /&gt;    putc('\r', stderr);&lt;br /&gt;    pst(stderr);&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  putc('\r', stderr);&lt;br /&gt;  pst(stdout);&lt;br /&gt;  putc('\n', stdout);&lt;br /&gt;&lt;br /&gt;  gettimeofday(&amp;second, NULL);&lt;br /&gt;&lt;br /&gt;  if (first.tv_usec &gt; second.tv_usec) {&lt;br /&gt;    second.tv_usec += 1000000;&lt;br /&gt;    second.tv_sec--;&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  lapsed.tv_usec = second.tv_usec - first.tv_usec;&lt;br /&gt;  lapsed.tv_sec  = second.tv_sec  - first.tv_sec;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;  if (nt[RndRd] || nt[RndWr]) {&lt;br /&gt;        printf("I/O latency: %f\n", (lapsed.tv_sec*1000+(lapsed.tv_usec/1000))/(float)tioc);&lt;br /&gt;&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  return 0;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 24 Jul 2008 16:50:04 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5821</guid>
      <author>sandos (John B&#228;ckstrand)</author>
    </item>
    <item>
      <title>Listing the files and subdirectories in C - Linux</title>
      <link>http://snippets.dzone.com/posts/show/5734</link>
      <description>// program lists the files and subdirectories within a given directory in full path&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;#include &lt;stdio.h&gt;&lt;br /&gt;#include &lt;stdlib.h&gt;&lt;br /&gt;#include &lt;string.h&gt;&lt;br /&gt;#include &lt;dirent.h&gt;&lt;br /&gt;&lt;br /&gt;char *path_cat (const char *str1, char *str2);&lt;br /&gt;&lt;br /&gt;int main () {&lt;br /&gt;	struct dirent *dp;&lt;br /&gt;&lt;br /&gt;        // enter existing path to directory below&lt;br /&gt;	const char *dir_path="/path/to/directory/to/list";&lt;br /&gt;	DIR *dir = opendir(dir_path);&lt;br /&gt;	while ((dp=readdir(dir)) != NULL) {&lt;br /&gt;		char *tmp;&lt;br /&gt;		tmp = path_cat(dir_path, dp-&gt;d_name);&lt;br /&gt;		printf("%s\n", tmp);&lt;br /&gt;		free(tmp);&lt;br /&gt;		tmp=NULL;&lt;br /&gt;	}&lt;br /&gt;	closedir(dir);&lt;br /&gt;	return 0;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;char *path_cat (const char *str1, char *str2) {&lt;br /&gt;	size_t str1_len = strlen(str1);&lt;br /&gt;	size_t str2_len = strlen(str2);&lt;br /&gt;	char *result;&lt;br /&gt;	result = malloc((str1_len+str2_len+1)*sizeof *result);&lt;br /&gt;	strcpy (result,str1);&lt;br /&gt;	int i,j;&lt;br /&gt;	for(i=str1_len, j=0; ((i&lt;(str1_len+str2_len)) &amp;&amp; (j&lt;str2_len));i++, j++) {&lt;br /&gt;		result[i]=str2[j];&lt;br /&gt;	}&lt;br /&gt;	result[str1_len+str2_len]='\0';&lt;br /&gt;	return result;&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 08 Jul 2008 01:13:50 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5734</guid>
      <author>Tvrtko (Tvrtko)</author>
    </item>
    <item>
      <title>Removing Files of Size Zero</title>
      <link>http://snippets.dzone.com/posts/show/5680</link>
      <description>// description of your code here&lt;br /&gt;This will delete all files in the current directory with size == 0.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;find . -type f -size 0k -exec rm {} \; | awk '{ print $8 }'&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Mon, 23 Jun 2008 09:00:34 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5680</guid>
      <author>k1mmeh (Kim Chirnside)</author>
    </item>
  </channel>
</rss>
