Operating System - HP-UX
1830340 Members
2468 Online
110001 Solutions
New Discussion

High CPU usage due to getch() curses fn.

 
CD Ajaykumar
Occasional Contributor

High CPU usage due to getch() curses fn.

Hi All,

We have found that our application process in C/Sybase, at times takes very high CPU.
From detailed analyis it was observed that this is due to a curses function, getch(), used within a do.. while loop.

The getch() function waits for user input and if it is not given, approximately after 2hrs, it starts utilizing high cpu time.

However,this doesn't prevent other processes from getting CPU time, if they need .

Why getch() consumes 99% CPU time? How to get rid of this problem? Is there any way the process can come out after trying for some time?

Thanks
Ajay
1 REPLY 1
Paddy_1
Valued Contributor

Re: High CPU usage due to getch() curses fn.

The problem is getch is a blocking I/O call
That is it blocks till it gets a input.Instead use "getc" which is unbuffered.
that is write something like
int getch(void)
{
/*
* fetches the next char (if any) hit by the user
*/

return(getc(stdin));
}
in your program to unbuffer it.
Better still would be to use alternatives like
polling or signal driven input in your application.
Hope this helps
The sufficiency of my merit is to know that my merit is NOT sufficient