【问题标题】:In terms of call-return behaviour, how are the fork() and exec() system calls different from other system calls?在调用返回行为方面,fork() 和 exec() 系统调用与其他系统调用有何不同?
【发布时间】:2017-07-27 05:47:57
【问题描述】:

fork 返回两次-

  • 曾在父母中

  • 小时候曾经

但是,就调用和返回行为而言,exec() 与其他系统调用有何不同?

【问题讨论】:

  • exec() 根本不会返回...除非出现错误。

标签: c unix fork exec manpage


【解决方案1】:

实际上,有一些不遵守“返回一次”的范式。

fork() 的调用返回一次两次 - 后者在成功时返回一次在父级和一次在子级中,前者在失败时仅在父级中返回一次.

exec() 的调用将在失败时返回,但如果成功,当前进程会被新程序覆盖。

还有其他的,例如exit()abort(),预计根本不会返回。

【讨论】:

  • 如果有人想知道在哪里可以找到此信息,请在man exec --- "The exec() functions only return if an error has occurred. The return value is -1, and errno is set to indicate the error." 中找到一个条目以供将来参考
  • 如果它保存的jmp_buf 也是longjmpsetjmp() 可以多次返回。
  • Nitpick: setjmp / longjmp 是 C 标准库函数,而不是系统调用。 (它们不需要内核帮助。)
  • @paxdiablo,严格来说,exit(3) 不是系统调用,而是库函数,因为它必须处理 atexit(3) 安装的回调,之后,它应该调用 _exit(2) .同样的事情也适用于abort(3),它通常转化为对kill(2) 的调用。但无论如何都是很好的例子。
猜你喜欢
  • 2011-10-03
  • 1970-01-01
  • 2021-02-18
  • 2015-10-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-09
  • 2011-06-10
相关资源
最近更新 更多