用C程序启动和终止cu
问题描述:
我试图通过ttyS0使用cu(谷歌“cu unix”来了解更多关于cu的信息)与另一个UNIX设备进行通信。我的程序工作得很好,但问题是,在第一次执行程序(建立连接,读取日志文件和其他东西)之后,终端无法再访问。我刚刚发布了我的问题的核心代码的简化版本,我只专注于我的实际问题:用C程序启动和终止cu
当我手动执行这些命令“cu -l/dev/ttyS0 -s 115200“和”〜“。 (就像在cu的手册页中所示:〜。终止连接)一切正常。像
system("cu -l /dev/ttyS0 -s 115200");
system("~.");
顺序程序无法正常工作,因为铜依然活跃,并没有什么后执行....程序只是坐在那里等待立方米......同样的事情会发生在一个简单的bash脚本... cu会阻止程序/脚本继续 - 这就是为什么我使用线程,就像我说的,我的实际程序工作,但该程序没有终止像我想要的和终端有重新启动。 当我执行下面的程序,我只得到
Connected
sh: ~.: not found
按enter
cu: can't restore terminal: Input/Output error
Disconnected
和不能使用的终端是开放的(不能打字或用它做任何事情)...
#define _BSD_SOURCE
#include <termios.h>
#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>
#include <time.h>
#include <stdlib.h>
#include <pthread.h>
void first(){
system("cu -l /dev/ttyS0 -s 115200");
pthread_exit(NULL);
}
void second(){
system("~."); //also "~.\n" isn't changing anything
pthread_exit(NULL);
int main(){
pthread_t thread1, thread2;
pthread_create (&thread1, NULL, (void*)first, NULL);
sleep(3);
pthread_create (&thread2, NULL, (void*)second, NULL);
sleep(4);
exit(0);
return 0;
}
答
当你的手去做,你键入~.
未作为一个系统命令,但作为输入仍在运行的进程。最好的证明是当时你没有shell提示符。
所以相当于不要做另一个system("~.")
,而是将这些字符作为输入传递给第一个system("cu ...")
。 例如:
system("echo '~.' | cu ....");
显然,这并不让你开出“铜”连接,并发送“〜”。晚些时候。如果你想这样做,我建议你看看popen
命令(man 3 popen
)。这将启动一个cu
进程,并为您留下一个文件描述符,稍后您可以在其中编写~.
。
你知道'系统'产生一个新的过程,对吧?而且你不应该在多线程的过程中做到这一点('fork')? – zoska 2014-12-05 10:42:22
你期望什么'system(“〜。”);'去做? – user3629249 2014-12-05 11:23:18
答案在我的文章中:“当我手动执行这些命令”cu -l/dev/ttyS0 -s 115200“和”〜“时(就像在cu:〜的手册页中所述),终止连接)一切正常。“请在提问前阅读。 – Buitenlander 2014-12-05 11:44:55