How to monitor a C code on Linux

Asked

Viewed 199 times

2

I was trying to use top to monitor the performance of my codes in c, but when I run the program does not appear. Is it just not visible, or can’t monitor code in c using the top? What should I use to monitor if the top doesn’t work? The code is this:

#include <unistd.h> 
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/time.h>
#include <sys/stat.h> 

int main(){
    struct stat info; 
    pid_t id, filho;
    struct timeval tv1, tv2;
    double t1, t2;

    id = getpid();
    printf("Processo %ld \n", (long)id);

    gettimeofday(&tv1, NULL);
    lstat("teste", &info);
    gettimeofday(&tv2, NULL);

    t1 = (double)(tv1.tv_sec) + (double)(tv1.tv_usec)/ 1000000.00;
    t2 = (double)(tv2.tv_sec) + (double)(tv2.tv_usec)/ 1000000.00;
    printf("\nO tempo de execucao foi %lf\n", (t2 - t1));

    _exit(0);
}

I use gcc -o dados dados.c to compile the file. And ./dados to run in the terminal. In the code itself it shows the pid of it and when I go to search in ps, I do not find.

  • The program runs very fast to appear in the top list. When you run _Exit(0) at the end the PID dies, it will no longer appear in ps or top...

  • You have some tips on how to finish the program, so?

  • I don’t know exactly what you want to monitor, but suddenly this lib can help you: man getrusage NAME getrusage -- get information about Resource Utilization SYNOPSIS #include <sys/Resource. h> #define RUSAGE_SELF 0 #define RUSAGE_CHILDREN -1

  • I’m monitoring runtime, memory usage and processing. What you gave me just does not return processing, but it is still very helpful. If you know any q do this would be very good indeed. Thank you :)

  • You’re welcome. Let me ask you. What do you define by "processing"? If the CPU percentage is used, just divide the user time by the real time.

  • That’s right. These team'I can only use the command team?

  • I waver my. What you’ve been through already does it. Thanks!

  • I need to substantiate this test and I looked around here and I couldn’t find anything that says that the percentage of CPU is that calculation that you gave me. Where can I find this? Thanks in advance, now!

  • I found this article very interesting (in English): http://www.linuxjournal.com/article/9001 .

Show 4 more comments

2 answers

2

1- You can use the top to monitor only one PID. Discover the program PID as the colleague suggested:

ps -aucx|grep <nome do programa>

I like to use the command below as it only lists children SHELL commands that I am using, so if you ran the program from the same SHELL, you can easily discover the PID of it.:

ps -T

And for the top command use with the -p parameter:

top -p <PID>

Below are some commands that can be used within the top interface:

       Global_defaults
          ’A’ - Alt display      Off (full-screen)
        * ’d’ - Delay time       3.0 seconds
          ’I’ - Irix mode        On  (no, ’solaris’ smp)
        * ’p’ - PID monitoring   Off
        * ’s’ - Secure mode      Off (unsecured)
          ’B’ - Bold disable     Off
       Summary_Area_defaults
          ’l’ - Load Avg/Uptime  On  (thus program name)
          ’t’ - Task/Cpu states  On  (1+1 lines, see ’1’)
          ’m’ - Mem/Swap usage   On  (2 lines worth)
          ’1’ - Single Cpu       On  (thus 1 line if smp)
       Task_Area_defaults
          ’b’ - Bold hilite      On  (not ’reverse’)
        * ’c’ - Command line     Off (name, not cmdline)
        * ’H’ - Threads          Off (show all threads)
        * ’i’ - Idle tasks       On  (show all tasks)
          ’R’ - Reverse sort     On  (pids high-to-low)
        * ’S’ - Cumulative time  Off (no, dead children)
          ’x’ - Column hilite    Off (no, sort field)
          ’y’ - Row hilite       On  (yes, running tasks)
          ’z’ - color/mono       Off (no, colors)

If the top shows that your program is consuming 0% CPU, use other means to know what the process is doing.

2- If you want to see files opened by your process: (name or pid)

lsof -c httpd
lsof -p <PID>

3- Processes interacting with a directory or file (for example, your directory where you run binary)

lsof /home/user1/bin

4 - List all system calls of a process

strace -p <PID>
  • I’m running the code exactly to analyze the system calls. In it, I can call the pid of the code itself, but I can’t monitor it. I gave an update on the question.

0

You can use the commands @cemdorst specified and run it as if it were in the terminal via the popen function.

Here is an example for the PID 1234 process:

int main() {
    FILE *stream= popen("top -p 1234", "r");

    if (stream != NULL) {
        int c;

        while ((c = getc(stream)) != EOF)
            printf("%c", c);

        pclose(stream);
    }

}
  • Did it work out for you? Did it?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.