如何在程序中打开一个控制台终端并在其中运行指定程序?

han012 2009-04-15 05:26:41
请问如何在程序中打开一个新的控制台终端(terminal), 然后在启动
一个新程序(fork/exec),并让新进程在新的控制台终端中运行?
...全文
758 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
超龄编码人 2009-04-22
  • 打赏
  • 举报
回复
system调用
BlairZhong 2009-04-21
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 HellDevil 的回复:]
引用 1 楼 hairetz 的回复:
直接在代码里system("shell指令");

该指令会在一个后台子进程终端执行.


system() 不好的习惯呕,估计代码看的少了,一般老外写的程序很少使用system()尽管这个方法比较简单。
你可以尝试使用 FILE *popen(const char *command, const char *type);

或者gboolean g_spawn_command_line_sync (const gchar *command_line, //这里放你的shell命令

[/Quote]

这个g_spawn_command_line_sync是glib的?
once_and_again 2009-04-20
  • 打赏
  • 举报
回复
systm("gnome-terminal");
T-Quake 2009-04-20
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 hairetz 的回复:]
直接在代码里system("shell指令");

该指令会在一个后台子进程终端执行.
[/Quote]

system() 不好的习惯呕,估计代码看的少了,一般老外写的程序很少使用system()尽管这个方法比较简单。
你可以尝试使用 FILE *popen(const char *command, const char *type);

或者gboolean g_spawn_command_line_sync (const gchar *command_line, //这里放你的shell命令
gchar **standard_output,
gchar **standard_error,
gint *exit_status,
GError **error);
推荐 最好使用后者 popen没办法的话也可以使用(如果你想获取命令在终端上执行后的输出结果,可以用fread读取命令后的结果,可以使用popen),如果没这个要求,推荐使用g_spawn_command_line_sync()(同步的) 异步的也有。自己看吧
hupo1982 2009-04-20
  • 打赏
  • 举报
回复
我一直用system
xhy_851221 2009-04-19
  • 打赏
  • 举报
回复
system和fork+exec都可以
nyhenry1 2009-04-15
  • 打赏
  • 举报
回复
morris88的代码挺有用记下来了:)
liliangbao 2009-04-15
  • 打赏
  • 举报
回复
帮顶~~~
morris88 2009-04-15
  • 打赏
  • 举报
回复
建议看看LZ, agetty -> login -> bash,它貌似就像楼主需要的:

/*
* We must fork before setuid() because we need to call
* pam_close_session() as root.
*/

child_pid = fork();
if (child_pid < 0) {
int errsv = errno;
/* error in fork() */
fprintf(stderr, _("login: failure forking: %s"), strerror(errsv));
PAM_END;
exit(0);
}

if (child_pid) {
/* parent - wait for child to finish, then cleanup session */
close(0);
close(1);
close(2);
sa.sa_handler = SIG_IGN;
sigaction(SIGQUIT, &sa, NULL);
sigaction(SIGINT, &sa, NULL);

/* wait as long as any child is there */
while(wait(NULL) == -1 && errno == EINTR)
;
openlog("login", LOG_ODELAY, LOG_AUTHPRIV);
PAM_END;
exit(0);
}

/* child */

/* restore to old state */
sigaction(SIGHUP, &oldsa_hup, NULL);
sigaction(SIGTERM, &oldsa_term, NULL);
if(got_sig)
exit(1);

/*
* Problem: if the user's shell is a shell like ash that doesnt do
* setsid() or setpgrp(), then a ctrl-\, sending SIGQUIT to every
* process in the pgrp, will kill us.
*/

/* start new session */
setsid();

/* make sure we have a controlling tty */
opentty(ttyn);
openlog("login", LOG_ODELAY, LOG_AUTHPRIV); /* reopen */

/*
* TIOCSCTTY: steal tty from other process group.
*/
if (ioctl(0, TIOCSCTTY, 1))
syslog(LOG_ERR, _("TIOCSCTTY failed: %m"));
#endif
signal(SIGINT, SIG_DFL);

/* discard permissions last so can't get killed and drop core */
if(setuid(pwd->pw_uid) < 0 && pwd->pw_uid) {
syslog(LOG_ALERT, _("setuid() failed"));
exit(1);
}

/* wait until here to change directory! */
if (chdir(pwd->pw_dir) < 0) {
printf(_("No directory %s!\n"), pwd->pw_dir);
if (chdir("/"))
exit(0);
pwd->pw_dir = "/";
printf(_("Logging in with home = \"/\".\n"));
}

/* if the shell field has a space: treat it like a shell script */
if (strchr(pwd->pw_shell, ' ')) {
buff = malloc(strlen(pwd->pw_shell) + 6);

if (!buff) {
fprintf(stderr, _("login: no memory for shell script.\n"));
exit(0);
}

strcpy(buff, "exec ");
strcat(buff, pwd->pw_shell);
childArgv[childArgc++] = "/bin/sh";
childArgv[childArgc++] = "-sh";
childArgv[childArgc++] = "-c";
childArgv[childArgc++] = buff;
} else {
tbuf[0] = '-';
xstrncpy(tbuf + 1, ((p = rindex(pwd->pw_shell, '/')) ?
p + 1 : pwd->pw_shell),
sizeof(tbuf)-1);

childArgv[childArgc++] = pwd->pw_shell;
childArgv[childArgc++] = tbuf;
}

childArgv[childArgc++] = NULL;

execvp(childArgv[0], childArgv + 1);

errsv = errno;

if (!strcmp(childArgv[0], "/bin/sh"))
fprintf(stderr, _("login: couldn't exec shell script: %s.\n"),
strerror(errsv));
else
fprintf(stderr, _("login: no shell: %s.\n"), strerror(errsv));

exit(0);
}
  • 打赏
  • 举报
回复
直接在代码里system("shell指令");

该指令会在一个后台子进程终端执行.

23,116

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧