[linux操作系统实验]进程的控制(execl()第二个传参为什么是可执行程序名)

/ 909评论 / 3561阅读 / 1点赞

问题

实验示例代码中,调用execl()时,第二个传参为什么会是可执行程序名:

execl("/bin/ls", "ls", "-l", "-color", NULL)

#include <iostream>
using namespace std;

int main(int aNum, char** aStr)
{
        cout << "hello,coolight" << endl;
        cout << "main参数列表:" << endl;
        for(int i = 0; i < aNum; ++i)
                cout << i + 1 << " : " << aStr[i] << endl;

        return 0;
}

实验具体内容

实验内容

涉及的系统调用

在Linux中fork()是一个非常有用的系统调用,但在Linux中建立进程除了fork()之外,也可用与fork() 配合使用的exec()。

exec()系列

exec()fork()联合使用

int status;
     ............
if (fork() == 0)
{
     ............
     execl(...);
     ............
}
wait(&status);

wait()

exit()

源代码

#include <iostream>
#include <unistd.h>
#include "sys/wait.h"
#include "sys/types.h"
using namespace std;
int main()
{
        cout << "<< coolgiht" << endl;
        int status, renum_fork = fork();
        if(renum_fork == -1){
                cout << "<< fork error!" << endl;
        }else if(renum_fork == 0){ //子进程
                execl("./hello", "hello", "-1", "-2", NULL);
                cout << "execl error!" << endl;
        }else{  //父进程
                wait(NULL);
                cout << "<< 完成!" << endl;
        }
        return 0;
}
#include <iostream>
using namespace std;
int main(int aNum, char** aStr)
{
        cout << "hello,coolight" << endl;
        cout << "main参数列表:" << endl;
        for(int i = 0; i < aNum; ++i)

                cout << i + 1 << " : " << aStr[i] << endl;

        return 0;
}

运行结果截图

思考

  1. Leonardlex说道:

    lasix uses: Buy Lasix – lasix medication

  2. Jamescix说道:

    amoxicillin 500mg capsule cost: amoxicillin 500mg cost – amoxicillin azithromycin

  3. Charlesflarm说道:

    https://buyprednisone.store/# prednisone 10 mg daily

  4. Davidham说道:

    amoxicillin 500 mg tablet amoxicillin order online no prescription amoxicillin 500 mg brand name

  5. Charlesflarm说道:

    http://buyprednisone.store/# prednisone 60 mg price

  6. Jamescix说道:

    buy stromectol online: can you buy stromectol over the counter – ivermectin eye drops

  7. Stephenvew说道:

    http://buyprednisone.store/# buy prednisone online india

  8. Charlesflarm说道:

    http://lisinopril.top/# price of lisinopril generic

  9. Leonardlex说道:

    furosemide 40 mg: Buy Furosemide – lasix 100mg

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注