Bike-X
0.8
Main Page
Namespaces
Data Structures
Files
File List
Globals
All
Data Structures
Namespaces
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Friends
Macros
Device.cpp
Go to the documentation of this file.
1
#include <iostream>
2
#include <string>
3
#include <unistd.h>
4
#include <signal.h>
5
#include <stdlib.h>
6
#include <stdio.h>
7
#include <errno.h>
8
#include <
Device.h
>
9
10
using namespace
std;
11
12
int
Device::pythonPid
;
13
unsigned
char
Device::buffer
[
BUFFER_SIZE
];
14
18
static
void
initPython
()
19
{
20
int
parentPid = getpid();
21
22
// Creates a whole new process
23
Device::pythonPid
= fork();
24
if
(
Device::pythonPid
== -1)
25
{
26
cout <<
"Problems while initiazing a new process!!!"
<< endl;
27
perror(
"Reason"
);
28
exit(-1);
29
}
30
31
// pid > 0 means this is still parent process
32
if
(
Device::pythonPid
> 0)
33
{
34
cout <<
"Initiating python program with pid: "
<<
Device::pythonPid
<< endl;
35
return
;
// so we just keep doing our amazing job
36
}
37
38
// If we got here, from the line bellow until exit(0) means totaly the child process
39
cout <<
"$ "
<<
PYTHON_PROGRAM
<<
" "
<< parentPid << endl;
40
char
strPid[16];
// Gotta convert pid into a string
41
sprintf(strPid,
"%i"
, parentPid);
42
int
rc = execl(
PYTHON_PROGRAM
,
"main.py"
, strPid,
NULL
);
43
if
(rc == -1)
44
{
45
cout <<
"Error while initiating python program: "
<< rc << endl;
46
perror(
"Reason"
);
47
}
48
exit(0);
49
}
50
51
void
Device::init
()
52
{
53
Device::pythonPid
= 0;
54
initPython
();
55
}
56
57
void
Device::destroy
()
58
{
59
if
(
Device::pythonPid
> 0)
60
{
61
cout <<
"Killing python process: "
<<
Device::pythonPid
<< endl;
62
kill(
Device::pythonPid
, SIGTERM);
63
}
64
}
Device::destroy
static void destroy()
Definition:
Device.cpp:57
Device::init
static void init()
Definition:
Device.cpp:51
NULL
#define NULL
PYTHON_PROGRAM
#define PYTHON_PROGRAM
Definition:
Device.h:7
initPython
static void initPython()
Definition:
Device.cpp:18
Device::buffer
static unsigned char buffer[BUFFER_SIZE]
Definition:
Device.h:29
Device::pythonPid
static int pythonPid
Definition:
Device.h:16
BUFFER_SIZE
#define BUFFER_SIZE
Definition:
Device.h:6
Device.h
device
Device.cpp
Generated on Wed Oct 1 2014 00:45:20 for Bike-X by
1.8.6