Advanced Programming in UNIX Environment Episode 30

本文详细介绍了进程正常及异常终止的五种方式,并解释了三种异常终止情况。此外,还探讨了不同等待函数(如wait、waitpid和waitid)的特点及其应用场景,包括如何避免僵尸进程的产生。
exit Function

As we described in Section 7.3, a process can terminate normally in five ways:

1.Executing a return from the main function.
2.Calling the exit function.
3.Calling the _exit or _Exit function.
4.Executing a return from the start routine of the last thread in the process.
5.Calling the pthread_exit function from the last thread in the process.

The three forms of abnormal termination are as follows:

1.Calling abort. This is a special case of the next item, as it generates the
SIGABRT signal.
2.When the process receives certain signals.
3.The last thread responds to a cancellation request.

Regardless of how a process terminates, the same code in the kernel is eventually executed. This kernel code closes all the open descriptors for the process, releases the memory that it was using, and so on.

The final condition to consider is this: What happens when a process that has been inherited by init terminates? Does it become a zombie? The answer is ‘‘no,’’ because init is written so that whenever one of its children terminates, init calls one of the wait functions to fetch the termination status. By doing this, init prevents the system from being clogged by zombies.

wait and waitpid Functions

When a process terminates, either normally or abnormally, the kernel notifies the parent by sending the SIGCHLD signal to the parent. Because the termination of a child is an asynchronous event—it can happen at any time while the parent is running — this signal is the asynchronous notification from the kernel to the parent.

For now, we need to be aware that a process that calls wait or waitpid can

  • Block, if all of its children are still running
  • Return immediately with the termination status of a child, if a child has terminated and is waiting for its termination status to be fetched
  • Return immediately with an error, if it doesn’t have any child processes

If the process is calling wait because it received the SIGCHLD signal, we expect wait to return immediately. But if we call it at any random point in time, it can block.

#include <sys/wait.h>

pid_t wait(int *statloc);
pid_t waitpid(pid_t pid, int *statloc, int options);

The differences between these two functions are as follows:

  • The wait function can block the caller until a child process terminates, whereas waitpid has an option that prevents it from blocking.
  • The waitpid function doesn’t wait for the child that terminates first; it has a number of options that control which process it waits for.
#include "apue.h"
#include <sys/wait.h>

void pr_exit(int status)
{
    if(WIFEXITED(status))
        printf("normal termination, exit status = %d\n",
            WEXITSTATUS(status));
    else if(WIFSIGNAL(status))
        printf("abnormal termination, signal number = %d%s\n",
            WTERMSIG(status),

#ifdef WCOREDUMP
            WCOREDUMP(status)?" (core file generated)" : "");
#else
            "");
#endif
    else if(WIFSTOPPED(status))
        printf("child stopped, signal number=%d\n",
            WSTOPSIG(status));
}

Print a description of the exit status

FreeBSD 8.0, Linux 3.2.0, Mac OS X 10.6.8, and Solaris 10 all support the WCOREDUMP macro. However, some platforms hide its definition if the _POSIX_C_SOURCE constant is defined

#include "apue.h"
#include <sys/wait.h>

int main(void)
{
    pid_t pid;
    int status;

    if((pid=fork())<0)
        err_sys("fork error");
    else if(pid==0)
        exit(7);

    if(wait(&status)!=pid)
        err_sys("wait error");
    pr_exit(status);

    if((pid==fork())<0)
        err_sys("fork error");
    else if(pid==0)
        abort();

    if(wait(&status)!=pid)
        err_sys("wait error");
    pr_exit(status);

    if((pid=fork())<0)
        err_sys("fork error");
    else if(pid==0)
        status/=0;

    if(wait(&status)!=pid)
        err_sys("wait error");
    pr_exit(status);

    return 0;
}

Demonstrate various exit statuses

The interpretation of the pid argument for waitpid depends on its value:

pid == −1 Waits for any child process. In this respect, waitpid is equivalent to wait.
pid >0 Waits for the child whose process ID equals pid.
pid ==0 Waits for any child whose process group ID equals that of the calling process.
pid < −1 Waits for any child whose process group ID equals the absolute value of pid.

The waitpid function provides three features that aren’t provided by the wait function.

1.The waitpid function lets us wait for one particular process, whereas the wait function returns the status of any terminated child. We’ll return to this feature when we discuss the popen function.
2.The waitpid function provides a nonblocking version of wait. There are times when we want to fetch a child’s status, but we don’t want to block.
3.The waitpid function provides support for job control with the WUNTRACED and WCONTINUED options.

#include "apue.h"
#include <sys/wait.h>

int main(void)
{
    pid_t pid;
    if((pid==fork())<0)
    {
        err_sys("fork error");
    }
    else if(pid==0)
    {
        if((pid=fork())<0)
            err_sys("fork error");
        else if(pid>0)
            exit(0);

        sleep(2);
        printf("second child, parent pid=%ld\n",(long)getppid());
        exit(0);
    }

    if(waitpid(pid,NULL,0)!=pid)
        err_sys("waitpid error");

    return 0;
}

Avoid zombie processes by calling fork twice

waitid Function

The Single UNIX Specification includes an additional function to retrieve the exit status of a process. The waitid function is similar to waitpid, but provides extra flexibility.

#include <sys/wait.h>
int waitid(idtype_t idtype, id_t id, siginfo_t *infop, int options);

The infop argument is a pointer to a siginfo structure. This structure contains detailed information about the signal generated that caused the state change in the child process. The siginfo structure is discussed further in Section 10.14.

Of the four platforms covered in this book, only Linux 3.2.0, Mac OS X 10.6.8, and Solaris 10 provide support for waitid. Note, however, that Mac OS X 10.6.8 doesn’t set all the information we expect in the siginfo structure.

wait3 and wait4 Functions

Historically, these two variants descend from the BSD branch of the UNIX System. The only feature provided by these two functions that isn’t provided by the wait, waitid, and waitpid functions is an additional argument that allows the kernel to return a summary of the resources used by the terminated process and all its child processes.

#include <sys/types.h>
#include <sys/wait.h>
#include <sys/time.h>
#include <sys/resource.h>
pid_t wait3(int *statloc, int options, struct rusage *rusage);
pid_t wait4(pid_t pid, int *statloc, int options, struct rusage *rusage);

Race Conditions

For our purposes, a race condition occurs when multiple processes are trying to do something with shared data and the final outcome depends on the order in which the processes run. The fork function is a lively breeding ground for race conditions, if any of the logic after the fork either explicitly or implicitly depends on whether the parent or child runs first after the fork.

If the second child runs before the first child, then its parent process will be the first child. But if the first child runs first and has enough time to exit, then the parent process of the second child is init. Even calling sleep, as we did, guarantees nothing. If the system was heavily loaded, the second child could resume after sleep returns, before the first child has a chance to run.

For a parent and child relationship, we often have the following scenario. After the fork, both the parent and the child have something to do. For example, the parent could update a record in a log file with the child’s process ID, and the child might have to create a file for the parent. In this example, we require that each process tell the other when it has finished its initial set of operations, and that each wait for the other to complete, before heading off on its own.

The five routines TELL_WAIT, TELL_PARENT, TELL_CHILD, WAIT_PARENT, and WAIT_CHILD can be either macros or functions.

#include "apue.h"

static void charatatime(char *);

int main(void)
{
    pid_t pid;

    if((pid=fork())<0)
    {
        err_sys("fork error");
    }
    else if(pid==0)
    {
        charatatime("output from child\n");
    }
    else
    {
        charatatime("output from parent\n");
    }

    return 0;
}

static void charatatime(char *str)
{
    char *ptr;
    int c;

    setbuf(stdout, NULL);
    for(ptr=str;(c=*ptr++)!=0;)
    {
        putc(c,stdout);
    }
}

Program with a race condition

We need to change the program in Figure 8.12 to use the TELL and WAIT functions.

#include "apue.h"

static void charatatime(char *);

int main(void)
{
    pid_t pid;
    TELL_WAIT();

    if((pid=fork())<0)
    {
        err_sys("fork error");
    }
    else if(pid==0)
    {
        WAIT_PARENT();
        charatatime("output from child\n");
    }
    else
    {
        charatatime("output from parent\n");
        TELL_CHILD(pid);
    }

    return 0;
}

static void charatatime(char *str)
{
    char *ptr;
    int c;

    setbuf(stdout, NULL);
    for(ptr=str;(c=*ptr++)!=0;)
    {
        putc(c,stdout);
    }
}

Modification of Figure 8.12 to avoid race condition

### 光流法C++源代码解析与应用 #### 光流法原理 光流法是一种在计算机视觉领域中用于追踪视频序列中运动物体的方法。它基于亮度不变性假设,即场景中的点在时间上保持相同的灰度值,从而通过分析连续帧之间的像素变化来估计运动方向和速度。在数学上,光流场可以表示为像素位置和时间的一阶导数,即Ex、Ey(空间梯度)和Et(时间梯度),它们共同构成光流方程的基础。 #### C++实现细节 在给定的C++源代码片段中,`calculate`函数负责计算光流场。该函数接收一个图像缓冲区`buf`作为输入,并初始化了几个关键变量:`Ex`、`Ey`和`Et`分别代表沿x轴、y轴和时间轴的像素强度变化;`gray1`和`gray2`用于存储当前帧和前一帧的平均灰度值;`u`则表示计算出的光流矢量大小。 #### 图像处理流程 1. **初始化和预处理**:`memset`函数被用来清零`opticalflow`数组,它将保存计算出的光流数据。同时,`output`数组被填充为白色,这通常用于可视化结果。 2. **灰度计算**:对每一像素点进行处理,计算其灰度值。这里采用的是RGB通道平均值的计算方法,将每个像素的R、G、B值相加后除以3,得到一个近似灰度值。此步骤确保了计算过程的鲁棒性和效率。 3. **光流向量计算**:通过比较当前帧和前一帧的灰度值,计算出每个像素点的Ex、Ey和Et值。这里值得注意的是,光流向量的大小`u`是通过`Et`除以`sqrt(Ex^2 + Ey^2)`得到的,再乘以10进行量化处理,以减少计算复杂度。 4. **结果存储与阈值处理**:计算出的光流值被存储在`opticalflow`数组中。如果`u`的绝对值超过10,则认为该点存在显著运动,因此在`output`数组中将对应位置标记为黑色,形成运动区域的可视化效果。 5. **状态更新**:通过`memcpy`函数将当前帧复制到`prevframe`中,为下一次迭代做准备。 #### 扩展应用:Lukas-Kanade算法 除了上述基础的光流计算外,代码还提到了Lukas-Kanade算法的应用。这是一种更高级的光流计算方法,能够提供更精确的运动估计。在`ImgOpticalFlow`函数中,通过调用`cvCalcOpticalFlowLK`函数实现了这一算法,该函数接受前一帧和当前帧的灰度图,以及窗口大小等参数,返回像素级别的光流场信息。 在实际应用中,光流法常用于目标跟踪、运动检测、视频压缩等领域。通过深入理解和优化光流算法,可以进一步提升视频分析的准确性和实时性能。 光流法及其C++实现是计算机视觉领域的一个重要组成部分,通过对连续帧间像素变化的精细分析,能够有效捕捉和理解动态场景中的运动信息
微信小程序作为腾讯推出的一种轻型应用形式,因其便捷性与高效性,已广泛应用于日常生活中。以下为该平台的主要特性及配套资源说明: 特性方面: 操作便捷,即开即用:用户通过微信内搜索或扫描二维码即可直接使用,无需额外下载安装,减少了对手机存储空间的占用,也简化了使用流程。 多端兼容,统一开发:该平台支持在多种操作系统与设备上运行,开发者无需针对不同平台进行重复适配,可在一个统一的环境中完成开发工作。 功能丰富,接口完善:平台提供了多样化的API接口,便于开发者实现如支付功能、用户身份验证及消息通知等多样化需求。 社交整合,传播高效:小程序深度嵌入微信生态,能有效利用社交关系链,促进用户之间的互动与传播。 开发成本低,周期短:相比传统应用程序,小程序的开发投入更少,开发周期更短,有助于企业快速实现产品上线。 资源内容: “微信小程序-项目源码-原生开发框架-含效果截图示例”这一资料包,提供了完整的项目源码,并基于原生开发方式构建,确保了代码的稳定性与可维护性。内容涵盖项目结构、页面设计、功能模块等关键部分,配有详细说明与注释,便于使用者迅速理解并掌握开发方法。此外,还附有多个实际运行效果的截图,帮助用户直观了解功能实现情况,评估其在实际应用中的表现与价值。该资源适用于前端开发人员、技术爱好者及希望拓展业务的机构,具有较高的参考与使用价值。欢迎查阅,助力小程序开发实践。资源来源于网络分享,仅用于学习交流使用,请勿用于商业,如有侵权请联系我删除!
### Python 中循环 Episode 的代码片段 在 Python 编程中,`for` 循环常用于遍历一系列的迭代对象,比如列表、元组或范围。当涉及到模拟多个 episode(例如,在强化学习环境中),可以使用 `range()` 函数来指定要执行的次数。 下面是一个简单的例子,展示了如何设置一个 for 循环来进行多次 episode 运行: ```python num_episodes = 10 # 定义想要运行的episode数量 for episode in range(num_episodes): # 使用range()函数创建从0到num_episodes-1的序列 print(f"Starting episode {episode + 1}") # 输出当前是第几个episode # 此处放置每轮episode的具体逻辑操作... print(f"Finished episode {episode + 1}\n") # 结束本轮后打印完成信息 ``` 在这个例子中,变量 `num_episodes` 设置了总的 episode 数目;而 `range(num_episodes)` 则会生成一个由整数组成的不可变序列,这些整数是从零开始直到 `num_episodes - 1` 。每次迭代时,都会更新 `episode` 变量,并可以在循环体内访问其值以跟踪进度或者作为参数传递给其他功能模块[^1]。 如果希望调整上述代码中的某些行为,可以根据具体需求更改 `num_episodes` 的数值大小或是向循环内部添加更多处理步骤。此外,还可以引入条件判断语句或其他控制结构使程序更加灵活多变。 #### 修改循环Episode的行为 假设现在有一个特定的任务需要每隔一定数量的 episodes 执行一次特殊动作,则可以通过增加额外的计数器以及相应的条件测试来实现这一点: ```python special_action_interval = 5 # 设定每隔多少个episodes触发一次特别行动 counter = 0 # 初始化辅助计数器 for episode in range(num_episodes): counter += 1 # 增加计数器 if counter % special_action_interval == 0: print(f"Performing special action at the end of episode {episode}") # 继续常规的episode流程... print("All episodes have been completed.") ``` 这里增加了两个新的元素:一个是用来记录已经过了多少个连续 episodes 的计数器 `counter`; 另外则是每当到达预设间隔(`special_action_interval`)就采取措施的一段逻辑分支。这样就可以轻松地扩展基础框架的功能而不改变原有核心部分的工作方式。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值