守护进程(daemon)中启动另一个程序的问题!

zkxz 2004-08-30 09:39:07
如何在守护进程中启动另外一个程序,使的那个程序可以在某个指定的tty中执行?
各位高手指点一下,已经郁闷很久了!谢谢!
...全文
622 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
zkxz 2004-09-07
  • 打赏
  • 举报
回复
为什么没人回答了?yanghuajia(newperson)在不在?
zkxz 2004-09-04
  • 打赏
  • 举报
回复
没有人写过类似的程序吗?
zkxz 2004-08-31
  • 打赏
  • 举报
回复
yanghuajia(newperson),救命啊!
第二次给的代码不行啊!加到我的守护进程中,一启动,就登出,而且按键也没响应!
yanghuajia 2004-08-31
  • 打赏
  • 举报
回复
命令演示2
1.停止tty1的进程
# ps xj|grep tty1
19356 19361 19361 19361 tty1 19431 S 0 0:00 -bash
19361 19429 19429 19361 tty1 19431 R 0 0:17 yes
19361 19431 19431 19361 tty1 19431 S 0 0:00 less log
9239 19441 19440 9239 pts/9 19440 S 0 0:00 grep tty1

#kill -SIGSTOP 19361 19429 19431
# # ps xj|grep tty1
19356 19361 19361 19361 tty1 19431 T 0 0:00 -bash
19361 19429 19429 19361 tty1 19431 T 0 1:39 yes
19361 19431 19431 19361 tty1 19431 T 0 0:00 less log
9239 19445 19444 9239 pts/9 19444 S 0 0:00 grep tty1

---------# ./simple 我的程序

#ps xj|grep vi
1 19448 19447 9239 pts/9 19453 S 0 0:00 vi
9239 19454 19453 9239 pts/9 19453 S 0 0:00 grep vi

vi 已经在tty1上,好像大部分操作正常。
vi退出后

#kill -SIGCONT 19361 19429 19431
#ps xj|grep tty1
19356 19361 19361 19361 tty1 19431 S 0 0:00 -bash
19361 19429 19429 19361 tty1 19431 R 0 2:17 yes
19361 19431 19431 19361 tty1 19431 S 0 0:00 less log
9239 19468 19467 9239 pts/9 19467 S 0 0:00 grep tty1


一切正常
yanghuajia 2004-08-31
  • 打赏
  • 举报
回复
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <termios.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <stdlib.h>

int main(int argc, char * argv[])
{
int fd = 0;
pid_t pid = 0;
setsid();
fd = open("/dev/tty1",O_RDWR);
dup2(fd,0);
dup2(fd,1);
dup2(fd,2);
pid = fork();
if(pid == 0)
{
//tcsetpgrp(fd,getpid());
execl("/bin/vi","vi",NULL);

exit(0);
}
return 1;
}

命令演示
1.停止tty1的进程
# ps xj|grep tty1
1 8521 8521 8521 tty1 8521 S 0 0:00 /sbin/mingetty tty1
9239 19335 19334 9239 pts/9 19334 S 0 0:00 grep tty1
#kill -SIGSTOP 8521
# ps xj|grep tty1
1 8521 8521 8521 tty1 8521 T 0 0:00 /sbin/mingetty tty1
9239 19337 19336 9239 pts/9 19336 S 0 0:00 grep tty1
#ps xj|grep vi
1 19345 19344 9239 pts/9 19349 S 0 0:00 vi
9239 19350 19349 9239 pts/9 19349 S 0 0:00 grep vi
vi 已经在tty1上,好像大部分操作正常。

#kill -SIGCONT 8521
#ps xj|grep tty1
1 19356 19356 19356 tty1 19356 S 0 0:00 /sbin/mingetty tty1
9239 19360 19359 9239 pts/9 19359 S 0 0:00 grep tty1

一切正常
yanghuajia 2004-08-31
  • 打赏
  • 举报
回复
是我说的不够清楚。

我们当时的做法之一是 先停止那个使用控制终端的进程。(kill SIGSTOP)

然后简单的打开终端读和写.因为只有你在使用该终端,不会有干扰。

用完后再起动那个进程 (kill SIGCONT)

因此这个方法的问题是 找到那个进程id
如果还想偷懒不愿意找,就停止整个session的进程

参考书: Unix 环境高级编程
zkxz 2004-08-30
  • 打赏
  • 举报
回复
比如在tty1中启动mail,或者在tty2中启动vi ^_^
pacman2000 2004-08-30
  • 打赏
  • 举报
回复
指定的tty? 什么意思?
zkxz 2004-08-30
  • 打赏
  • 举报
回复
To yanghuajia(newperson),
谢谢你先!我的确非要做这个工作。我明天会试试上面的代码。
再次感谢!
yanghuajia 2004-08-30
  • 打赏
  • 举报
回复
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <termios.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <stdlib.h>

int main(int argc, char * argv[])
{
int fd = 0;
pid_t pid = 0;
//setsid();
pid = fork();
if(pid == 0)
{
//setpgid(0,7182);
fd = open("/dev/pts/6",O_RDWR);
if(dup2(fd,0) == -1)
{
perror("dup2 sid :");
exit(0);
}
if(setsid()== -1)
{
perror("set sid :");
exit(0);
}

if( -1 == setpgid(0,7182))
{
perror("seta pg id :");
// exit(0);
}


if (ioctl(0, TIOCSCTTY, (char *) 1) < 0)
{
perror("TIOCSCTTY error");
exit(0);
}
//if(-1 == (pid =tcgetpgrp(0)))
if(0)
{
perror("tcgetpgrp :");
exit(0);
}
else
printf("tc grp %d\n",pid);
if(tcsetpgrp(0,getpid()) == -1)
perror("tcsetpgrp :");

dup2(0, 1);
dup2(0, 2);


execl("/bin/vi","vi",NULL);

exit(0);
}
//wait(NULL);
sleep(4);
return 1;

}
yanghuajia 2004-08-30
  • 打赏
  • 举报
回复
最简单的方法 据说是

kill -SIGSTOP 13210(对应的控制终端进程ID)

做完你的事情

kill -SIGCONT 13210(对应的控制终端进程ID)
yanghuajia 2004-08-30
  • 打赏
  • 举报
回复
很麻烦!一定要完成这个任务吗?如果是我再帮你。不然的话,讨论一下其他方法。
zkxz 2004-08-30
  • 打赏
  • 举报
回复
To yanghuajia(newperson),
我刚才把你的代码加进了我的程序中,是可以的。不过,正如你所说,它只是刚好完成我的工作要求,但又出了很多问题。

我想你这段代码是重新让daemon与tty相关联,使得子进程可以在该tty上执行。那么,完成之后,也应该重新让daemon进程与tty脱离吧!哪该怎么做呢?是不是像daemon初始化时候一样做?

我的daemon程序想在启动了vi之后等待其结束才继续向下执行,于是我就在你的那段代码中加了
if(pid>0)
waitpid(pid);
可是vi启动之后,所有的键盘输入都不是只送给vi,连shell也会接受到,结果造成vi还在执行的同时shell也可以接受命令,我把waitpid去掉了也这样,这是怎么回事呢?

请高手指点迷津!
yanghuajia 2004-08-30
  • 打赏
  • 举报
回复
很麻烦。要做很多保存和恢复工作。上面的例子只是刚好完成你的工作要求
yanghuajia 2004-08-30
  • 打赏
  • 举报
回复
//sample
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
int main(int argc, char * argv[])
{
int fd = 0;
pid_t pid = 0;
setsid();
fd = open("/dev/pts/6",O_RDWR);
dup2(fd,0);
dup2(fd,1);
dup2(fd,2);
pid = fork();
if(pid == 0)
{
tcsetpgrp(fd,getpid());
execl("/bin/vi","vi",NULL);

exit(0);
}
return 1;
}

23,110

社区成员

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

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