免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 4793 | 回复: 2
打印 上一主题 下一主题

g_spawn_command_line_sync 这个函数 怎么理解 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-07-13 11:20 |只看该作者 |倒序浏览
g_spawn_command_line_sync  这个函数 怎么理解   
谢谢!!!

论坛徽章:
0
2 [报告]
发表于 2010-07-13 13:17 |只看该作者
难道楼主都只用百度么?
http://tadeboro.blogspot.com/200 ... ses-using-glib.html

  1. /*
  2. * Compile me with:
  3. *   gcc -o spawn spawn.c $(pkg-config --cflags --libs gtk+-2.0)
  4. */
  5. #include <gtk/gtk.h>

  6. typedef struct _Data Data;
  7. struct _Data {
  8.     /*
  9.      * Buffers that will display output
  10.      */
  11.     GtkTextBuffer  *out;
  12.     GtkTextBuffer  *err;

  13.     /*
  14.      * Progress bar that will be updated
  15.      */
  16.     GtkProgressBar *progress;

  17.     /*
  18.      * Timeout source id
  19.      */
  20.     gint            timeout_id;
  21. };

  22. static void
  23. cb_child_watch(GPid pid, gint status, Data * data)
  24. {
  25.     /*
  26.      * Remove timeout callback
  27.      */
  28.     g_source_remove(data->timeout_id);

  29.     /*
  30.      * Close pid
  31.      */
  32.     g_spawn_close_pid(pid);
  33. }

  34. static          gboolean
  35. cb_out_watch(GIOChannel * channel, GIOCondition cond, Data * data)
  36. {
  37.     gchar          *string;
  38.     gsize           size;

  39.     if (cond == G_IO_HUP) {
  40.         g_io_channel_unref(channel);
  41.         return (FALSE);
  42.     }

  43.     g_io_channel_read_line(channel, &string, &size, NULL, NULL);
  44.     gtk_text_buffer_insert_at_cursor(data->out, string, -1);
  45.     g_free(string);

  46.     return (TRUE);
  47. }

  48. static          gboolean
  49. cb_err_watch(GIOChannel * channel, GIOCondition cond, Data * data)
  50. {
  51.     gchar          *string;
  52.     gsize           size;

  53.     if (cond == G_IO_HUP) {
  54.         g_io_channel_unref(channel);
  55.         return (FALSE);
  56.     }

  57.     g_io_channel_read_line(channel, &string, &size, NULL, NULL);
  58.     gtk_text_buffer_insert_at_cursor(data->err, string, -1);
  59.     g_free(string);

  60.     return (TRUE);
  61. }

  62. static          gboolean
  63. cb_timeout(Data * data)
  64. {
  65.     /*
  66.      * Bounce progress bar
  67.      */
  68.     gtk_progress_bar_pulse(data->progress);

  69.     return (TRUE);
  70. }

  71. static void
  72. cb_execute(GtkButton * button, Data * data)
  73. {
  74.     GPid            pid;
  75.     gchar          *argv[] = { "./helper", NULL };
  76.     gint            out,
  77.                     err;
  78.     GIOChannel     *out_ch,
  79.                    *err_ch;
  80.     gboolean        ret;

  81.     /*
  82.      * Spawn child process
  83.      */
  84.     ret = g_spawn_async_with_pipes(NULL, argv, NULL,
  85.                                    G_SPAWN_DO_NOT_REAP_CHILD, NULL,
  86.                                    NULL, &pid, NULL, &out, &err, NULL);
  87.     if (!ret) {
  88.         g_error("SPAWN FAILED");
  89.         return;
  90.     }

  91.     /*
  92.      * Add watch function to catch termination of the process. This
  93.      * function * will clean any remnants of process.
  94.      */
  95.     g_child_watch_add(pid, (GChildWatchFunc) cb_child_watch, data);

  96.     /*
  97.      * Create channels that will be used to read data from pipes.
  98.      */
  99. #ifdef G_OS_WIN32
  100.     out_ch = g_io_channel_win32_new_fd(out);
  101.     err_ch = g_io_channel_win32_new_fd(err);
  102. #else
  103.     out_ch = g_io_channel_unix_new(out);
  104.     err_ch = g_io_channel_unix_new(err);
  105. #endif

  106.     /*
  107.      * Add watches to channels
  108.      */
  109.     g_io_add_watch(out_ch, G_IO_IN | G_IO_HUP, (GIOFunc) cb_out_watch,
  110.                    data);
  111.     g_io_add_watch(err_ch, G_IO_IN | G_IO_HUP, (GIOFunc) cb_err_watch,
  112.                    data);

  113.     /*
  114.      * Install timeout fnction that will move the progress bar
  115.      */
  116.     data->timeout_id = g_timeout_add(100, (GSourceFunc) cb_timeout, data);
  117. }

  118. int
  119. main(int argc, char **argv)
  120. {
  121.     GtkWidget      *window,
  122.                    *table,
  123.                    *button,
  124.                    *progress,
  125.                    *text;
  126.     Data           *data;

  127.     data = g_slice_new(Data);

  128.     gtk_init(&argc, &argv);

  129.     window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  130.     gtk_window_set_default_size(GTK_WINDOW(window), 400, 300);
  131.     g_signal_connect(G_OBJECT(window), "destroy",
  132.                      G_CALLBACK(gtk_main_quit), NULL);

  133.     table = gtk_table_new(2, 2, FALSE);
  134.     gtk_table_set_row_spacings(GTK_TABLE(table), 6);
  135.     gtk_table_set_col_spacings(GTK_TABLE(table), 6);
  136.     gtk_container_add(GTK_CONTAINER(window), table);

  137.     button = gtk_button_new_from_stock(GTK_STOCK_EXECUTE);
  138.     g_signal_connect(G_OBJECT(button), "clicked",
  139.                      G_CALLBACK(cb_execute), data);
  140.     gtk_table_attach(GTK_TABLE(table), button, 0, 1, 0, 1,
  141.                      GTK_FILL, GTK_SHRINK | GTK_FILL, 0, 0);

  142.     progress = gtk_progress_bar_new();
  143.     data->progress = GTK_PROGRESS_BAR(progress);
  144.     gtk_table_attach(GTK_TABLE(table), progress, 1, 2, 0, 1,
  145.                      GTK_FILL, GTK_SHRINK | GTK_FILL, 0, 0);

  146.     text = gtk_text_view_new();
  147.     data->out = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text));
  148.     gtk_table_attach(GTK_TABLE(table), text, 0, 1, 1, 2,
  149.                      GTK_EXPAND | GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);

  150.     text = gtk_text_view_new();
  151.     data->err = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text));
  152.     gtk_table_attach(GTK_TABLE(table), text, 1, 2, 1, 2,
  153.                      GTK_EXPAND | GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);

  154.     gtk_widget_show_all(window);

  155.     gtk_main();

  156.     g_slice_free(Data, data);

  157.     return (0);
  158. }
复制代码

论坛徽章:
0
3 [报告]
发表于 2010-07-14 23:16 |只看该作者
类似system函数吧
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP