【发布时间】:2017-08-28 22:23:20
【问题描述】:
我现在遇到了这个奇怪的问题,基本上我正在尝试并行执行两个函数。
出现这个问题的小代码sn-p有这个目的:只要父函数/进程没有完成自己的任务,子函数就监听一些事件,以防子进程发生新事件/function 子进程应该终止整个程序。
这里是示例代码sn-p:
#include<stdio.h>
#include<stdlib.h>
void send();
int main(void)
{
send();
}
void send()
{
if( fork() ) { /*child process */
keep_checking_status(); /*listens to audio related events, and terminate the whole program when some specific kind of event occurs */
}
else {
/* Parent Process */
/* The parent process executes different other functions, lines of code here */
/* code lines */
func1();
func2();
func3();
func4();
/* More lines of code */
/* And when no event occurs in the child which means the program is still running and the parent process almost executed all the code it was supposed to do, so finally exit(0); */
exit(0);
}
}
现在的问题是,父进程不是只执行一次父进程,而是进入无限状态,并且不会执行所有代码以到达最后一行 func4() 或 exit(0); 并和平终止.
再次,我希望子进程监听我处理过的一些特定音频事件,而父进程应该只执行其中的代码行并退出。
父进程也没有while循环,只是一段代码,执行和计算一些其他的东西,所以感觉很奇怪。
我确实研究了一些 fork() 使用方式不同的示例,我尝试了它们但仍然是同样的问题。
编辑:(已解决):
重命名函数void send()后就ok了;到 void send11(); ,它现在可以按我的意愿工作了。
似乎我正在使用的库,即 libcurl 等具有像 send() 这样的功能,或者它们已经包含了所有 send() 的 linux 系统,这为我创造了这个问题。我的错。
如果有人可以回答为什么这种从不终止的行为,请发布您的详细答案,因为通常编译器会报告名称冲突,但在我的情况下它没有报告任何错误,而是程序进入无穷大。
【问题讨论】:
标签: c linux shell pthreads fork