我试图了解程序,shell命令和操作系统的工作原理.请原谅我的无知,因为我是新手.
当我在命令行上使用C编译器时,当我输入cc [filename]时,我认为shell使用fork()系统调用来复制其进程,然后exec()系统调用将cc编译器可执行文件加载到儿童过程的核心形象.然后,包含cc可执行文件的子进程将执行其操作,而执行shell的父进程是否等待.这样对吗?
那些shell命令如cp,mv,ls等等.这些是什么?它们是可执行程序,也将在shell分叉的新子进程中执行吗?shell脚本怎么样?假设我创建了一个像这样的简单shell脚本(请忽略任何我不知道如何执行此操作的错误):
echo "Hello"
date
echo
cc -o test file1.c file2.c file3.c
Run Code Online (Sandbox Code Playgroud)
然后我使用命令行执行此脚本.请问命令行fork()一个新进程和exec()这个脚本在新进程中?然后这个新进程会包含脚本fork()其他进程来执行date,cc编译器等吗?
我希望这听起来不会太混乱,因为我= /.
我使用以下代码获得"Segmentation fault(core dumped)"错误:
#include <iostream>
#include <cstring>
template <class T>
T maxn(T* elements, int n);
template <> const char* maxn <const char*>(const char* elements[], int num);
int main(){
using namespace std;
//Using template
int num[6] = {2, 3, 4, 5, 6, 22};
double num2[4] = {16.6, 10.5, 2.3, 1.1};
int larg = maxn(num, 6);
double larg2 = maxn(num2, 4);
cout << larg << endl << larg2 << endl;
//Using specialization
const char* pps[5] = {"Hello", "There", "I", "am", "me"};
const …Run Code Online (Sandbox Code Playgroud)