Tag: 等待

父尝试读取子退出状态(或返回值),fork和wait

我糊涂了。 据说,基于man和许多其他来源,如下所示: 当操作系统终止你的进程等待(和状态) 时返回代码应该能让我获得子进程的退出状态或返回值吗? 那两段代码,应该让我看到它的孩子的退出值,然后打印出来。 子进程函数: int childFunction(char in[],char logPath[]){ FILE *logFile= fopen( logPath, “a” ); if(logFile==NULL) return 1; int c=system(in); fclose(logFile); return(c); } 主要: {…some unimportant code before this} result= fork(); if(result==0){ exit(childFunction(inLine,logPath)); } else if(result>0){ int status=0;; int c=(int)waitpid(-1,&status,0); printf(“a:%db:%d\n”,status, WIFEXITED(status)); else return -1; i=0; 我试着等待,睡了一段时间,退出,返回,并阅读手册页几次。 在我对这个function的理解中存在一个基本错误,或者在看了4个小时后我再也看不到它了。 已解决由于某些原因,我绝对不明白,如果你将childFunction中的return(c)更改为if(c!=)return(1);else return(0)它将起作用。 不知道为什么。 已解决2好的,现在我想我知道为什么。 看,似乎返回调用或等待将状态减少到8个最高有效位(256)。 那是什么意思? […]

使用条件变量和互斥量来同步C中的线程

根据评论者的要求编辑。 该程序创建两个线程。 每个线程从两个特定输入文件中的一个读取,每个输入文件包含一行字母或每行代码一个’0’。 线程应该将字母读入全局字符数组,然后打印出来。 问题是,在达到’0’时,活动线程必须将控制转移到另一个线程,该线程在该线路上不应该为’0’。 (我们确信,如果文件1在一行上有一个’0’,那么相应行上的文件2就有一个字母。多个零可以互相跟随,多个字母也可以。) 文件一 h 0 h 0 h 0 h 0 h 0 文件二 0 i 0 i 0 i 0 i 0 i 我正在尝试使用pthread互斥锁定/解锁以及信号并等待使其工作。 但是,我一直处于僵局状态。 有两个线程。 目前,它们彼此镜像意味着它们执行相同的操作,只是使用不同的文件和相反的条件。 线程示例: char final[1001]; pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER; pthread_cond_t condition1 = PTHREAD_COND_INITIALIZER; pthread_cond_t condition2 = PTHREAD_COND_INITIALIZER; int w = 1; void *get() { //start […]

c等待stdin读?

在我的应用程序中,我试图实现这样的事情: 我有: 数据0,数据1,数据2,…数据n。 一些参数传递 流: 使用一些参数启动程序并将数据0写入stdin 程序根据传递的数据“数据0”和参数进行计算 “等待”新的stdin和(清除旧的stdin,缓冲区和变量?) 当我输入数据1,数据2 ……等时重复1~2 当到达数据n时,终止(或者如果我输入一个中止代码到stdin告诉程序终止)。 也许是这样的?(伪代码): int main(int argc, char *argv[]) { get parameters(); int fslen = data size char *c = (char *)malloc(fslen); fgets(c, fslen, stdin); while((c != null) || (c != Terminate code?)) { do calculations with int c; clear c; } return 0; } 或者他们是一个更好的方法? 或者这样做是不好的做法? […]

如何在父母继续前等待所有孩子终止?

我正在做一些并行编程(多处理),我需要父代: 1) Fork几个孩子 2)在创建所有孩子之后,只需等待所有孩子终止 3)在所有孩子被终止后,做一些其他的工作。 这是我试过的: int main(int argc, char **argv[]) { int j, i; pid_t children[3]; int pid; // Fork the processes for(j = 0; j < 3; j++){ if((children[j] = fork()) == 0){ // Child process for(i = 0; i < 2; i++){ printf("child %d printing: %d\n", j, i); } }else { // […]

等待信号,然后继续执行

我正在尝试制作一个暂停执行的程序, 直到信号到达 。 然后,在信号到达后,我只想让我的代码从原来的位置继续执行 。 我不希望它执行函数处理程序或任何其他。 有一个简单的方法吗? 我已经挣扎了一个星期左右,在这里和那里阅读,并没有设法得到一个完整的操作代码。 特别是,我希望主程序创建一个等待某个特定事件发生的线程 (例如,用户已经向stdin输入了一些数据)。 与此同时,主程序正在做一些事情,但在某些时候它会暂停执行,直到收到信号。 信号可能来自线程,因为它已检测到事件,或者可能是由于超时,因为我不希望它等待。 我已经制作了一些代码,但它没有按预期工作…… /* * This code SHOULD start a thread that gets messages from stdin. * If the message is a “quit”, the thread exits. Otherwise it raises * a signal that should be caught by the main program. * The main program simply […]

从execv()中获取返回值

//code for foo (run executable as ./a.out) #include #include #include #include #include int main (int argc, char **argv) { pid_t pid; pid = fork(); int i = 1; char *parms[] = {“test2”, “5”, NULL}; //test executable named test2 if(pid < 0) { fprintf(stderr, "Fork failed"); return 1; } else if(pid == 0) { printf("Child pid […]

如何等待线程完成他们的工作,在c中克隆创建的线程?

我试着等待主要function,直到线程完成他们的工作。 但主要function完成其工作并退出。 我认为因为线程在变量中没有正确的指针/值。(理货和步骤) 在这种情况下,有人知道如何正确使用waitpid / wait吗? 我的代码: #define _GNU_SOURCE #include #include /* for PRIu64 and uint64_t */ /* you’ll need further includes */ #include #include #include “tally.h” #include #include #include #define STACK_SIZE 32768 #define THREADS 2 struct clone_args{ uint64_t steps; uint64_t* tally; }; int incHelper(void* arg){ struct clone_args *args = (struct clone_args*)arg; uint64_t steps= args->steps; […]

只需检查c中的状态过程

我想知道一个过程的状态。 我想我可以使用等待系列function,但实际上我不想等待这个过程,只需检查状态并继续。 我想要类似的东西 checkStatusOfProcess(&status); if(status == WORKING) { //do something } else if(status == exited) { //do something else } else \\I dont care about other states

如何使用Fork()只创建2个子进程?

我开始学习一些C并且在学习fork时,等待函数我得到了意想不到的输出。 至少对于我来说。 有没有办法只从父级创建2个子进程? 这是我的代码: #include #include #include #include int main () { /* Create the pipe */ int fd [2]; pipe(fd); pid_t pid; pid_t pidb; pid = fork (); pidb = fork (); if (pid < 0) { printf ("Fork Failed\n"); return -1; } else if (pid == 0) { //printf("I'm the child\n"); } else […]