943,096 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 1953
  • C RSS
Mar 19th, 2010
0

CPU Usage for my little program

Expand Post »
Hi guy, i have this little program:

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include <string.h>
  5. #include <unistd.h>
  6.  
  7. //Obtem o nome do buffer de trade baseado na hora
  8.  
  9. char *btfile() {
  10. char *fname;
  11.  
  12. fname = malloc(14);
  13.  
  14. time_t t = time(NULL);
  15. struct tm *lt;
  16.  
  17. lt = localtime(&t);
  18.  
  19. strftime(fname, 14, "%m%d%H%M.tbf", lt);
  20.  
  21. return fname;
  22.  
  23. }
  24.  
  25. /*
  26.  *
  27.  */
  28. int main(int argc, char** argv) {
  29.  
  30. //Caminho dos arquivos
  31. char *fpath;
  32. fpath = malloc(17);
  33. strcpy(fpath, "/home/donda/ddc/");
  34.  
  35.  
  36. //Arquivo de buffer
  37. FILE *fbf;
  38.  
  39. //Buffer de leitura
  40. char *b;
  41.  
  42. //Caminho completo
  43. char *ffull;
  44.  
  45. //Aloca buffer de leitura na memoria
  46. b = malloc(500);
  47.  
  48. //Aloca destino completo
  49. ffull = malloc(26);
  50.  
  51. //Gera caminho
  52. strcpy(ffull, fpath);
  53. strcat(ffull, btfile());
  54.  
  55. //Abre Arquivo
  56. fbf = fopen(ffull, "r");
  57.  
  58. //Verifica se abriu
  59. if (!fbf) {
  60. //Forca retorno ao inicio
  61. exit(1);
  62. }
  63.  
  64. while(fgets(b,500,fbf) != NULL){
  65.  
  66. printf("%s",b);
  67.  
  68. bzero(b,500);
  69. }
  70.  
  71. return (EXIT_SUCCESS);
  72. }

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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
dondajr is offline Offline
33 posts
since Jan 2010
Mar 19th, 2010
0
Re: CPU Usage for my little program
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.
Reputation Points: 416
Solved Threads: 181
Nearly a Posting Virtuoso
Adak is offline Offline
1,463 posts
since Jun 2008
Mar 19th, 2010
0
Re: CPU Usage for my little program
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.
Reputation Points: 10
Solved Threads: 0
Light Poster
dondajr is offline Offline
33 posts
since Jan 2010
Mar 19th, 2010
1
Re: CPU Usage for my little program
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.
Reputation Points: 445
Solved Threads: 72
Posting Pro
Banfa is offline Offline
506 posts
since Mar 2010
Mar 19th, 2010
0
Re: CPU Usage for my little program
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.
Reputation Points: 10
Solved Threads: 0
Light Poster
dondajr is offline Offline
33 posts
since Jan 2010
Mar 25th, 2010
1

efficiency, overflow

The "bzero" in the loop is redundant and can be deleted.
It is also overflowing the allocated buffer size of "ffull".
Reputation Points: 68
Solved Threads: 6
Junior Poster in Training
UncleLeroy is offline Offline
56 posts
since Mar 2010
Mar 25th, 2010
0
Re: CPU Usage for my little program
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.


.
Last edited by jephthah; Mar 25th, 2010 at 5:39 pm. Reason: peas in the porridge five days old
Reputation Points: 2143
Solved Threads: 178
Posting Maven
jephthah is offline Offline
2,567 posts
since Feb 2008
Mar 26th, 2010
0
Re: CPU Usage for my little program
thanks for all
Reputation Points: 10
Solved Threads: 0
Light Poster
dondajr is offline Offline
33 posts
since Jan 2010

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: C in Vista
Next Thread in C Forum Timeline: Encrypting





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC