开发者

Multiple terminal processes in C

开发者 https://www.devze.com 2023-02-24 03:41 出处:网络
I would like to, in C code, create more than one terminal processes. As in, I want to run foo in a terminal process, and then in a separate terminal process, I want to ru开发者_运维知识库n bar. Is thi

I would like to, in C code, create more than one terminal processes. As in, I want to run foo in a terminal process, and then in a separate terminal process, I want to ru开发者_运维知识库n bar. Is this possible? Could I do it with system(char *)?


This sounds like a job for posix_spawn(). Here is an example. Definitely do not call system() to launch new processes.


It's not clear what you mean by "terminal process". You can't (easily?) create another process that somehow causes the user to have more terminals open, but you can use fork(2) to create a child process.

fork creates another copy of your process with the same initial state, except that it returns 0 in the child process, and returns some non-zero PID in the parent process. So a sketch would look like:

if (fork())
    system("bar");
else
    system("foo");

This causes your original program to spawn two processes, running foo and bar.


If you really want to be evil, assuming you are running X, you could launch xterm's like this:

while(processes to spawn)
{
    if(!fork())
        execlp("xterm", "-e", "foo"); // or "bar" or "baz" ...
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号