- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我在结合使用 execl() 和 pthread 时遇到问题。
我的想法很简单:编写一个守护进程,在特定情况下启动一个外部进程(一个独立于守护进程本身的可执行文件)并等待该进程的返回值。此外,我希望有可能同时启动同一进程的多个实例。
我处理多线程的代码部分:
...
for (c_thread=0,i=0;i<N;i++)
{
/* Start actions before start threads */
for (j=c_thread;j<c_thread+config.max_threads;j++)
Before_Process(act[act_index[j]].measID);
/* Now create threads */
for (c=0,j=c_thread;j<c_thread+config.max_threads;j++)
{
Print_Log(LOG_DEBUG,"Create tread n. %d, measurementID=%s",c,act[act_index[j]].measID);
if ((ret=pthread_create(&pth[c],NULL,Start_Process_Thread,(void *) &act[act_index[j]].measID)))
{
Print_Log(LOG_ERR,"Error in creating thread (errorcode: %d)",ret);
exit(EXIT_FAILURE);
}
c++;
}
/* Joint threads */
for (j=0;j<config.max_threads;j++)
{
if ((ret=pthread_join(pth[j], (void**) &r_value[j])))
{
Print_Log(LOG_ERR,"Error in joint thread (errorcode: %d)",ret);
exit(EXIT_FAILURE);
}
}
/* Perform actions after the thread */
for (j=0;j<config.max_threads;j++)
{
status=*(int*) r_value[j];
Print_Log(LOG_DEBUG,"Joint tread n. %d. Return value=%d",j,status);
After_Process(act[act_index[c_thread+j]].measID,status);
}
c_thread += config.max_threads;
}
...
和函数 Start_Process_Thread:
void *Start_Process_Thread(void *arg)
{
int *ret;
char *measID;
measID=(char*)arg;
if (!(ret=malloc(sizeof(int))))
{
Print_Log(LOG_ERR, "allocation memory failed, code=%d (%s)",
errno, strerror(errno) );
exit(EXIT_FAILURE);
}
*ret=Start_Process(measID);
pthread_exit(ret);
}
int Start_Process(char *measID)
{
...
pipe(pfd);
pid=fork();
if (!pid)
{
signal(SIGALRM,Timeout);
alarm(config.timeout_process);
flag=0;
/*
Start the Process.
*/
ret=execl(config.pre_processor,buff_list[TokCount-1],config.db_name,measID,(char *) 0);
if (ret==-1)
{
alarm(0);
flag=1;
Print_Log(LOG_ERR,"Cannot run script %s, code=%d (%s)",config.process, errno, strerror(errno));
}
alarm(0);
close(1);
close(pfd[0]);
dup2(pfd[1],1);
write(1,&flag,sizeof(int));
}
else
{
wait(&status);
close(pfd[1]);
read(pfd[0],&flag,sizeof(int));
close(pfd[0]);
if (!flag)
{
if (WIFEXITED(status))
{
if (!(return_value=WEXITSTATUS(status)))
{
/*
Process gives no errors.
*/
Print_Log(LOG_INFO, "Processing of measurementID=%s ended succesfully!",measID);
}
else
{
/*
Process gives errors.
*/
Print_Log(LOG_WARNING,"Processor failed for measurementID=%s, code=%d",measID, return_value);
}
}
else
{
/*
Timeout for Process
*/
Print_Log( LOG_WARNING,"Timeout occurred in processing measurementID=%s",measID);
return_value=255;
}
}
}
}
从技术角度来看,上面的代码工作正常,但我在处理被调用外部进程的不同实例的返回值时遇到了问题。特别是,与某个实例关联的返回值会随机归因于另一个实例。例如,假设调用了 4 个不同的外部进程实例,参数分别为 meas1、meas2、meas3 和 meas4,并假设 meas1、meas2 和 meas3 已成功处理,而 meas4 进程失败。在这种情况下,我的代码混合了返回值,meas1、meas3 和 meas4 成功,meas2 失败,或者 meas1、meas2、meas4 成功,meas3 失败。
知道为什么会发生这种情况吗?
非常欢迎任何帮助。
预先感谢您的关注。
最佳答案
当进程中的任何线程执行 wait()
时,它会获得关于进程的任何已死亡子进程的信息——不一定是关于正在等待的线程启动的最后一个子进程的信息。
你需要考虑:
wait()
返回,但您忽略它)。wait()
并记录和报告子进程系列中的死亡事件)。有时这是一件病态的事情......
关于C编程。使用 execl 和 pthread,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11471797/
假设我 putenv 一个环境变量 ABC,然后执行 execl,或者我执行 execle 并将 ABC 添加到 envp 数组中我传递给 execle 的指针。 如果有的话,有区别吗? 最佳答案 p
我试图在 C++ 中运行 execl(),但它失败了,我不确定原因。 string fileName = "test.txt"; string computerName = "jssoile@
POSIX 系统公开了一系列 exec 函数,这些函数允许将可能不同的东西加载到当前进程中,保持打开的文件描述符、进程标识符等。 这可以出于多种原因完成,在我的例子中,这是自举——我想更改我自己进程的
我正在使用 c 中的 find 命令进行编程,但当我想对文件使用 exec 时,我被阻止了。 如果它是一个目录,那么很简单,我只使用 chdir(path) 但在文件上我有错误不是目录,所以我不能在上
if (fork_return==0) { printf("FROM THE CHILD PROCESS \n"); //print process id printf("The chil
我真的很喜欢这方面的一些调试帮助。我从早上到凌晨 4 点就一直在做这件事。 (我预计会在 7 小时内交付此内容 [上午 11 点]) main.c 中的所有内容都有效,但是当我创建一些子进程来使用 e
我试图理解这个系统调用(execle()),但我不知道它是如何工作的。我不知道如何使用 char* envp[],我们必须将其作为参数传递。我已经尝试过,但它不起作用: #include #incl
我有一个程序可以读取文件,对其进行处理并将结果放入输出文件中。当我有一个参数(输入文件)时,我创建输出文件并写入内容。 我创建了一个 fork() 以便将 stdout 重定向为 write() 内容
#include #include #include #define MAXLINE 512 main(int argc,char* argv[]){ int k; for (k
程序从配置文件中读取一些值,有些已定义,有些未定义,有些值为 0,有些处于事件状态。 我有以下代码: char *arg1="", *arg1_value="", *arg2="", *arg
你能帮我解决这个问题吗?execl 调用的程序“exam”和“students”执行了多少次?我认为正确的答案是程序“考试”有 8 个运行时间,“学生”有 0 个运行时间,因为在前两个 fork 中将
我正在制作一个 C 程序,该程序使用 execl 来执行 ls 命令来列出文件。如果在没有任何命令行参数的情况下调用程序,则会列出当前目录,如果用户将文件目录指定为命令行参数,则会列出该目录。 exe
我使用 TCC 编译并运行了以下程序在 Windows 7 下出现应用程序崩溃: #include int main(void) { if (execlp("c:\\windows\\sys
我正在使用 cywin 在 Windows 上编译 crashme 源代码,我遇到了一些编译错误。 错误: crashme.c: In function 'vfork_main': crashme.c
在UNIX环境下的系统编程上下文中,在使用C++编程语言时,在我的理解中,execl()会传入它要运行的程序的路径,以及一个vector。当传入该 vector 时,我将其理解为传入入口点,通常是 m
我有以下两个简单的程序: bye.cc #include int main() { std::cout #include #include #include using namespace
这个 execl 语句可能有什么问题?当我尝试运行它时,接收可执行文件提示 argc 小于 3。当我打印 argv 内容时,我得到以下信息: argv[1] = -1076146944 argv[2]
我的目标是学习如何执行 execle() 函数。我在 Head First C 书中找到了这段代码。 这是主进程代码(diner_info.c): #include #include int ma
在一个项目上工作,当我调用 execl() 时它不工作。它在 fork 之后调用,应该重新执行当前文件。(参数在文件的前面声明): argument = argv[0]; int err =execl
我正在尝试用 C 语言重现此命令: ls | wc > output.txt 因此,为此,我编写了以下程序: #include #include #include #include #incl
我是一名优秀的程序员,十分优秀!