gpt4 book ai didi

根据程序状态更改退出行为

转载 作者:太空狗 更新时间:2023-10-29 16:10:15 25 4
gpt4 key购买 nike

我正在尝试使用 atexit () 注册一个函数,它将根据程序的当前状态打印不同的消息。我知道当您使用 atexit 注册函数时,您不能传递参数。

那么,如何传递程序状态,例如一个整数,并使该函数打印不同的语句?

我知道我可以注册多个函数,但我必须只使用一个。

最佳答案

不使用 atexit,而是使用 on_exit 函数,它将传递给 exit() 的状态作为一个参数, void * 可以传入。

int on_exit(void (*function)(int , void *), void *arg);

The on_exit() function registers the given function to be called at normal process termination, whether via exit(3) or via return from the program's main(). The function is passed the status argument given to the last call to exit(3) and the arg argument from on_exit().

The same function may be registered multiple times: it is called once for each registration. When a child process is created via fork(2), it inherits copies of its parent's registrations. Upon a successful call to one of the exec(3) functions, all registrations are removed.

关于根据程序状态更改退出行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49580716/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com