vfork.c

来自「klibc精简化的c程序库」· C语言 代码 · 共 46 行

C
46
字号
/* * usr/klibc/tests/vfork.c * * vfork is messy on most architectures.  Do our best to test it out. */#include <errno.h>#include <stdio.h>#include <unistd.h>#include <sys/types.h>#include <sys/wait.h>int main(int argc, char *argv[]){	pid_t f, rv;	int status;	f = vfork();	if (f == 0) {		printf("Child (%d)...\n", (int)getpid());		_exit(123);	} else if (f > 0) {		int err = 0;		printf("Parent (child = %d)\n", (int)f);		rv = waitpid(f, &status, 0);		if (rv != f) {			printf("waitpid returned %d, errno = %d\n",			       (int)rv, errno);			err++;		}		if (!WIFEXITED(status) || WEXITSTATUS(status) != 123) {			printf("Child process existed with wrong status %d\n",			       status);			err++;		}		return err;	} else {		printf("vfork returned %d, errno = %d\n",		       (int)f, errno);		return 127;	}}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?