【发布时间】:2015-11-22 10:12:09
【问题描述】:
这是我的代码:
#include<stdio.h>
#include<signal.h>
void my_isr();
int main()
{
signal(SIGALRM,my_isr);
alarm(5);
pause();
printf("in main()...\n");//case2 comments it //case 1 & 3 uncomments it
return 0;//case 1 & 2 comments this line // case 3 uncomment it
}
void my_isr()
{
printf("I am in my_isr()\n");
}
如果我在后台运行进程
案例 1 :如果我在 main 作用域末尾的 main 函数中没有 return 0,它(shell-bash)会打印 exit 值 13(最后一个 printf 中的可打印字符数)。为什么只有最后一个 printf ?它是未定义的行为吗?
xyz@xyz-PC:~/s_flow$ ./a.out &
[1] 9158
xyz@xyz-PC:~/s_flow$ I am in my_isr()
in main()...
[1]+ Exit 13 ./a.out
然后shell打印Exit,状态值为13。
案例 2 :如果 main() 末尾没有 return 0 并且我删除了 last printf(打印 13 个字符)
xyz@xyz-PC:~/s_flow$ ./a.out &
[1] 9169
xyz@xyz-PC:~/s_flow$ I am in my_isr()
[1]+ Exit 255 ./a.out
然后shell打印退出状态值为255。如何?
案例 3:最后,如果所有内容都正确提供,即如果我在 main() 末尾提及 return 0,则 shell 打印的 Done 消息没有任何价值。
xyz@xyz-PC:~/s_flow$ ./a.out &
[1] 9178
xyz@xyz-PC:~/s_flow$ I am in my_isr()
in main()...
[1]+ Done ./a.out
我大致了解为什么 bash shell 会打印 Exit 13、Exit 255 和 Done,但欢迎提供任何进一步的解释。我真正的问题是,当一个进程在后台运行时,shell 如何与操作系统交互,我可以在哪里找到这些 shell 消息?以及OS 如何将这些状态值返回给shell & 然后 shell 打印?任何帮助表示赞赏。提前致谢。
【问题讨论】: