当前位置: 技术问答>linux和unix
请大家看看这段串口代码在运行时占用100% Cpu资源的问题!
来源: 互联网 发布时间:2015-09-22
本文导语: 代码如下,该代码因为比较长,所以简化了一下,只帖出关键部分。该程序在Linux下已经编译通过并可以运行,经验证接收到的数据也正确,但是占用系统资源为100%,请问能优化一下吗?我还要在接收数据的同时处...
代码如下,该代码因为比较长,所以简化了一下,只帖出关键部分。该程序在Linux下已经编译通过并可以运行,经验证接收到的数据也正确,但是占用系统资源为100%,请问能优化一下吗?我还要在接收数据的同时处理数据。
/* Open serial port ComPort at baudrate baud rate. */
INT32 OpenComPort (INT32 ComPort, INT32 baudrate, INT32 databit,const char *stopbit, char parity)
{
}
/* close serial port by use of file descriptor fd */
void CloseComPort ()
{
/* flush output data before close and restore old attribute */
tcsetattr (fd, TCSADRAIN, &termios_old);
close (fd);
}
INT32 ReadComPort (void *data, INT32 datalength)
{
INT32 retval = 0;
FD_ZERO (&fs_read);
FD_SET (fd, &fs_read);
tv_timeout.tv_sec = TIMEOUT_SEC (datalength, GetBaudrate ());
tv_timeout.tv_usec = TIMEOUT_USEC;
retval = select (fd + 1, &fs_read, NULL, NULL, &tv_timeout);
if (retval) {
return (read (fd, data, datalength));
}
else
return (-1);
}
/* send file const char *pathname */
INT32 SendFile ()
{
while (1) {
SetPortSig ();
} /* end of while (1) */
fflush (stdout);
fflush (stderr);
close (fd);
return (0);
}
static void signal_handler_IO (int status)
{
char recvbuf[CHAR_MAX];
int iReadCount = 0;
iReadCount = ReadComPort (recvbuf, sizeof (recvbuf));
printf ("nRead = %d", iReadCount);
static int ii = 0;
if (ii++ > 1000) {
close (iFile);
return;
}
write (iFile, recvbuf, iReadCount);
}
static int SetPortSig ()
{
sigaction_io.sa_handler = signal_handler_IO;
sigemptyset (&(sigaction_io.sa_mask));
sigaction_io.sa_flags = 0;
sigaction_io.sa_restorer = NULL;
sigaction (SIGIO, &sigaction_io, NULL);
/* allow the process to receive SIGIO */
if (-1 == fcntl (fd, F_SETFL, O_ASYNC))
return (-1);
if (-1 == fcntl (fd, F_SETOWN, getpid ()))
return (-1);
/* Make the file descriptor asynchronous (the manual page says only
O_APPEND and O_NONBLOCK, will work with F_SETFL...) */
return (0);
}
int main ()
{
if (OpenComPort (0, 115200, 8, "1", 'N')) {
fprintf (stderr,
"Make sure /dev/ttyS0 not in use or you have enough privilege.n");
exit (-1);
}
SendFile ();
CloseComPort ();
exit (0);
}
/* Open serial port ComPort at baudrate baud rate. */
INT32 OpenComPort (INT32 ComPort, INT32 baudrate, INT32 databit,const char *stopbit, char parity)
{
}
/* close serial port by use of file descriptor fd */
void CloseComPort ()
{
/* flush output data before close and restore old attribute */
tcsetattr (fd, TCSADRAIN, &termios_old);
close (fd);
}
INT32 ReadComPort (void *data, INT32 datalength)
{
INT32 retval = 0;
FD_ZERO (&fs_read);
FD_SET (fd, &fs_read);
tv_timeout.tv_sec = TIMEOUT_SEC (datalength, GetBaudrate ());
tv_timeout.tv_usec = TIMEOUT_USEC;
retval = select (fd + 1, &fs_read, NULL, NULL, &tv_timeout);
if (retval) {
return (read (fd, data, datalength));
}
else
return (-1);
}
/* send file const char *pathname */
INT32 SendFile ()
{
while (1) {
SetPortSig ();
} /* end of while (1) */
fflush (stdout);
fflush (stderr);
close (fd);
return (0);
}
static void signal_handler_IO (int status)
{
char recvbuf[CHAR_MAX];
int iReadCount = 0;
iReadCount = ReadComPort (recvbuf, sizeof (recvbuf));
printf ("nRead = %d", iReadCount);
static int ii = 0;
if (ii++ > 1000) {
close (iFile);
return;
}
write (iFile, recvbuf, iReadCount);
}
static int SetPortSig ()
{
sigaction_io.sa_handler = signal_handler_IO;
sigemptyset (&(sigaction_io.sa_mask));
sigaction_io.sa_flags = 0;
sigaction_io.sa_restorer = NULL;
sigaction (SIGIO, &sigaction_io, NULL);
/* allow the process to receive SIGIO */
if (-1 == fcntl (fd, F_SETFL, O_ASYNC))
return (-1);
if (-1 == fcntl (fd, F_SETOWN, getpid ()))
return (-1);
/* Make the file descriptor asynchronous (the manual page says only
O_APPEND and O_NONBLOCK, will work with F_SETFL...) */
return (0);
}
int main ()
{
if (OpenComPort (0, 115200, 8, "1", 'N')) {
fprintf (stderr,
"Make sure /dev/ttyS0 not in use or you have enough privilege.n");
exit (-1);
}
SendFile ();
CloseComPort ();
exit (0);
}
|
~~~snip~~~
while (1) {
SetPortSig ();
} /* end of while (1) */
~~~snip~~~
这里是个死循环,当然会把CPU给用光了
循环一次睡个10毫秒吧
while (1) {
SetPortSig ();
} /* end of while (1) */
~~~snip~~~
这里是个死循环,当然会把CPU给用光了
循环一次睡个10毫秒吧
|
while (1) {
SetPortSig ();
}
fflush (stdout);
fflush (stderr);
close (fd);
return (0);
没细看 不过怀疑上面while(1){} 后面的程序执行不到 while 内不加延迟是个不好的习惯
如果真的需要时时不停 至少也需要等等什么需要的资源做个信号量什么的
SetPortSig ();
}
fflush (stdout);
fflush (stderr);
close (fd);
return (0);
没细看 不过怀疑上面while(1){} 后面的程序执行不到 while 内不加延迟是个不好的习惯
如果真的需要时时不停 至少也需要等等什么需要的资源做个信号量什么的
|
同意,如果SetPortSig不是阻塞的话,自然就会占用率100%了……
|
忙等,换
|
-_-b