⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 wait.c

📁 在UNIX/LINUX下面挂起一个进程并在SHELL下观察进程.
💻 C
字号:
#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <sys/wait.h>/* Spawn a child process running a new program. PROGRAM is the name    of the program to run; the path will be searched for this program.    ARG_LIST is a NULL-terminated list of character strings to be    passed as the program's argument list. Returns  the process ID of    the spawned process.  */  int spawn (char* program, char** arg_list) {  pid_t child_pid;    /* Duplicate this process. */   child_pid = fork ();   if (child_pid != 0)     /* This is the parent process. */     return child_pid;   else {    /* Now execute PROGRAM, searching for it in the path.  */     execvp (program,  arg_list);     /* The execvp  function returns only if an error occurs.  */     fprintf (stderr,  "pid: %d, an error occurred in execvp\n", (int)getpid());     abort ();   } } int main  () {  int child_status;    /* The argument list to pass to the "ls" command.  */   char* arg_list[] = {    "ls",     /* argv[0], the name of the program.  */     "-l",     "/",     NULL /*  The  argument list must end with a NULL.  */   };    /* Spawn a child process running the "ls" command. Ignore the      returned child process ID.  */   spawn ("ls", arg_list);    /* Wait for the child process to complete.  */   wait (&child_status);   if (WIFEXITED (child_status))     printf ("the child process exited normally, with exit code %d\n",             WEXITSTATUS (child_status));   else     printf ("the child process exited abnormally\n");    return  0; } 

⌨️ 快捷键说明

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