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

/ 2,438评论 / 11216阅读 / 2点赞

问题

实验示例代码中,调用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. I know this if off topic but I’m looking into starting my own weblog and was curious what all is required to
    get setup? I’m assuming having a blog like
    yours would cost a pretty penny? I’m not very web smart so
    I’m not 100% certain. Any recommendations or advice
    would be greatly appreciated. Cheers

  2. This information is priceless. Where can I find out more?

  3. Excellent blog! Do you have any helpful hints for aspiring writers?
    I’m hoping to start my own website soon but I’m a
    little lost on everything. Would you propose starting with a free platform like WordPress or
    go for a paid option? There are so many choices
    out there that I’m completely overwhelmed ..
    Any tips? Cheers!

  4. kids cloths说道:

    Aw, this was a very good post. Taking the time and actual effort to create a great article… but what can I say… I put things off a lot and never seem to get anything done.

  5. Javaburn reviews说道:

    I’m really enjoying the design and layout of your site. It’s a very easy on the eyes which makes it much more enjoyable for me to come here and visit more often. Did you hire out a developer to create your theme? Superb work!

  6. Boostaro Reviews说道:

    What¦s Going down i am new to this, I stumbled upon this I have found It positively useful and it has aided me out loads. I’m hoping to give a contribution & aid different users like its helped me. Great job.

  7. official site说道:

    Instead of manually clicking your mouse on the “Play” button to
    start a new round, you can use one of the specified hotkeys for a more streamlined gaming experience.

  8. Mushroom gummies说道:

    Excellent article! We will be linking to this particularly great article on our site. Keep up the great writing.

  9. services说道:

    Your style is unique in comparison to other folks I have read stuff from. I appreciate you for posting when you’ve got the opportunity, Guess I will just book mark this site.

  10. lease option说道:

    I was able to find good advice from your blog posts.

  11. Eveline说道:

    Greetings! Very useful advice within this article! It’s the
    little changes which will make the largest changes.
    Thanks a lot for sharing!

  12. Thanks for sharing your thoughts. I really appreciate your efforts and I
    am waiting for your next write ups thank you once again.

  13. Fashion说道:

    Great article.

  14. Java Burn Review说道:

    Wow! Thank you! I permanently needed to write on my site something like that. Can I include a portion of your post to my site?

  15. excellent post.Ne’er knew this, thanks for letting me know.

  16. Сантехника купить Краснодар можно в специализированных
    магазинах или заказать онлайн.
    Город предлагает широкий выбор качественной продукции.

  17. Pancakeswap说道:

    Wow, wonderful weblog layout! How lengthy have you ever been running a
    blog for? you make blogging glance easy. The entire look
    of your site is great, as smartly as the content material!

  18. Leanbiome Review说道:

    I have read some good stuff here. Certainly value bookmarking for revisiting. I surprise how so much attempt you set to create this kind of excellent informative web site.

  19. Hello, I enjoy reading through your post. I wanted to write a little comment to support
    you.

  20. pronerve 6说道:

    ProNerve 6 nerve relief formula stands out due to its advanced formula combining natural ingredients that have been specifically put together for the exceptional health advantages it offers.

  21. discuss说道:

    Definitely believe that which you said. Your favorite justification seemed to be on the internet the easiest thing to be
    aware of. I say to you, I certainly get irked while people consider worries that they plainly don’t know about.
    You managed to hit the nail upon the top as well as
    defined out the whole thing without having side effect
    , people can take a signal. Will probably be back to get more.
    Thanks

  22. analytics firm说道:

    excellent points altogether, you just received a new reader. What could you recommend in regards to your put up that you simply made a few days ago? Any positive?

  23. Hi my loved one! I want to say that this post is amazing, nice written and include almost
    all vital infos. I’d like to peer extra posts like this
    .

  24. whoah this blog is excellent i love reading your articles. Keep up the great work! You know, many people are hunting around for this information, you could help them greatly.

  25. Outlook 2021说道:

    As postagens são realmente úteis.

  26. CharissaH说道:

    Very interesting subject, thanks for putting up..

  27. Hello! I could have sworn I’ve been to this blog before but after browsing through some of the post I realized it’s new to me. Anyways, I’m definitely happy I found it and I’ll be book-marking and checking back frequently!

  28. Good day! Do you know if they make any plugins to protect against hackers?
    I’m kinda paranoid about losing everything I’ve worked hard on.
    Any recommendations?

  29. hey there and thank you for your info – I’ve certainly picked
    up anything new from right here. I did however expertise some technical
    points using this site, since I experienced to reload the web site lots of times previous to I could get it to load properly.
    I had been wondering if your hosting is OK? Not that I am complaining,
    but slow loading instances times will sometimes
    affect your placement in google and can damage your quality score
    if advertising and marketing with Adwords. Well I’m adding this RSS to my email and could look out for
    much more of your respective fascinating content. Make sure you update this again soon..
    Escape room

  30. top iptv说道:

    I really like it when folks get together and share ideas.
    Great website, keep it up!

  31. I just could not depart your website before suggesting that I really enjoyed
    the standard information a person supply for your guests?
    Is gonna be again ceaselessly in order to check out new
    posts

  32. I think this is among the most significant information for me.
    And i’m happy reading your article. But should commentary on some general things, The
    site style is great, the articles is actually great : D.

    Excellent process, cheers

  33. zoritoler imol说道:

    Hi there! This is kind of off topic but I need some help from an established blog. Is it hard to set up your own blog? I’m not very techincal but I can figure things out pretty fast. I’m thinking about creating my own but I’m not sure where to start. Do you have any points or suggestions? With thanks

  34. -.说道:

    Откройте дверь к лучшей версии себя – перейдите по ссылке на -%D0%9E%D0%BF%D1%81%D1%83%D0%B8%D0%BC%D0%BE%D0%BB%D0%BE%D0%B3.%20%D0%9A%D1%82%D0%BE%20%D1%82%D0%B0%D0%BA%D0%BE%D0%B9

  35. It’s actually very complex in this busy life to listen news on TV, so I
    simply use the web for that purpose, and get the newest
    news.

  36. deneme bonusu说道:

    You actually make it seem so easy with your presentation but I find
    this topic to be actually something that I think I would never understand.

    It seems too complicated and very broad for
    me. I am looking forward for your next post,
    I’ll try to get the hang of it!

发表回复

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