create and kill child process on linux

來源:互聯網
上載者:User
代碼#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>

class ProcessMap
{
private:
  map<std::string, pid_t> m_process_map;

public:

const bool create_process(const std::string& process_name, const std::string& user_name);

const bool remove_process(const std::string& user_name);
};

//create child process and add to process map
const bool ProcessMap::create_process(const std::string& process_name, const std::string& user_name)
{
    QMServerMap::remove_process(user_name);

    pid_t pID = fork();
    if (pID == 0)
    {
                //you can add arguments for the child process if you like
        execl(process_name, NULL);
    }
    else if (pID < 0)
    {
        // failed to fork
        return false;
    }
    else
    {
        // parent
        m_process_map[user_name] = pID;
    }
    return true;
}

//delete child process and remove from the process map
const bool QMServerMap::remove_process(const std::string& user_name)
{
    if (m_process_map.find(user_name)!=m_process_map.end())
    {
        pid_t pid = m_process_map[user_name];
        if (pid > 0)
        {
            kill(pid,SIGTERM);
            waitpid(pid, NULL, 0);
        }
        m_process_map.erase(user_name);

        return true;
    }
    return false;
}

 

1. 如果子進程未結束, 父進程仍在執行, kill 子進程之後, 調用waitpid 讀取子進程狀態, 可以避免僵死進程

 

2. 如果子進程已經退出, 父進程仍在執行且未調用remove process , 此時子進程為僵死進程, 可以通過調用 remove process 刪除僵死子進程

 

 A better way to avoid zombie child process

 

 

代碼#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>

class ProcessMap
{
private:
  static map<std::string, pid_t> m_process_map;

public:

static void create_process(const std::string& process_name, const std::string& user_name);

static void remove_process(const std::string& user_name);

static void zombie_handler(int signal);
};

void ProcessMap::zombie_handler(int signal)
{
    int status;
    pid_t pid = wait(&status);
    map<string, pid_t>::iterator iter;
    for(iter = m_process_map.begin(); iter!=m_process_map.end(); ++iter)
        if (iter->second == pid)
        {
            m_process_map.erase();
            return;
        }
}

//create child process and add to process map
void ProcessMap::create_process(const std::string& process_name, const std::string& user_name)
{
    if (m_process_map.find(user_name)!=m_process_map.end())
        return;

    signal(SIGCHLD, zombie_handler);

    pid_t pID = fork();
    if (pID == 0)
    {
        //you can add arguments for the child process if you like
        execl(process_name, NULL);
    }
    else if (pID < 0)
    {
        // failed to fork
        return;
    }
    else
    {
        // parent
        m_process_map[user_name] = pID;
    }
}

//kill child process
void QMServerMap::remove_process(const std::string& user_name)
{
    if (m_process_map.find(user_name)!=m_process_map.end())
            kill(m_process_map[user_name],SIGTERM);
 }

1. signal(SIGCHLD, zombie_handler) ; 註冊zombie_handler 來處理 子進程結束事件, 調用wait, 刪除zombie process

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.