我从waitpid得到错误的退出代码,我无法弄清楚原因.有人可以给我一些想法吗?
我在这做什么:
open2waitpid$?从子进程返回的内容总是以-1为单位返回.我检查VS调试器我的程序返回退出代码0. VS说这样的事情:
The program '[3256] Test.exe: Native' has exited with code 0 (0x0).
Run Code Online (Sandbox Code Playgroud)
我确保pids匹配.
有任何想法吗?
假设我在子进程和父进程之间创建了一个管道并且子进程正常结束,子进程的管道会自动关闭吗?
另外,如果子进程也有子进程并且子进程以分段错误结束,它也会杀死我的孙子进程吗?我的意思是从进程表中删除它(我不需要等待它)。
编辑:例如,对于以下代码,我在子进程中生成分段错误并尝试在父进程中等待它。运行程序后,waitpid返回-1,但是当我检查WIFEXITED(status)时,子进程程序似乎正常退出。我得到了一个
杀死子进程失败:没有这样的进程
错误尝试杀死我的孙子进程。我想知道这是不是因为分段错误会自动关闭子进程和孙进程?
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>
int main( void ) {
pid_t childpid;
int ends[ 2 ];
pipe( ends );
if ( ( childpid = fork() ) == -1 ) {
perror( "fork failed" );
exit( 0 );
}
if( childpid == 0 ) {
pid_t cpid;
if ( ( cpid = fork() ) == -1 ) {
perror( "fork failed" );
exit( …Run Code Online (Sandbox Code Playgroud) 从我读到的wait/的默认行为waitpid是等待进程中的状态更改。我找不到的是waitpid使用相同pid_t参数的两个进程的预期行为。
两者都返回并继续执行,还是只有一个人注意到状态变化的竞争条件?
我总是听说你永远不应该使用system(),而是fork/exec因为 system() 阻止了父进程。
如果是这样,我是否通过调用 做错了什么waitpid(),这也会在我执行 a 时阻止父进程fork/exec?有没有办法绕过调用waitpid...我一直认为在执行fork/exec.
pid_t pid = fork();
if (pid == -1)
{
// failed to fork
}
else if (pid > 0)
{
int status;
waitpid(pid, &status, 0);
}
else
{
execve(...);
}
Run Code Online (Sandbox Code Playgroud) 希望是一个简单的问题.我正在尝试同时学习fork(),pipe()和waitpid()并遇到一些问题.
if (pipe(myout)<0 || pipe(myin)<0 || pipe(myerr)<0) { perror("Couldn't make pipes"); return; }
int childpid=fork();
if (childpid==0) { //child
fdopen(myout[1], "w");
fdopen(myin[1], "r");
fdopen(myerr[1], "w");
dup2(myout[1], 1);
dup2(myin[1], 0);
dup2(myerr[1], 2);
printf("This should be seen\n");
fclose(stdout); fclose(stdin); fclose(stderr);
sleep(10);
_exit(0);
} else { //parent, wait on child
printf("parent, monitoring\n");
sim_out=fdopen(myout[0], "r");
sim_in=fdopen(myin[0], "w");
sim_err=fdopen(myerr[0], "r");
printf("have my fds\n");
int status;
do {
int ch;
if (read(myout[0], &ch, 1)>0)
write(1, &ch, 1);
else printf("no go\n");
waitpid(childpid, &status, WNOHANG);
} while (!WIFEXITED(status) …Run Code Online (Sandbox Code Playgroud) 我有这段代码,也许我错过了一些东西:
const int NPROCESSES = 32;
pid_t pids[128];
for (int i = 0; i < NPROCESSES;i ++) {
pids[i] = fork();
if (!pids[i]) {
/*... other code ...*/
exit(0);
}
}
for (int i = 0; i < NPROCESSES; i++)
waitpid(pids[i], 0, 0);
Run Code Online (Sandbox Code Playgroud)
该程序应启动 32 个进程并等待所有进程终止。但有时程序会被子僵尸进程阻塞。我哪里错了?
我想yes | head -n 1在 Rust 中实现,正确连接管道并检查退出状态:即,我希望能够确定由于yes退出SIGPIPE和head正常完成而退出。管道功能很简单(Rust Playground):
use std::process;
fn a() -> std::io::Result<()> {
let child_1 = process::Command::new("yes")
.arg("abracadabra")
.stdout(process::Stdio::piped())
.spawn()?;
let pipe: process::ChildStdout = child_1.stdout.unwrap();
let child_2 = process::Command::new("head")
.args(&["-n", "1"])
.stdin(pipe)
.stdout(process::Stdio::piped())
.spawn()?;
let output = child_2.wait_with_output()?;
let result = String::from_utf8_lossy(&output.stdout);
assert_eq!(result, "abracadabra\n");
println!("Good from 'a'.");
Ok(())
}
Run Code Online (Sandbox Code Playgroud)
但是虽然我们可以child_2在任何时候等待,但pipe移动的声明
child_1,所以不清楚如何等待child_1。如果我们只是在child_1.wait()?之前添加assert_eq!,我们会遇到编译时错误:
error[E0382]: borrow of moved value: `child_1` …Run Code Online (Sandbox Code Playgroud) 由于某种原因,此代码立即执行父命令,终止我的信号量并搞砸了我对其他程序的流控制.谁能告诉我为什么waitpid()不起作用?
//Create child processes
pid = fork();
if(pid < 0){
fprintf(stderr, "Fork Failed.\n");
exit(1);
return;
}else if(pid==0){
if(execl("/home/tropix/hw11-2","/home/tropix/hw11-2",semarg,pipe_to_p3,pipe_to_p4,(char*)0)){
fprintf(stderr, "File Exexecution of hw11-2 failed.\n");
exit(1);
}
} else {
pid = fork();
if(pid < 0){
fprintf(stderr, "Fork Failed.\n");
exit(1);
return;
} else if(pid==0){
if(execl("/home/tropix/hw11-3","/home/tropix/hw11-3",shmarg,semarg,pipe_from_p2,pipe_to_p5_1, (char*)0)){
fprintf(stderr, "File Execution of hw11-3 failed.\n");
exit(1);
}
} else {
pid = fork();
if(pid < 0){
fprintf(stderr, "Fork Failed.\n");
exit(1);
return;
} else if (pid == 0){
if(execl("/home/tropix/hw11-4","/home/tropix/hw11-4",shmarg,semarg,pipe_from_p2_2,pipe_to_p5_2, (char*)0)){
fprintf(stderr, "File Execution of hw11-4 …Run Code Online (Sandbox Code Playgroud) 如果我使用 execl 是可能需要时间完成的子进程,我什么时候需要使用 waitpid?
当我在父级中使用 waitpid 时,它让我的孩子运行,因为 waitpid 的返回值为 0。一段时间后,我试图在另一个函数中使用 waitpid,它用 ECHILD 返回 -1。如果我不确定天气孩子是否完成,我应该什么时候使用 waitpid?
//pid_t Checksum_pid = fork();
Checksum_pid = fork();
if (Checksum_pid == 0)
{
execl(path, name, argument as needed, ,NULL);
exit(EXIT_SUCCESS);
}
else if (Checksum_pid > 0)
{
pid_t returnValue = waitpid(Checksum_pid, &childStatus, WNOHANG);
if ( returnValue > 0)
{
if (WIFEXITED(childStatus))
{
printf("Exit Code: _ WEXITSTATUS(childStatus)") ;
}
}
else if ( returnValue == 0)
{
//Send positive response with routine status running (0x03)
printf("Child process …Run Code Online (Sandbox Code Playgroud) 在学习分叉和管道时,我遇到了以下优秀教程:https://www.cs.rutgers.edu/~pxk/416/notes/c-tutorials/pipe.html
但是,本教程将讨论如何在最后一个父级生成的2个子进程之间建立管道.虽然它做得很好,但是某段代码让我困惑了一段时间:
while ((pid = wait(&status)) != -1) /* pick up all the dead children*/
fprintf(stderr, "process %d exits with %d\n", pid, WEXITSTATUS(status));
exit(0);
Run Code Online (Sandbox Code Playgroud)
我真的很困惑wait(&status)(是的,我已经阅读了http://linux.die.net/man/2/wait上的手册页).我们只是声明一个int status,从来没有给它一个值,只是把它传递给等待.此状态是否在wait()功能中透明设置?
手册页说:
wait():成功时,返回已终止子进程的进程ID; 出错时,返回-1.
所以在上面的代码行中,while循环在wait(&status)返回-1 时退出.这很不耐烦:有错误,为什么?我们如何确保父母一直在旋转,直到所有孩子都正常终止?无论如何,'状态'是什么,它是如何设定的?
编辑:要添加,该程序确实编译和运行完美.
我想用 fork() 创建 3 个子进程。这是我创建一个子进程的代码:
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
void main(){
int pid = fork();
if(pid < 0){
/* was not successfully */
}
else if (pid > 0){
/* Parent process */
}
else{
/* Child process */
for (int i = 0; i < 20; i++){
printf("1");
usleep(1000);
}
exit(0);
}
}
Run Code Online (Sandbox Code Playgroud)
子进程应该打印 20 次数字 1,并在每次后休眠 1 毫秒。
我知道我不能只使用 fork() 3 次,因为那样我将得到 7 个子进程。但我怎样才能得到正好3呢?我怎样才能做到每个子进程都打印另一个数字?例如,第一个进程编号为 1,第二个进程编号为 2,第三个进程编号为 3。
父进程应该使用 waitpid() 来等待所有 3 个子进程。如果他们完成了,家长应该打印一条消息。但是这里如何使用waitpid呢?