应用程序在第一次启动命名管道的 GTK3 g_io_add_watch() 后获得 100% CPU

问题描述 投票:0回答:1

我正在制作以下应用程序,它监视一个名为“信息”的命名管道,并使用此命名管道提供的字符串更新标签文本。

#include <gtk/gtk.h>

GtkWidget* label;

static void
activate (GtkApplication *app,
          gpointer        user_data)
{
  GtkWidget *window;

  window = gtk_application_window_new (app);
  gtk_window_set_title (GTK_WINDOW (window), "Window");
  gtk_window_set_default_size (GTK_WINDOW (window), 100, 20);

  // create label
  label = gtk_label_new ("aho");
  gtk_container_add (GTK_CONTAINER (window), label);


  gtk_widget_show_all (window);
}

gboolean my_callback(GIOChannel *source, GIOCondition condition, gpointer data){
  gchar *buf=NULL;
  GError *error = NULL;

  g_io_channel_read_line(source, &buf, NULL, NULL, NULL);
  gtk_label_set_text (GTK_LABEL(label), buf); 
  g_free(buf);

  // for next watch
  g_io_add_watch(source, G_IO_IN,(GIOFunc) my_callback, NULL);

  return FALSE;
}

int
main (int    argc,
      char **argv)
{
  GtkApplication *app;
  GIOChannel *channel;
  int status, fd;

  app = gtk_application_new ("org.gtk.example", G_APPLICATION_FLAGS_NONE);  
  g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);

  channel = g_io_channel_new_file ("info", "r", NULL);
  g_io_add_watch(channel, G_IO_IN,(GIOFunc) my_callback, NULL);

  status = g_application_run (G_APPLICATION (app), argc, argv);
  g_object_unref (app);

  return status;
}

此应用程序运行良好,除了 CPU 占用在第一次触发 g_io_add_watch() 后从外部进程 fx 写入命名管道时获得 100%

echo neko > info
.

我查看了我的个人资料,但没有任何信息。

pi@raspberrypi:~/gtk3-sandbox $ gprof ./infobox3 gmon.out
Flat profile:

Each sample counts as 0.01 seconds.
 no time accumulated

  %   cumulative   self              self     total           
 time   seconds   seconds    calls  Ts/call  Ts/call  name    

 %         the percentage of the total running time of the
time       program used by this function.

cumulative a running sum of the number of seconds accounted
 seconds   for by this function and those listed above it.

 self      the number of seconds accounted for by this
seconds    function alone.  This is the major sort for this
           listing.

calls      the number of times this function was invoked, if
           this function is profiled, else blank.

 self      the average number of milliseconds spent in this
ms/call    function per call, if this function is profiled,
       else blank.

 total     the average number of milliseconds spent in this
ms/call    function and its descendents per call, if this
       function is profiled, else blank.

name       the name of the function.  This is the minor sort
           for this listing. The index shows the location of
       the function in the gprof listing. If the index is
       in parenthesis it shows where it would appear in
       the gprof listing if it were to be printed.

CPU 100% 是什么原因造成的?我怎样才能避免它? 我怎样才能达到罚款的原因和解决办法?

c gtk3 named-pipes
1个回答
0
投票

您获得 100% CPU 的原因是因为管道在处理完成后被 HUP 信号关闭。因此,每次您的应用程序检查新数据时,它都会立即返回

G_IO_HUP
。到时候对应的
GIOChannel
就不能用了

解决此问题的方法是在您的

G_IO_HUP
调用中显式添加
g_io_add_watch
并在您的回调中检查它。如果你想从同一个文件中再次读取,你必须创建一个新的
GIOChannel
并用
g_io_add_watch()
添加它。

在任何情况下,实际上,将

g_io_add_watch()
G_IO_IN
一起使用而忽略
G_IO_HUP
G_IO_ERR
没有多大意义,因为否则您将明确不处理任何错误(导致像您这样的问题).

© www.soinside.com 2019 - 2024. All rights reserved.