在C中等待系统调用

先决条件: Fork系统调用 调用wait()会阻止调用进程,直到它的一个子进程退出或收到信号。子进程终止后,父进程 继续 它在等待系统调用指令后执行。 子进程可能因以下任何原因而终止:

null
  • 它调用exit();
  • 它从main返回(一个int)
  • 它接收一个信号(来自操作系统或另一个进程),其默认操作是终止。

图片[1]-在C中等待系统调用-yiteyi-C++库

c语言中的语法:

#include#include// take one argument status and returns // a process ID of dead children.pid_t wait(int *stat_loc);   

如果任何进程有多个子进程,那么在调用wait()之后,如果没有子进程终止,则父进程必须处于等待状态。 如果只有一个子进程被终止,那么return a wait()返回终止的子进程的进程ID。 如果终止了多个子进程,则不必等待任何子进程 独断专行的孩子 并返回该子进程的进程ID。 当wait()返回时,它们还定义 退出状态 (这告诉我们,一个进程为什么终止)通过指针,如果状态不是 无效的 . 如果任何进程没有子进程,那么wait()立即返回“-1”。 注意:“由于环境问题,这些代码不能在简单的IDE中运行,所以请使用终端来运行代码” 例如:

CPP

// C program to demonstrate working of wait()
#include<stdio.h>
#include<stdlib.h>
#include<sys/wait.h>
#include<unistd.h>
int main()
{
pid_t cpid;
if (fork()== 0)
exit (0); /* terminate child */
else
cpid = wait(NULL); /* reaping parent */
printf ( "Parent pid = %d" , getpid());
printf ( "Child pid = %d" , cpid);
return 0;
}


输出:

Parent pid = 12345678 Child pid = 89546848 

C

// C program to demonstrate working of wait()
#include<stdio.h>
#include<sys/wait.h>
#include<unistd.h>
int main()
{
if (fork()== 0)
printf ( "HC: hello from child" );
else
{
printf ( "HP: hello from parent" );
wait(NULL);
printf ( "CT: child has terminated" );
}
printf ( "Bye" );
return 0;
}


输出: 依赖环境

HC: hello from childByeHP: hello from parentCT: child has terminated     (or)HP: hello from parentHC: hello from childHC: ByeCT: child has terminated    // this sentence does                             // not print before HC                             // because of wait.Bye

儿童状态信息: wait报告的孩子的状态信息不仅仅是孩子的退出状态,还包括

  • 正常/异常终止
  • 终止原因
  • 退出状态

要查找有关状态的信息,我们使用 WIF ….宏 1. 妻子退出(身份) :儿童正常退出 • WEXIT状态(状态) :子进程退出时返回代码 2. WIFSIGNALED(状态) :孩子退出,因为没有捕捉到信号 • WTERMSIG(状态) :给出终止信号的编号 3. WIFSTOPPED(状态) :孩子被拦住了 • WSTOPSIG(状态) :给出停止信号的编号

/*if we want to prints information about a signal */void psignal(unsigned sig, const char *s);

例如: 检查以下程序的输出。

C

// C program to demonstrate working of status
// from wait.
#include<stdio.h>
#include<stdlib.h>
#include<sys/wait.h>
#include<unistd.h>
void waitexample()
{
int stat;
// This status 1 is reported by WEXITSTATUS
if (fork() == 0)
exit (1);
else
wait(&stat);
if (WIFEXITED(stat))
printf ( "Exit status: %d" , WEXITSTATUS(stat));
else if (WIFSIGNALED(stat))
psignal(WTERMSIG(stat), "Exit signal" );
}
// Driver code
int main()
{
waitexample();
return 0;
}


输出:

Exit status: 1              

我们知道如果终止了多个子进程,那么wait()将获取任意子进程,但是如果我们想要获取任何特定的子进程,我们使用 waitpid() 作用

c语言中的语法: pid_t waitpid(子pid和状态、选项);

选项参数

  • 如果0表示没有选项,则父级必须等待子级。
  • 如果 WNOHANG 表示如果子进程不终止,父进程不等待,只需检查并返回waitpid()。(不阻止父进程)
  • 如果孩子是 -1 那就意味着任何 独断专行的孩子 这里的waitpid()工作与wait()工作相同。

waitpid()的返回值

  • 孩子的pid,如果孩子已经退出
  • 0,如果使用WNOHANG且子项尚未退出。

C

// C program to demonstrate waitpid()
#include<stdio.h>
#include<stdlib.h>
#include<sys/wait.h>
#include<unistd.h>
void waitexample()
{
int i, stat;
pid_t pid[5];
for (i=0; i<5; i++)
{
if ((pid[i] = fork()) == 0)
{
sleep(1);
exit (100 + i);
}
}
// Using waitpid() and printing exit status
// of children.
for (i=0; i<5; i++)
{
pid_t cpid = waitpid(pid[i], &stat, 0);
if (WIFEXITED(stat))
printf ( "Child %d terminated with status: %d" ,
cpid, WEXITSTATUS(stat));
}
}
// Driver code
int main()
{
waitexample();
return 0;
}


输出:

Child 50 terminated with status: 100Child 51 terminated with status: 101Child 52 terminated with status: 102Child 53 terminated with status: 103Child 54 terminated with status: 104

在这里,子PID取决于系统,但为了打印所有子信息。 本文由 卡丹·帕特尔 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 贡献极客。组织 或者把你的文章寄到contribute@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

© 版权声明
THE END
喜欢就支持一下吧
点赞11 分享