C 带fork代码的perror(“wait”)放在哪里

C 带fork代码的perror(“wait”)放在哪里,c,fork,C,Fork,只有当您知道某个函数出现故障并且 设置了errno,因此您希望打印错误消息。你通常写 perror(“失败的东西”)就在可能失败的函数之后和 设置errno(函数的文档将告诉您 故障时的功能设置errno) 曼佩罗 简介 $ gcc -Wall above.c $ ./a.out hello world hello world hello world hello world hello world hello world hello world hello world hello world h

只有当您知道某个函数出现故障并且 设置了
errno
,因此您希望打印错误消息。你通常写
perror(“失败的东西”)
就在可能失败的函数之后 设置
errno
(函数的文档将告诉您 故障时的功能设置
errno

曼佩罗

简介

$ gcc -Wall above.c
$ ./a.out
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
^C (until ctrl C is hit)
$
   #include <stdio.h>

   void perror(const char *s);

   #include <errno.h>
说明

所有这些系统调用都用于等待调用进程的子进程中的状态更改。。。

wait()
waitpid()

wait()
系统调用将暂停调用进程的执行,直到其一个子进程终止。调用
wait(&wstatus)
失败 相当于:

   #include <sys/types.h>
   #include <sys/wait.h>

   pid_t wait(int *wstatus);

   pid_t waitpid(pid_t pid, int *wstatus, int options);

返回值

wait()
:成功时,返回终止子进程的进程ID;出现错误时,返回-1

在发生错误时,每个调用都会将
errno
设置为适当的值

如果您只想等待孩子退出,您可以只执行
wait(NULL)
。 但是,如果您想知道退出的子级的状态,则必须 将指针传递到
int

       waitpid(-1, &wstatus, 0);
我个人更喜欢
waitpid
而不是
wait
,它可以让您更好地控制 你在等的孩子


请参见

只有当您知道某个函数出现故障且 设置了
errno
,因此您希望打印错误消息。你通常写
perror(“失败的东西”)
就在可能失败的函数之后 设置
errno
(函数的文档将告诉您 故障时的功能设置
errno

曼佩罗

简介

$ gcc -Wall above.c
$ ./a.out
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
^C (until ctrl C is hit)
$
   #include <stdio.h>

   void perror(const char *s);

   #include <errno.h>
说明

所有这些系统调用都用于等待调用进程的子进程中的状态更改。。。

wait()
waitpid()

wait()
系统调用将暂停调用进程的执行,直到其一个子进程终止。调用
wait(&wstatus)
失败 相当于:

   #include <sys/types.h>
   #include <sys/wait.h>

   pid_t wait(int *wstatus);

   pid_t waitpid(pid_t pid, int *wstatus, int options);

返回值

wait()
:成功时,返回终止子进程的进程ID;出现错误时,返回-1

在发生错误时,每个调用都会将
errno
设置为适当的值

如果您只想等待孩子退出,您可以只执行
wait(NULL)
。 但是,如果您想知道退出的子级的状态,则必须 将指针传递到
int

       waitpid(-1, &wstatus, 0);
我个人更喜欢
waitpid
而不是
wait
,它可以让您更好地控制 你在等的孩子