Replace multi-process logic to multi-thread logic #2
38
src/main.cpp
38
src/main.cpp
@@ -1,7 +1,10 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <sys/wait.h>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include <set>
|
||||||
#include <array>
|
#include <array>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
@@ -45,18 +48,45 @@ void readingThreadFunc()
|
|||||||
void scannerThreadFunc()
|
void scannerThreadFunc()
|
||||||
{
|
{
|
||||||
std::unique_lock<std::mutex> lock(mtx);
|
std::unique_lock<std::mutex> lock(mtx);
|
||||||
|
std::vector<pid_t> childrens;
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
cv.wait(lock, []
|
cv.wait(lock, []
|
||||||
{ return manager.isAnybodyInQueue(); });
|
{ return manager.isAnybodyFlagged(); });
|
||||||
|
|
||||||
std::vector<std::string> scanUsers = manager.getFlaggedUsers();
|
std::set<std::string> scanUsers = manager.getFlaggedUsers();
|
||||||
manager.unflagAllUsers();
|
manager.unflagAllUsers();
|
||||||
|
|
||||||
for (std::vector<std::string>::iterator it = scanUsers.begin(); it != scanUsers.end(); it++)
|
lock.unlock();
|
||||||
|
childrens.clear();
|
||||||
|
|
||||||
|
for (const std::string& user : scanUsers)
|
||||||
{
|
{
|
||||||
system((std::string(SCAN_CMD_USR) + *it).c_str());
|
pid_t child = fork();
|
||||||
|
|
||||||
|
if (child < 0)
|
||||||
|
{
|
||||||
|
std::cerr << "Fork failed for: " << user << std::endl;
|
||||||
|
}
|
||||||
|
else if (child == 0) // child
|
||||||
|
{
|
||||||
|
std::string cmd = (std::string(SCAN_CMD_USR) + user);
|
||||||
|
execl("/bin/sh", "sh", "-c", cmd.c_str(), static_cast<char *>(nullptr));
|
||||||
|
std::cerr << "Scan failed" << std::endl;
|
||||||
|
_exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
else // parent
|
||||||
|
{
|
||||||
|
childrens.push_back(child);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for (const pid_t& pid : childrens)
|
||||||
|
{
|
||||||
|
waitpid(pid, nullptr, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
lock.lock();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -4,6 +4,7 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include <set>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include "locations.h"
|
#include "locations.h"
|
||||||
@@ -71,23 +72,23 @@ public:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<std::string> getUsers()
|
std::set<std::string> getUsers()
|
||||||
{
|
{
|
||||||
std::vector<std::string> ret;
|
std::set<std::string> ret;
|
||||||
|
|
||||||
std::lock_guard<std::mutex> lock(mtx);
|
std::lock_guard<std::mutex> lock(mtx);
|
||||||
|
|
||||||
for (std::map<std::string, bool>::iterator it = users.begin(); it != users.end(); ++it)
|
for (std::map<std::string, bool>::iterator it = users.begin(); it != users.end(); ++it)
|
||||||
{
|
{
|
||||||
ret.push_back(it->first);
|
ret.insert(it->first);
|
||||||
}
|
}
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<std::string> getFlaggedUsers()
|
std::set<std::string> getFlaggedUsers()
|
||||||
{
|
{
|
||||||
std::vector<std::string> ret;
|
std::set<std::string> ret;
|
||||||
|
|
||||||
std::lock_guard<std::mutex> lock(mtx);
|
std::lock_guard<std::mutex> lock(mtx);
|
||||||
|
|
||||||
@@ -95,14 +96,14 @@ public:
|
|||||||
{
|
{
|
||||||
if (it->second)
|
if (it->second)
|
||||||
{
|
{
|
||||||
ret.push_back(it->first);
|
ret.insert(it->first);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool isAnybodyInQueue()
|
bool isAnybodyFlagged()
|
||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> lock(mtx);
|
std::lock_guard<std::mutex> lock(mtx);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user