How to monitor a C code in Linux

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 there just not visible, or can not monitor c-codes using the top? What should I use to monitor if the top does not 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. E ./dados to run on terminal. In the code itself it shows the pid of it and when I go looking in ps, I can not find.

    
asked by anonymous 02.06.2015 / 13:35

2 answers

2

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

ps -aucx|grep <nome do programa>

I like to use the command below, since I only list SHELL child commands that I'm using, so if you ran the program from the same SHELL, you can easily find out the PID of it.

ps -T

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

top -p <PID>

Below are some commands that you can use 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 find out 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 the binary is running)

lsof /home/user1/bin

4 - List all system calls to a process

strace -p <PID>
    
02.06.2015 / 15:29
0

You can use the commands that @cemdorst indicated and execute it as if you were in the terminal through the popen function.

Here is an example for the process with PID 1234:

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

    if (stream != NULL) {
        int c;

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

        pclose(stream);
    }

}
    
30.09.2017 / 21:29