简体   繁体   中英

C++ Diff vector<struct> and a vector<string>?

I track my child processes and their 'title' in my parent process using:

struct c_process {
    string device;
    pid_t pid;
};

vector<c_process> children;

I terminate and fork these children based on if 'folders' exist when I SIGHUP the parent and reload its 'config'.

Parent running
Folders /a1 /a3
Reads config and spawn processes (list of folders)
vector<c_process> = {['a1', 111], ['a3', 222]}
Something changes folders
Folders /a1 /a4 /a5
SIGHUP parent
Reads config and spawn processes
vector<c_process> = {['a1', 111], ['a4', 1121], ['a5', 452]}

My problem occurs when I read the list of folders and need to decide 'what needs to be added/fork' and 'what needs to be 'deleted/terminated'.

vector<string> config;
vector<c_process> c_process;

So with the above structures how can I do this?

Be aware that if std::vector grows and has not enoough space reserved it may allocate and construct new elements via copy constructor from the old ones and afterwards destroy the old ones. If pid_t is some high-level object this may hurt. An alternative is to keep only pointers (may be counting smart pointers) within the vector and therefore avoid the destruction of the refered objects.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM