return !ppid || !kill(ppid, 0) ? 1 : 0;
}
-int ForkChild::child_iteration()
-{
- int ret = read_child(100);
- if( ret <= 0 ) return ret;
- return handle_child();
-}
-
void ForkParent::start_child()
{
lock("ForkParent::new_child");
// Return -1 if the parent is dead
// Return 0 if timeout
// Return 1 if success
-int ForkBase::read_timeout(int ms, int fd, void *data, int bytes)
+int ForkBase::read_timeout(int64_t usec, int fd, void *data, int bytes)
{
fd_set rfds;
struct timeval timeout_struct;
uint8_t *bp = (uint8_t *)data;
while( bytes_read < bytes ) {
- timeout_struct.tv_sec = ms / 1000;
- timeout_struct.tv_usec = (ms % 1000) * 1000;
+ timeout_struct.tv_sec = usec / 1000000;
+ timeout_struct.tv_usec = usec % 1000000;
FD_ZERO(&rfds);
FD_SET(fd, &rfds);
int result = select(fd+1, &rfds, 0, 0, &timeout_struct);
return !pid || !kill(pid, 0) ? 1 : 0;
}
-int ForkBase::read_parent(int ms)
+int ForkBase::read_parent(int64_t usec)
{
token_bfr_t bfr;
- int ret = read_timeout(ms, parent_fd, &bfr, sizeof(bfr));
+ int ret = read_timeout(usec, parent_fd, &bfr, sizeof(bfr));
if( ret > 0 ) {
parent_token = bfr.token;
parent_bytes = bfr.bytes;
parent_data = new uint8_t[parent_allocated = parent_bytes];
}
if( parent_bytes ) {
- ret = read_timeout(1000, parent_fd, parent_data, parent_bytes);
+ ret = read_timeout(1000000, parent_fd, parent_data, parent_bytes);
if( !ret ) {
printf("read_parent timeout: %d\n", parent_fd);
ret = -1;
return ret;
}
-int ForkBase::read_child(int ms)
+int ForkBase::read_child(int64_t usec)
{
token_bfr_t bfr;
- int ret = read_timeout(ms, child_fd, &bfr, sizeof(bfr));
+ int ret = read_timeout(usec, child_fd, &bfr, sizeof(bfr));
if( ret > 0 ) {
child_token = bfr.token;
child_bytes = bfr.bytes;
child_data = new uint8_t[child_allocated = child_bytes];
}
if( child_bytes ) {
- ret = read_timeout(1000, child_fd, child_data, child_bytes);
+ ret = read_timeout(1000000, child_fd, child_data, child_bytes);
if( !ret ) {
printf("read_child timeout: %d\n", child_fd);
ret = -1;
ForkChild::ForkChild()
{
- done = 0;
+ parent_done = 0;
}
ForkChild::~ForkChild()
{
}
-int ForkChild::handle_child()
-{
- printf("ForkChild::handle_child %d\n", __LINE__);
- return 0;
-}
-
ForkParent::ForkParent()
: Thread(1, 0, 0)
{
- done = -1;
+ parent_done = -1;
}
ForkParent::~ForkParent()
void ForkParent::start()
{
- done = 0;
+ parent_done = 0;
Thread::start();
}
void ForkParent::run()
{
- while( !done && parent_iteration() >= 0 );
- done = 1;
+ while( !parent_done && parent_iteration() >= 0 );
+ parent_done = 1;
}