标签:top 编程 返回 puts 变量 函数 his pass local
/****** * waitpid.c - Simple wait usage *********/ #include <unistd.h> #include <sys/types.h> #include <sys/wait.h> #include <stdio.h> #include <stdlib.h> int main( void ) { pid_t childpid; int status; childpid = fork(); if ( -1 == childpid ) { perror( "fork()" ); exit( EXIT_FAILURE ); } else if ( 0 == childpid ) { puts( "In child process" ); sleep( 3 );//让子进程睡眠3秒,看看父进程的行为 printf("\tchild pid = %d\n", getpid()); printf("\tchild ppid = %d\n", getppid()); exit(EXIT_SUCCESS); } else { waitpid( childpid, &status, 0 ); puts( "in parent" ); printf( "\tparent pid = %d\n", getpid() ); printf( "\tparent ppid = %d\n", getppid() ); printf( "\tchild process exited with status %d \n", status ); } exit(EXIT_SUCCESS); }
#include <unistd.h> #include <stdio.h> int main(int argc, void ** argv ) { int pid = fork(); if(pid < 0 ) { // print("error!"); } else if( pid == 0 ) { // print("This is the child process!"); } else { // print("This is the parent process! child process id = %d", pid); } return 0; }
/* 执行ls -al /etc/passwd execlp()会依PATH 变量中的/bin找到/bin/ls */ #include<unistd.h> main() { execlp(“ls”,”ls”,”-al”,”/etc/passwd”,(char *)0); }
Linux环境编程--waitpid与fork与execlp
标签:top 编程 返回 puts 变量 函数 his pass local
原文地址:http://www.cnblogs.com/mingfeng002/p/6905007.html