Skip to content

Commit

Permalink
improve process demo
Browse files Browse the repository at this point in the history
  • Loading branch information
waruqi committed Mar 22, 2020
1 parent 2780ce3 commit 221b5e8
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 9 deletions.
66 changes: 58 additions & 8 deletions src/demo/platform/process.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@
#include "../demo.h"

/* //////////////////////////////////////////////////////////////////////////////////////
* main
* private implementation
*/
tb_int_t tb_demo_platform_process_main(tb_int_t argc, tb_char_t** argv)
static tb_void_t tb_demo_process_test_run(tb_char_t const** argv)
{
#if 0
// run
tb_long_t ok = tb_process_run(argv[1], (tb_char_t const**)(argv + 1), tb_null);

// trace
tb_trace_i("run: %s: %ld", argv[1], ok);

#elif 1
}
static tb_void_t tb_demo_process_test_pipe(tb_char_t const** argv)
{
// init pipe files
tb_pipe_file_ref_t file[2] = {0};
if (tb_pipe_file_init_pair(file, 0))
Expand Down Expand Up @@ -68,8 +68,9 @@ tb_int_t tb_demo_platform_process_main(tb_int_t argc, tb_char_t** argv)
tb_pipe_file_exit(file[0]);
tb_pipe_file_exit(file[1]);
}
#else

}
static tb_void_t tb_demo_process_test_waitlist(tb_char_t const** argv)
{
// init processes
tb_size_t count1 = 0;
tb_process_ref_t processes1[5] = {0};
Expand Down Expand Up @@ -117,8 +118,57 @@ tb_int_t tb_demo_platform_process_main(tb_int_t argc, tb_char_t** argv)
count1 = count2;
}
}
}
static tb_void_t tb_demo_process_test_exit(tb_char_t** argv, tb_bool_t detach)
{
tb_size_t i = 0;
tb_process_attr_t attr = {0};
tb_process_ref_t processes[10];
if (detach) attr.flags |= TB_PROCESS_FLAG_DETACH;
for (i = 0; i < 10; i++)
processes[i] = tb_process_init(argv[1], (tb_char_t const**)(argv + 1), &attr);

// we attempt to enter or do ctrl+c and see process list in process monitor
tb_getchar();

// exit processes
for (i = 0; i < 10; i++)
{
tb_process_exit(processes[i]);
processes[i] = tb_null;
}
}

/* //////////////////////////////////////////////////////////////////////////////////////
* main
*/
tb_int_t tb_demo_platform_process_main(tb_int_t argc, tb_char_t** argv)
{
#if 0
tb_demo_process_test_run(argv);
#else
tb_used(tb_demo_process_test_run);
#endif

#if 0
tb_demo_process_test_pipe(argv);
#else
tb_used(tb_demo_process_test_pipe);
#endif

#if 0
tb_demo_process_test_waitlist(argv);
#else
tb_used(tb_demo_process_test_waitlist);
#endif

// ok
#if 1
// we can run `xxx.bat` or `xxx.sh` shell command to test it
// @see https://github.com/xmake-io/xmake/issues/719
tb_demo_process_test_exit(argv, tb_false);
// tb_demo_process_test_exit(argv, tb_true);
#else
tb_used(tb_demo_process_test_exit);
#endif
return 0;
}
2 changes: 1 addition & 1 deletion src/tbox/platform/process.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ typedef enum __tb_process_flag_e
{
TB_PROCESS_FLAG_NONE = 0
, TB_PROCESS_FLAG_SUSPEND = 1 //!< suspend process
, TB_PROCESS_FLAG_DETACH = 2 //!< detach process from the parent process group, this process will be not exited after the parent process exited
, TB_PROCESS_FLAG_DETACH = 2 //!< all subprocesses will be exited when the parent process is exited (ctrl+c or onexit) if this flag is not setted

}tb_process_flag_e;

Expand Down

0 comments on commit 221b5e8

Please sign in to comment.