子进程的主函数
Main function for a child process
我有一个小而令人困惑的问题...第一个问题是主要的用途是什么。我知道这个问题既愚蠢又简单,但我有一个问题。我写了一个代码
#include<unistd.h>
#include<sys/types.h>
#include<stdio.h>
int main(){
pid_t ty;
ty=fork();
if(ty==0){
const char* x="/home/brucewilson/Desktop/jack_sahoo_teja_CDP/hey2";
static char *argv[]={"echo","Foo is my name.",NULL};
int main(){//observe this is second main in my child process
printf("hello");
}
int add(){
printf("5");
}
main();
add();
}
}'
您是否很好地观察了我在子进程中使用的第二个主要函数,编译器没有给我任何错误。.除此之外,它还给了我"hello"和 5 的输出。
显然,下面的代码会给出一个错误...
int main(){
printf("main");
main();
}
int main(){
}
所以我的问题是为什么它对子进程有效?因此,假设 im 为真,没有函数可以命名为 main(),并且每个子进程都有一个与其父进程共享的主函数的概念是错误的。请解释一下我的系统内这段代码下面发生了什么,因为子进程将 main 作为另一个函数,它也不需要 main 函数。那么子进程如何知道它应该从哪里开始呢?
您正在使用称为"嵌套函数"的非标准 GCC 扩展。
第二个示例失败,因为您没有嵌套main()
的第二个定义,因此它与第一个定义冲突。
相关文章:
- 终止 QProcess 不会终止子进程
- 什么时候最好在子进程中使用 CPU 或 I/O 密集型代码 [ C++ ]
- 子进程更新共享 mmap 内存,但父进程没有更改
- 使用 waitpid 时等待子进程终止
- 使用重定向标准处理子进程中的 kbhit
- 由 JOB 中的进程启动的子进程是否可以将 JOB 属性设置为脱离作业?
- 是否可以将子进程的 stdout 重定向到父进程中的另一个文件?
- kill() 总是返回 0(成功),即使在子进程已经结束之后?
- 父进程和子进程之间的 POSIX 信号量
- 检测到由于操作系统内存不足而导致子进程终止
- 使用system()创建独立的子进程
- 从stdin读取时子进程挂起(fork/dup2竞争条件)
- 在 Bash 脚本中处理来自子进程的信号
- Qt C++ - 如何成功将数据传递给子进程?
- C++ 窗口本地系统模拟在子进程中失败
- 将类型化数组写入子进程 stdin 无法正常工作
- 子进程的主函数
- c++通过exec函数杀死子进程,但不杀死父进程和正常运行时间
- 从Python子进程终止时的c++调用函数
- 当子进程完成时调用函数