ONPOSIX  2.0
 All Classes Namespaces Files Functions Variables Enumerator Friends Macros Pages
Process.hpp
Go to the documentation of this file.
1 /*
2  * Process.hpp
3  *
4  * Copyright (C) 2012 Evidence Srl - www.evidence.eu.com
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 #ifndef PROCESS_HPP_
22 #define PROCESS_HPP_
23 
24 #include <sys/types.h>
25 #include <unistd.h>
26 #include <string>
27 #include <vector>
28 
29 // Uncomment to enable Linux-specific methods:
30 #define ONPOSIX_LINUX_SPECIFIC
31 
32 namespace onposix {
33 
34 /**
35  * \brief Class for launching a new process
36  *
37  * Class to launch a process through fork().
38  *
39  * Example of usage to run a function:
40  * \code
41  *
42  * void function ()
43  * {
44  * //...
45  * }
46  *
47  * int main ()
48  * {
49  * Process p(function);
50  * }
51  * \endcode
52  *
53  * Example of usage to run a program (i.e., through for()+execvp())
54  * \code
55  *
56  * int main ()
57  * {
58  * std::vector<std::string> args;
59  * args.push_back("-l");
60  * args.push_back("*.cpp");
61  *
62  * Process p("ls", args);
63  * }
64  * \endcode
65  */
66 class Process {
67 
68  /**
69  * \brief Pid of the new process
70  *
71  * This value contains the pid of the new process for both the
72  * parent process and the child process (who sees its own pid)
73  */
74  pid_t pid_;
75 
76  /**
77  * \brief If the class instance is related to the current process
78  *
79  * In a parent-child relationship, this variable is useful to
80  * distinguish between the parent and the child.
81  * This variable is euql to false for the parent (i.e., the process
82  * who created the new process) and equal to true for the child
83  * process (i.e., the one who has been created).
84  */
85  bool is_child_;
86 
87  /**
88  * \brief If the process is running
89  */
90  bool running_;
91 
92  /**
93  * \brief Exit status of the child process when terminated.
94  *
95  * The child process can be terminated by the parent through
96  * sendSignal(KILL). The parent can also waith the normal termination
97  * of the child through waitForTermination().
98  */
99  int status_;
100 
101  void createProcess();
102 
103 public:
104 
105  /**
106  * \brief Get PID of the process related to this instance
107  *
108  * This value is the pid of the new process for both the
109  * parent process and the child process (who gets its own pid)
110  */
111  inline pid_t getPid () const {
112  return pid_;
113  }
114 
115  Process(void (*function)(void));
116 
117  Process(const std::string& program,
118  const std::vector<std::string>& args);
119 
120 
121  bool waitForTermination();
122  inline bool checkNormalTermination();
123  inline bool checkSignalTermination();
124  bool sendSignal(int sig);
125  static bool setSignalHandler(int sig, void (*handler) (int));
126  bool setSchedParam(int policy, int priority);
127  bool getSchedParam(int* policy, int* priority);
128 
129 #if defined(ONPOSIX_LINUX_SPECIFIC) && defined(__GLIBC__) && \
130  ((__GLIBC__ > 2) || ((__GLIBC__ == 2) && (__GLIBC_MINOR__ > 3)))
131  // Functions to get/set affinity are available only from glibc 2.4
132  void setAffinity(const std::vector<bool>& s);
133  void getAffinity(std::vector<bool>* v);
134 #endif /* ONPOSIX_LINUX_SPECIFIC && GLIBC */
135 };
136 
137 } /* onposix */
138 
139 #endif /* PROCESS_HPP_ */