Skip to content

运行了你的代码,没有成为僵尸进程。 #22

@bing15

Description

@bing15
    $pid = pcntl_fork();
	if( $pid > 0 ){
	    // 下面这个函数可以更改php进程的名称
		cli_set_process_title('php father process');
		
		// 返回值保存在$wait_result中
		// $pid参数表示 子进程的进程ID
		// 子进程状态则保存在了参数$status中
		// 将第三个option参数设置为常量WNOHANG,则可以避免主进程阻塞挂起,此处父进程将立即返回继续往下执行剩下的代码
		$wait_result = pcntl_waitpid( $pid, $status, WNOHANG );
		var_dump( $wait_result );
		var_dump( $status );
		echo "不阻塞,运行到这里".PHP_EOL;
		
	    // 让主进程休息60秒钟
	    sleep(60);
		
	} else if( 0 == $pid ) {
	    cli_set_process_title('php child process');
		// 让子进程休息10秒钟,但是进程结束后,父进程不对子进程做任何处理工作,这样这个子进程就会变成僵尸进程
		sleep(10);
	} else {
	    exit('fork error.'.PHP_EOL);
	}

我运行试了这段代码,没有成为僵尸进程,是怎么回事。
image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions