`
memorymyann
  • 浏览: 266549 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

wait4的使用

阅读更多

wait4会挂起当前进程,等待指定的子进程状态改变,并且会返回关于子进程使用资源的信息。所谓的状态的改变包括终止,挂起所有的进程状态的改变。
其次,在linux中进程终止后不会释放其资源,它会进入一种僵死的状态,内核会为僵死态的进程保留最少的信息(进程标识,终止状态和资源使用信息).而wait4就可以释放与子进程关联的资源。如果父进程不执行wait,而父进程又死亡后,僵死态的子进程会被指定成系统初始化进程init的子进程.下面是wait4使用方法:
#include <sys/types.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/wait.h>
pid_t wait4(pid_t pid, int *status, int options, struct rusage *rusage);
pid是要关注的子进程的pid(进程标识号)
status子进程的返回状态。
options进程等待选项
rusage死亡进程资源使用记录

pid这个很好理解,进程标识号,也是你ps -le显示出来的pid。如果指定了pid则会在指定pid发生变化后唤醒父进程,此外还有一下4种情况:
< -1   which means to wait for any child process whose process group ID is equal to the absolute value of pid.
-1     which means to wait for any child process; this is equivalent to calling wait3.
0      which means to wait for any child process whose process group ID is equal to that of the calling process.
> 0    which means to wait for the child whose process ID is equal to the value of pid.

status子进程的返回状态,这是个用于描述进程返回状态的整数,有各种宏操作可以查看,如下:
WIFEXITED(status)
    is non-zero if the child exited normally.

WEXITSTATUS(status)
    evaluates  to the least significant eight bits of the return code of the child which terminated, which may have been set as the argument to a call
to exit() or as the argument for a return statement in the main program.  This macro can only be evaluated if WIFEXITED returned non-zero.

WIFSIGNALED(status)
    returns true if the child process exited because of a signal which was not caught.

WTERMSIG(status)
    returns the number of the signal that caused the child process to terminate. This macro can only be evaluated if WIFSIGNALED returned non-zero.

WIFSTOPPED(status)
    returns true if the child process which caused the return is currently stopped; this is only possible if the call was done using WUNTRACED.

WSTOPSIG(status)
    returns the number of the signal which caused the child to stop.  This macro can only be evaluated if WIFSTOPPED returned non-zero.

options进程等待选项,当为WNOHANG表示立即返回,而当WUNTRACED表示等子进程状态发生变化后才返回。

最后rusage记录死亡进程资源使用信息,这个数据结构定义如下:
struct rusage {
    struct timeval ru_utime; /* user time used */
    struct timeval ru_stime; /* system time used */
    long   ru_maxrss;        /* maximum resident set size */
    long   ru_ixrss;         /* integral shared memory size */
    long   ru_idrss;         /* integral unshared data size */
    long   ru_isrss;         /* integral unshared stack size */
    long   ru_minflt;        /* page reclaims */
    long   ru_majflt;        /* page faults */
    long   ru_nswap;         /* swaps */
    long   ru_inblock;       /* block input operations */
    long   ru_oublock;       /* block output operations */
    long   ru_msgsnd;        /* messages sent */
    long   ru_msgrcv;        /* messages received */
    long   ru_nsignals;      /* signals received */
    long   ru_nvcsw;         /* voluntary context switches */
    long   ru_nivcsw;        /* involuntary context switches */
};

使用样例
[root@liumengli wait]# cat print.c
#include "stdio.h"

int main() {
        int i;
        for (i = 0; i < 50; i++)
                printf("Hello, This is print!!!\n");
        exit(0);
}
[root@liumengli wait]# gcc print.c -o print
[root@liumengli wait]# cat test_wait.c
#include "stdio.h"
#include <sys/types.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/wait.h>

int main() {
        int child;
        int * status;
        struct rusage  * rus;
        char * argu[] = {"/program/wait/print", NULL};
        if(!(child = vfork())) {
                execve("/program/wait/print", argu, NULL);
        } else {
                wait4(child,status,WUNTRACED,rus);
                printf("The son's pid:%d, status:%d, The memory is:%d\n", child, WIFEXITED(status),rus->ru_nswap);
        }
        exit(0);
}
[root@liumengli wait]# gcc test_wait.c -o test_wait
[root@liumengli wait]# ./test_wait
Hello, This is print!!!
Hello, This is print!!!
Hello, This is print!!!
Hello, This is print!!!
.......
Hello, This is print!!!
Hello, This is print!!!
Hello, This is print!!!
Hello, This is print!!!
The son's pid:3237, status:0, The memory is:-1996293951
[root@liumengli wait]#

分享到:
评论

相关推荐

    Pro.SQL.Server.Wait.Statistics.1484211405

    Chapter 4: CPU-Related Wait Types Chapter 5: IO-Related Wait Types Chapter 6: Backup-Related Wait Types Chapter 7: Lock-Related Wait Types Chapter 8: Latch-Related Wait Types Chapter 9: High-...

    Python库 | wait4it-0.2.1.tar.gz

    资源分类:Python库 所属语言:Python 资源全名:wait4it-0.2.1.tar.gz 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059

    PyPI 官网下载 | wait4it-0.2.1-py3-none-any.whl

    资源来自pypi官网。 资源全名:wait4it-0.2.1-py3-none-any.whl

    fast-wait-free-queue, 并发队列实现的基准框架.zip

    fast-wait-free-queue, 并发队列实现的基准框架 快速等待空闲队列这是评估并发队列性能的基准测试框架。 目前,它包含4 个并发队列。 它们是:一个快速等待队列 wfqueueafek的Morrison和 lcrqkallimanis的Fatourou和...

    __lll_mutex_lock_wait的错误原因

    #0 0x00002b9405ea1c38 in __lll_mutex_lock_wait () from /lib64/libc.so.6 #1 0x00002b9405e45e5f in _L_lock_4026 () from /lib64/libc.so.6 #2 0x00002b9405e42df1 in free () from /lib64/libc.so.6 #3 0x00002...

    sleep()、wait()、yield()和join()方法特点及区别.docx

    4.sleep必须捕获异常,而wait,notify和notifyAll不需要捕获异常。(如果不是在同步方法或同步代码块中调用wait()方法,则抛出IllegalMOnitorStateException,它是RuntimeException的一个子类,因此,不需要try-...

    Delphi开发代码规范(修改).rar_Delphi 编程规范_wait4gw

    国内某软件公司文件:Delphi开发代码规范(修改),

    系统调优,你所不知道的TIME_WAIT和CLOSE_WAIT1

    1. 主动关闭连接的 - 也就是主动调socket的close操作的,最终 2. 被动关闭连接的,有个中间状态,即CLOSE_WAIT,因为协议 4. 在个连接

    wait4ports:一个等待网络对等可用性的小实用程序

    wait4ports 一个等待网络对等可用性的小... 如果设置为0,则不使用超时,并且wait4ports将永远等待。环境也可以通过环境变量启用选项: WAIT4PORTS_VERBOSE : 默认情况下,详细程度为打开状态,但是将此值设置为0 ,

    stm32f4通过spi用fatfs读写sd卡程序

    stm32f4通过spi用fatfs读写sd卡程序,已经调通。用的是单片机中B口的B11--CS、B13--SCLK、B14--MISO、B15--MOSI.

    wait4it:一个简单的go应用程序,用于测试端口或服务是否准备就绪

    wait4it 一个简单的go应用程序,用于测试端口是否已准备好接受连接或检查MySQL,PostgreSQL,MongoDB或Redis服务器是否已就绪。您还可以执行Http调用并检查响应代码和响应文本。 它还支持超时,因此可以等待特定...

    wait-module:轻量级模块加载器,用来加载构建js文件依赖

    4.只在设计上遵循了AMD规范,但是使用上却使用了类似CMD规范。我和我的团队都喜欢在require时暴露出过程,会比较灵活。可以伴随逻辑require,而不是预先require好资源再执行。 5.目前只支持到ie8。 6.支持加载css...

    UNIX期末试卷

    4、使用fork,exec以及wait函数构造简单的shell解释程序。(15分) (1) 假定所编辑的c程序为myxsh.c,使用命令行方式将该源程序编译,目标执行程序为myxsh。给出具体的编译实现过程。 (2) 假定当前目录下有文件信息...

    highres-timer:利用 mach_wait_until 的精确计时器

    利用 mach_wait_until 的精确计时器 要求 iOS:iOS 4 MacOS X:Mac OS X 10.6 演示 iOS 应用程序使用 Xcode 6.1 制作并针对 iOS 8 如果您在商业项目中使用它,请在 README 或 About Box 中提供署名。

    ASA防火墙最实用的排错思路和4种工具.doc

    思科推荐的排错流量: ①ping直连测试,如果不通,就检查接口配置。 ②查看路由,如果没有就写静态或者动态。...④使用Capture分别抓取收包和发包接口的数据,进行对比(ASA自带程序),这也是终极解决方案。

    TCP_CloseWaitProblem

    tcp4 0 0 127.0.0.1.50937 127.0.0.1.8383 CLOSE_WAIT 这次我们了解到的是,只有在应用程序显式调用close或终止应用程序时,才可以接收等待CLOSE_WAIT的关闭。 因此,如果您在这种CLOSE_WAIT状态下无限期地等待,...

    c++ linux系统下notify的使用

    1.notify的使用 2. wait的使用 3. linux系统下的开发 4.多线程通信 5. 生产者和消费者模式

    spin-wait-status-action

    uses : armory-io/spin-wait-status-action@master with : baseUrl : ' http://examplebaseUrl ' application : ' example-app ' eventId : ' 7ab2d981-430a-44e9-8f4e-77f96593e004 ' crtFile : ' SG

    drop-tcp-sock:此模块允许杀死TCP套接字(包括TIME-WAIT状态)

    0 该模块允许丢弃TCP连接,可用于终止TIME-WAIT套接字。 用法 首先编译并加载模块: ... # netstat -n -t | grep WAIT | awk '{print $4"\t"$5}' 127.0.0.1:41278 127.0.0.1:22 127.0.0.1:41276 127.0.0.1:22 127.0

Global site tag (gtag.js) - Google Analytics