]> ruin.nu Git - popboot.git/commitdiff
parallell
authorMichael Andreen <harv@ruin.nu>
Thu, 2 Jun 2005 15:41:24 +0000 (15:41 +0000)
committerMichael Andreen <harv@ruin.nu>
Thu, 2 Jun 2005 15:41:24 +0000 (15:41 +0000)
action.cpp
genoo
node.cpp
node.h
planner.cpp
planner.h
sim.txt
src.pro

index 6e4b60785917cf7627c7ef1969b9621481c40022..52c1686f21f3bc09ac096c8cae91327013fc5d7c 100644 (file)
@@ -1,5 +1,7 @@
 #include "action.h"
 #include <cstdlib>
+#include <unistd.h>
+#include <sys/wait.h>
 #include <iostream>
 using namespace std;
 using namespace __gnu_cxx;
@@ -33,7 +35,21 @@ const Preconditions& Action::preconditions() const{
 
 int Action::execute() const{
        cout << "Executing: " << _name << endl;
-       return system(_executable.c_str());
+       //return system(_executable.c_str());
+       pid_t proc = fork();
+       if (proc == -1) return proc;
+
+       if (proc == 0) {
+               //execl("/bin/sh",  "-c", _executable.c_str(), (char*) NULL);
+               int val = system(_executable.c_str());
+               exit(val);
+       }
+       int retval;
+
+       waitpid(proc,&retval,0);
+
+       cout << "Done executing: " << _name << endl;
+       return retval;
 }
 
 const string& Action::name() const{
diff --git a/genoo b/genoo
index a555e47bbb86fa70550974833432b1bafa17faad..9fcacd83ba6ac12c1c918d5f3726c7323a1aba37 100644 (file)
--- a/genoo
+++ b/genoo
@@ -1,6 +1,6 @@
 #runlevels
 2: AUMIX, HALD, CRON
-3: AUMIX, HALD, CRON, CUPSD, DISTCCD, FIREWALL, HDDTEMP, HDPARM, SSHD, NTPD
+3: NET, AUMIX, HALD, CRON, CUPSD, DISTCCD, FIREWALL, HDDTEMP, HDPARM, SSHD, NTPD
 4: SSHD, CUPSD
 
 #startup for gentoo
index 83b3334a7dab9b6095e1d1907a35ac808d9d6bc9..b6793f6c73b804b78101eca68389e6d49db3e4e4 100644 (file)
--- a/node.cpp
+++ b/node.cpp
@@ -34,17 +34,23 @@ bool Node::executed() const{
 const Literals& Node::effects() const{
        return _effects;
 }
-void Node::satisfyCondition(std::string effect){
+
+bool Node::satisfyCondition(std::string effect){
        _preconditions.erase(_preconditions.find(effect));
+       return _preconditions.size() == 0 && !_executed;
 }
 
-void Node::execute(const Literals& effects){
-       cerr << "Executing: " << _action->name() << endl;
+bool Node::satisfyConditions(const Literals& effects){
        for (Literals::const_iterator effect = effects.begin(); effect != effects.end(); ++effect){
                cerr << "Satisfied effect: " << *effect << endl;
                _preconditions.erase(_preconditions.find(*effect));
        }
        cerr << "Number of preconditions left: " << _preconditions.size() << endl;
+       return _preconditions.size() == 0 && !_executed;
+}
+
+void Node::execute(){
+       cerr << "Executing: " << _action->name() << endl;
        if(_executed)
                cerr << "Already executed" << endl;
        if ((_preconditions.size() != 0) || _executed)
@@ -56,11 +62,11 @@ void Node::execute(const Literals& effects){
 
        cerr << "Got returnvalue: " << value << ", number of effects: " << _effects.size() << endl;
 
-       for(vector<Node*>::iterator child = _children.begin(); child != _children.end(); ++child){
-               (*child)->execute(_effects);
-       }
 }
 
+const std::vector<Node*>& Node::children() const{
+       return _children;
+}
 StartNode::StartNode(const Literals& init){
        EffectsMap initial;
        initial[0] = init;
diff --git a/node.h b/node.h
index 272bee93bc565977f03a9499f8d02f8255ed7831..669657dd3e8ae373850e5c7967d9db6a3fbb6cbe 100644 (file)
--- a/node.h
+++ b/node.h
@@ -14,10 +14,12 @@ class Node {
                virtual ~Node(){}
                void addChild(Node* node);
                const Action* action() const;
-               void execute(const Literals& effects);
+               void execute();
                bool executed() const;
                const Literals& effects() const;
-               void satisfyCondition(std::string effect);
+               bool satisfyCondition(std::string effect);
+               bool satisfyConditions(const Literals& effects);
+               const std::vector<Node*>& children() const;
 
        protected:
                const Action* _action;
index 514277668c0e378a9a32a0473ba1fc6105012c11..9feeec8ed577398bc859e3d2fe9d1fb21266501c 100644 (file)
@@ -1,10 +1,18 @@
 #include "planner.h"
 #include "node.h"
 #include <iostream>
-#include <vector>
 using namespace std;
 using namespace __gnu_cxx;
 
+extern "C" void* executeNode(void* arg);
+
+struct ExecutionStuff {
+       sem_t* nodes;
+       sem_t* list;
+       Node* node;
+       queue<Node*>* execQueue;
+};
+
 Planner::Planner(std::vector<Action> actions, Literals init, Literals goal){
        _init = init;
        _goal = goal;
@@ -17,22 +25,22 @@ Planner::Planner(std::vector<Action> actions, Literals init, Literals goal){
                _actions.push_back(act);
                const Literals& effects = act->effects(0);
                for (Literals::const_iterator effect = effects.begin(); effect != effects.end(); ++effect){
-                       cerr << "Adding effect: '" << *effect << "', action: " << action->name() << endl;
+                       //cerr << "Adding effect: '" << *effect << "', action: " << action->name() << endl;
                        _actionEffects[*effect] = act;
                }
        }
-       cerr << "Number of actions: " << _actions.size() << endl;
+       //cerr << "Number of actions: " << _actions.size() << endl;
        makePlan(_finish);
 }
 
 Planner::~Planner(){
-       cerr << "Deleting " << _addedNodes.size() << " nodes" << endl;
+       //cerr << "Deleting " << _addedNodes.size() << " nodes" << endl;
        for (vector<Node*>::iterator node = _addedNodes.begin(); node != _addedNodes.end(); ++node){
-               cerr << "Deleting node " << (*node)->action()->name() << endl;
+               //cerr << "Deleting node " << (*node)->action()->name() << endl;
                delete *node;
        }
        for (vector<Action*>::iterator action = _actions.begin(); action != _actions.end(); ++action){
-               cerr << "Deleting action " << (*action)->name() << endl;
+               //cerr << "Deleting action " << (*action)->name() << endl;
                delete *action;
        }
 }
@@ -41,35 +49,35 @@ Planner::~Planner(){
 void Planner::makePlan(Node* node){
        addNode(node);
 
-       cerr << "Fetching preconditions for action: " << node->action()->name() << ".. ";
+       //cerr << "Fetching preconditions for action: " << node->action()->name() << ".. ";
        const Preconditions& preconds = node->action()->preconditions();
-       cerr << "done" << endl;
+       //cerr << "done" << endl;
 
 
        if (preconds.size() == 0){
-               cerr << "Found no preconds" << endl;
+               //cerr << "Found no preconds" << endl;
                _start->addChild(node);
        }else{
                for (Preconditions::const_iterator precond = preconds.begin(); precond != preconds.end(); ++precond){
-                       cerr << "Looking for: '" << precond->first << "'" <<  endl;
+                       //cerr << "Looking for: '" << precond->first << "'" <<  endl;
                        hash_map<string,Node*>::iterator addedNode = _addedEffects.find(precond->first);
                        if(addedNode != _addedEffects.end()){
-                               cerr << "Using already added node" << endl;
+                               //cerr << "Using already added node" << endl;
                                addedNode->second->addChild(node);
                        }else {
                                hash_map<string, Action*>::iterator action = _actionEffects.find(precond->first);
                                if (action != _actionEffects.end()){
-                                       cerr << "Adding new node" << endl;
+                                       //cerr << "Adding new node" << endl;
                                        Node* newnode = new Node(action->second);
                                        newnode->addChild(node);
                                        makePlan(newnode);
                                }else if (precond->second){
-                                       cerr << "Action with effect: " << precond->first << " not found!" << endl;
-                                       cerr << "This is a hard precondition, so this action and the children can't be executed." << endl;
+                                       //cerr << "Action with effect: " << precond->first << " not found!" << endl;
+                                       //cerr << "This is a hard precondition, so this action and the children can't be executed." << endl;
                                        return;
                                }else{
-                                       cerr << "Action with effect: " << precond->first << " not found!" << endl;
-                                       cerr << "This is a soft precondition, so we will continue" << endl;
+                                       //cerr << "Action with effect: " << precond->first << " not found!" << endl;
+                                       //cerr << "This is a soft precondition, so we will continue" << endl;
                                        node->satisfyCondition(precond->first);
                                        _start->addChild(node);
                                }
@@ -79,24 +87,69 @@ void Planner::makePlan(Node* node){
 }
 
 void Planner::addNode(Node* node){
-       cerr << "Adding node for action: " << node->action()->name() << endl;
+       //cerr << "Adding node for action: " << node->action()->name() << endl;
        const Literals& effects = node->action()->effects(0);
-       cerr << "Number of effects: " << effects.size() << endl;
+       //cerr << "Number of effects: " << effects.size() << endl;
        _addedNodes.push_back(node);
 
        for (Literals::const_iterator effect = effects.begin(); effect != effects.end(); ++effect){
-               cerr << "Adding node for effect: " << *effect << endl;
+               //cerr << "Adding node for effect: " << *effect << endl;
                _addedEffects[*effect] = node;
        }
 }
 
 
 void Planner::execute(){
-       _start->execute(Literals());
+       _executedNodes.push(_start);
+       sem_init(&_nodes, 0, 1);
+       sem_init(&_list, 0, 1);
+
+       int executions = 1;
+       while (executions > 0){
+               cerr << "Waiting for a node to finish execution" << endl;
+               int retval;
+               sem_getvalue(&_nodes, &retval);
+               cerr << "Semaphore before wait: " << retval << endl;
+               sem_wait(&_nodes);
+               sem_getvalue(&_nodes, &retval);
+               cerr << "Semaphore after wait: " << retval << endl;
+               --executions;
+
+               cerr << "Getting node: ";
+               sem_wait(&_list);
+               cerr << "Number of nodes in queue: " << _executedNodes.size() << endl;
+               Node* node = _executedNodes.front();
+               _executedNodes.pop();
+               sem_post(&_list);
+               cerr << (int) node << endl;
+               cerr << node->action()->name() << ", and children..  ";
+               vector<Node*> children = node->children();
+               cerr << " done"  << endl;
+               if (node == _finish)
+                       return;
+               cerr << "Iterating over the children, number: " << children.size() << endl;
+               for(vector<Node*>::iterator child = children.begin(); child != children.end(); ++child){
+                       if ((*child)->satisfyConditions(node->effects())){
+                               ++executions;
+                               cerr << "Creating new thread" << endl;
+                               pthread_attr_t tattr;
+                               pthread_t tid;
+                               pthread_attr_init(&tattr);
+                               pthread_attr_setscope(&tattr, PTHREAD_SCOPE_SYSTEM);
+                               ExecutionStuff* es = new ExecutionStuff;
+                               es->nodes = &_nodes;
+                               es->list = &_list;
+                               es->node = *child;
+                               es->execQueue = &_executedNodes;
+                               pthread_create(&tid, &tattr, executeNode, es);
+                               //executeNode(es);
+                       }
+               }
+
+       }
        cerr << "Number of nodes: " << _addedNodes.size() << endl;
        back_insert_iterator<Literals> ii(_init);
        _init.clear();
-       int executions = 0;
        for (vector<Node*>::iterator node = _addedNodes.begin(); node != _addedNodes.end(); ++node){
                cerr << "Deleting node " << (*node)->action()->name() << endl;
                if ((*node)->executed()){
@@ -146,3 +199,22 @@ void Planner::execute(){
        execute();
        
 }
+void* executeNode(void* arg){
+       cerr << "Running new thred." << endl;
+       ExecutionStuff* es = (ExecutionStuff*)arg;
+
+       if (es == 0)
+               pthread_exit((void*)1);
+
+       es->node->execute();
+
+       sem_wait(es->list);
+       cerr << "Adding pointer with value: " << (int)es->node << endl;
+       es->execQueue->push(es->node);
+       sem_post(es->list);
+
+       cerr << "Increasing semaphore" << endl;
+       sem_post(es->nodes);
+
+       pthread_exit((void*)0);
+}
index f00c61473382884c6c42bf427614278c627c851f..8d116933f14a6d0122aaa0182ca22f21686ca277 100644 (file)
--- a/planner.h
+++ b/planner.h
@@ -3,6 +3,9 @@
 
 #include <ext/hash_map>
 #include <vector>
+#include <queue>
+#include <pthread.h>
+#include <semaphore.h>
 #include "action.h"
 
 class Node;
@@ -14,10 +17,15 @@ class Planner {
 
                void execute();
 
+
        protected:
 
                void makePlan(Node* node);
                void addNode(Node* node);
+
+               sem_t _nodes;
+               sem_t _list;
+               std::queue<Node*> _executedNodes;
                
                Node* _start;
                Node* _finish;
diff --git a/sim.txt b/sim.txt
index 48eeff03d6721c5ceec5d126a17a88ebaeab2bcd..7210aac883b57a86d0b86ddc0a217035d6bcf284 100644 (file)
--- a/sim.txt
+++ b/sim.txt
@@ -11,6 +11,7 @@
 # 5 - X11
 #
 #
+1 : NET
 2 : NET, SOUND, PRINTING 
 3 : NET, HTTPD, SOUND, PRINTING, SSH, IRC
 5 : NET, HTTPD, SSH, IRC, X
diff --git a/src.pro b/src.pro
index 67b564646fc5170ef29992645cd269e01260278d..6495a0c03719a147827dbf9189fb6924f4682677 100644 (file)
--- a/src.pro
+++ b/src.pro
@@ -7,7 +7,7 @@ CONFIG -= qt
 CONFIG += debug
 INCLUDEPATH += .
 TARGET = planner
-#LIBS += -lpthread
+LIBS += -lpthread
 QMAKE_YACC = bison
 
 # Input