Hi guy, i have this little program:

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

//Obtem o nome do buffer de trade baseado na hora

char *btfile() {
    char *fname;

    fname = malloc(14);

    time_t t = time(NULL);
    struct tm *lt;

    lt = localtime(&t);

    strftime(fname, 14, "%m%d%H%M.tbf", lt);

    return fname;

}

/*
 * 
 */
int main(int argc, char** argv) {

    //Caminho dos arquivos
    char *fpath;
    fpath = malloc(17);
    strcpy(fpath, "/home/donda/ddc/");


    //Arquivo de buffer
    FILE *fbf;

    //Buffer de leitura
    char *b;

    //Caminho completo
    char *ffull;

    //Aloca buffer de leitura na memoria
    b = malloc(500);

    //Aloca destino completo
    ffull = malloc(26);

    //Gera caminho
    strcpy(ffull, fpath);
    strcat(ffull, btfile());

    //Abre Arquivo
    fbf = fopen(ffull, "r");

    //Verifica se abriu
    if (!fbf) {
        //Forca retorno ao inicio
        exit(1);
    }

    while(fgets(b,500,fbf) != NULL){

        printf("%s",b);

        bzero(b,500);
    }

    return (EXIT_SUCCESS);
}

When I run this program, my cpu usage jump from 1% to 70%.
I'm worry with this big usage just for a little program, anyone have some advice?

Thanks

The more your program does, and the more efficiently it does it, the higher the percentage of cpu utilitization will be.

That's not a bad thing. Sometimes just a few lines of code will generate 100's of lines of assembly or machine code.

If your program is starting to have longer run-times, and still has high cpu utilitization, then you have a worry to be checked out.

My worry is because this program will be read many many files.

If you see, the function btfile return the name of the file, so can be have file for each minute.

And each files have more than 1000 lines.

So, what can I do? if this program will be usage 70% of my machine, i will need to think another way to write and read all this datas.

Firstly if cpu utilisation is only getting to 70% there is nothing to worry about. All programs are getting all the cpu time they require with time (30% of the available time) left over.

A single run of your program however only opens a single file and reads it, if you are talking about multiple files then you must be running the program multiple times. It is the operating systems responsibility to share processor time between process that need it even if some of those processes are demanding 100% of the cpu time the operating system will on the whole not them them have it.

If you are still worried then are are things you can do but they will slow down the operation of the program.

You could run the program at a lower priority. The priority is an attribute that the operating system uses when scheduling tasks, lower priority programs get less processor time. You can change the priority either programatically by making approriate function calls or by using a utility like nice (try "man nice") to run the program with a reduced priority.

You could interrupt the read loop in the program with a yield or a small sleep.

However I would recomend that you do neither of these until you get to the point where you computer is getting problems because it is running out of processing resource which is isn't currently, it still has 30% of that resource left.

Yeah, i tried to put sleep function, and then the cpu usage were about
2%.

But, as you said, it's a low way and I realy need that the read is fast.

I'll do this: I'll let the program running all day long and watch how will his the performance.

If the performance be considerable, and don't have any problem about
overlow because of the cpu usage, i won't worry. But if the o.s stay low
because of the program, i will need to study another way.

The "bzero" in the loop is redundant and can be deleted.
It is also overflowing the allocated buffer size of "ffull".

EDIT : oops, i didnt realize this this thread was 5 days old.

nothing wrong with 70%.... heavy reads in a while loop,that would be expected. if other programs need (or are already using) cpu resources while this program is running, they will share the available resources.

and yes, get rid of the bzero. it's unnecessary. the fgets will null terminate the buffer after each read.


.

thanks for all

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.