-5

I have the following code:

#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <unistd.h>

int main()
{
  time_t result;
  char   hostname[10] = "ws45new";
  char   graphite[10] = "127.0.0.1";
  char   buf[1024];
  int    n = 0;
  int    num = 0;
  int    graphiteport = 2003;

  while (1 == 1)
  {
    result = time(NULL);
    n = n + 1;
    if (n >= 100)
      n = 0;
    printf("\n %d", n);
    // snprintf(buf, sizeof(buf), "echo \"stats.ws45new.test %d %d \" | nc 127.0.0.1 2003", n, result);
    num = sprintf(buf, "echo \"stats.ws45new1.test %d %d \" | nc 127.0.0.1 2003", n, result);
    system(buf);
    printf("\n %s", buf);
    sleep(2);
  }
  return 0;
}

This is supposed to generate something like a sawtooth signal, which is only for testing. My problem is that when i run the program it doesn't do anything.

If i hit Ctrl-C to stop it, the program iterates once through the while loop. It is supposed to send some statistics to a graphite server and I'm using it to understand how graphite is working. From what i've seen it has something to do with the concatenation function sprintf (snprintf also acts the same way).

If I comment out that line it works correctly and generates the numbers I want (and also the epoch), but i need the concatenation to be able to send the required dynamic info to graphite.

If anyone has any idea why these concatenation functions don't respect the while function please let me know as I'm really curious. Also I'm open to other suggestions but I'd rather not go in to deep, like create sockets and not use nc and system, because i'm not that good with the C language.

Eregrith
  • 3,970
  • 16
  • 38
primero
  • 581
  • 1
  • 6
  • 17
  • @Let_Me_Be you're right, snprintf is the function with that argument ;) – Evert Aug 28 '12 at 10:25
  • 2
    Use shell scripting instead of trying to write something from scratch. – auselen Aug 28 '12 at 10:27
  • I got the same behavior using bash scripting. Actually this was rewritten from a bash script. – primero Aug 28 '12 at 10:35
  • @auselen if you don't want to help just refrain from commenting. I did it in C for learning purposes, because I really want learn the language. – primero Aug 28 '12 at 10:39
  • @bitmask I would like to thank you for pointing the duplicate, but this is not the same case. My problem was that the system function never terminated, that's why it hangs. – primero Aug 28 '12 at 11:18

1 Answers1

2

The program most likely gets stuck on the system call.

You are printing out text without the trailing newline, so it will be kept in the buffer, until a newline is printed, which you do after the system call.

Šimon Tóth
  • 33,420
  • 18
  • 94
  • 135
  • So should i just add `\n` at the end of the string formed with sprintf? – primero Aug 28 '12 at 10:37
  • @primero No. If you use `printf("\n %d",n);` It will just print an empty line. The number will be printed when the next `'\n'` is put into the buffer. – Šimon Tóth Aug 28 '12 at 10:40
  • i can comment out the two printfs out because they were only for testing purposes, but the program still hangs. Should i empty the buffer somehow? – primero Aug 28 '12 at 10:55
  • @primero Then you program hangs on the `system` call. Have you actually tested the command? You can run your program under `strace` and `ltrace` to see where exactly it gets stuck. – Šimon Tóth Aug 28 '12 at 10:57
  • you were right, I forgot about strace. It's stuck on a wait4. Aftre a SIGINT(ctrl C) it does it's job and then get's stuck again. Thanks man you were really helpful. Now to trouble shoot further. – primero Aug 28 '12 at 11:04