如何在BeagleBoard上的Angstrom Linux上启动时运行C++、PortAudio应用程序
How to run a C++, PortAudio application on startup on Angstrom Linux on a BeagleBoard?
我有一个名为xooky_nabox的命令行应用程序,它是用c++编程的。它读取一个puredata补丁,处理比格犬板音频输入插孔的信号,并通过音频输出插孔输出信号。
我希望应用程序在Beagleboard启动时运行,并一直运行直到该板关闭。它没有GUI,也没有键盘或监视器,只有音频输入和输出插孔。
如果我手动运行应用程序,一切都很好:
xooky_nabox -audioindev 1 -audiooutdev 1 /var/xooky/patch.pd
如果我在后台运行它,它也会运行良好:
xooky_nabox -audioindev 1 -audiooutdev 1 /var/xooky/patch.pd &
现在,让我展示该程序的两个版本的代码布局(完整内容位于https://github.com/rvega/XookyNabox):
版本1,主线程保持活动状态:
void sighandler(int signum){
time_t rawtime;
time(&rawtime);
std::ofstream myfile;
myfile.open ("log.txt",std::ios::app);
myfile << ctime(&rawtime) << " Caught signal:" << signum << " " << strsignal(signum) << "n";
myfile.close();
if(signum == 15 || signum == 2){
exit(0);
}
}
int main (int argc, char *argv[]) {
// Subscribe to all system signals for debugging purposes.
for(int i=0; i<64; i++){
signal(i, sighandler);
}
// Sanity checks, error and help messages, etc.
parseParameters(argc, argv);
//Start Signal processing and Audio
initZenGarden();
initAudioIO();
// Keep the program alive.
while(1){
sleep(10);
}
// This is obviously never reached, so far no problems with that...
stopAudioIO();
stopZengarden();
return 0;
}
static int paCallback( const void *inputBuffer, void *outputBuffer, unsigned long framesPerBuffer, const PaStreamCallbackTimeInfo* timeInfo, PaStreamCallbackFlags statusFlags, void *userData ){
// This is called by PortAudio when the output buffer is about to run dry.
}
版本2,执行是分叉的,并与启动它的终端分离:
void go_daemon(){
// Run the program as a daemon.
pid_t pid, sid;
pid = fork(); // Fork off the parent process
if (pid < 0) {
exit(EXIT_FAILURE);
}
if (pid > 0) {
exit(EXIT_SUCCESS); // If child process started ok, exit the parent process
}
umask(0); // Change file mode mask
sid = setsid(); // Create a new session ID for the child process
if (sid < 0) {
// TODO: Log failure
exit(EXIT_FAILURE);
}
if((chdir("/")) < 0){ //Change the working directory to "/"
//TODO: Log failre
exit(EXIT_FAILURE);
}
close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);
}
int main (int argc, char *argv[]) {
go_daemon();
// Subscribe to all system signals for debugging purposes.
for(int i=0; i<64; i++){
signal(i, sighandler);
}
// Sanity checks, error and help messages, etc.
parseParameters(argc, argv);
//Start Signal processing and Audio
initZenGarden();
initAudioIO();
// Keep the program alive.
while(1){
sleep(10);
}
// This is obviously never reached, so far no problems with that...
stopAudioIO();
stopZengarden();
return 0;
}
尝试在启动时运行它
我试过在启动时使用几种方法运行这两个版本的程序。结果总是一样的。当比格犬启动时,我能听到微弱的声音输出,然后声音停止,出现登录屏幕(我的电脑上有一个串行终端和运行的微型计算机)。对我来说最奇怪的是,xooky_nabox进程实际上在登录后一直在运行,但没有声音输出。。。
以下是我尝试过的:
在crontab中添加@reboot条目,并使用尾随的"与"符号启动程序(程序的版本1):
@reboot xooky_nabox <params> &
在crontab(版本1)中添加了一个启停守护程序:
@reboot start-stop-daemon -S -b --user daemon -n xooky_nabox -a /usr/bin/xooky_nabox -- <params>
在/etc/init.d/xooky上创建了一个脚本并执行
$chmod +x xooky
$update-rc.d xooky defaults
并尝试了不同版本的启动脚本:版本1的start-stop守护进程,直接用尾随的"与"号调用程序(版本1),直接用没有尾随的"answers"号调用该程序(版本2)。
此外,如果我从串行终端或ssh会话(usb网络)手动运行程序;然后我运行top,程序会正常运行几秒钟,消耗大约15%的cpu。然后它将停止输出声音,它的cpu消耗将上升到30%左右。在这种情况下,我的log.txt文件没有显示操作系统发送到程序的信号。
当程序的版本2在启动时运行时,日志将显示如下内容:
Mon Jun 6 02:44:49 2011 Caught signal:18 Continued
Mon Jun 6 02:44:49 2011 Caught signal:15 Terminated
有人对如何调试这个有什么想法吗?关于如何在启动时启动我的程序的建议?
在版本2中,我认为你应该open
(和dup2
)/dev/null
到STDIN
/STDOUT
/STDERR
。只要关闭把手就会产生问题。
像这样的东西:
int fd = open("/dev/null", O_RDWR);
dup2( fd, STDOUT_FILENO );
(我不知道启动-停止守护程序做什么。无法帮助版本1,对不起)
有一个C函数可以创建一个守护程序
#include <unistd.h>
int daemon(int nochdir, int noclose);
更多信息可在daemon(3)
的手册页中找到也许这会有所帮助。
如果你想在linux启动时启动你的守护程序,你应该知道你在发行版中使用的是哪个init
版本,但通常情况下,你可以在/etc/init.d/rc
中添加执行守护程序的命令(但这似乎不是一个好主意)。该文件由init
在linux启动时执行。
我最终放弃了PortAudio,实现了一个运行自己服务器的JACK客户端,所以这个问题对我来说不再重要了。
- 运行同一解决方案的另一个项目的项目
- CMake-按正确顺序将项目与C运行时对象文件链接
- 如何运行位于boost/libs/python/example/tutorial目录中的hello.cpp和Jamfil
- 代码在main()中运行,但在函数中出现错误
- 我在c++代码中生成了一个运行时#3异常
- 如何在linux终端中同时编译和运行c++代码
- 为什么在运行时没有向我们提供有关分段错误的更多信息?
- 如何在运行中期切换GTK CSS style_context
- 如何在MS Visual Studio 2019中运行QT UI
- 如何通过cpp程序运行shell脚本
- IPC使用多个管道和分支进程来运行Python程序
- 删除指向指针的指针是运行时错误吗
- 如何用参数值调用函数(仅在运行时已知)
- 为什么即使使用-cudart-static进行编译,库用户仍然需要链接到cuda运行时
- 是否可以在编译时初始化数组,以便在运行时不会花费时间?
- c++中的指针和运行时错误
- 在C应用程序中运行C++(带有STL)函数
- 运行程序时出现问题
- 控制允许动态运行c++的并发操作数
- 无法获取菜单选择以运行函数.C++