1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
|
#include <iostream>
#include <signal.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include "process.h"
/* ---------------------------------------------------------------------- */
void clean_up_child_process(int s)
{
cout << "Signal: " << s << endl;
int status;
wait(&status);
}
void POSIX_init()
{
signal(SIGCHLD, SIG_IGN);
signal(SIGPIPE, SIG_IGN);
/*
// handle SIGCHLD by calling clean_up_child_process
struct sigaction sigchld_action;
memset(&sigchld_action, 0, sizeof(sigchld_action));
sigchld_action.sa_handler=&clean_up_child_process;
sigaction(SIGCHLD, &sigchld_action, NULL);
// handle SIGPIPE
memset(&sigchld_action, 0, sizeof(sigchld_action));
sigchld_action.sa_handler=&clean_up_child_process;
sigaction(SIGPIPE, &sigchld_action, NULL);
*/
}
/* asynchronous program execution */
int exec_async(list<string> & arguments)
{
pid_t child_pid;
child_pid=fork();
if (child_pid == 0)
{
/* child process */
const char ** arglist=new const char *[arguments.size()+1];
list<string>::iterator si;
int ind=0;
for (si=arguments.begin(); si!=arguments.end(); si++)
arglist[ind++]=(*si).c_str();
arglist[ind]=0; // NULL terminated (char * array)
execvp(arglist[0], (char * const *)arglist);
}
/* parent process */
return child_pid;
}
#define READ_BUFFER_SIZE 1000
string exec_sync_stdout(string & command)
{
string eval;
char * buffer=new char[READ_BUFFER_SIZE+2];
FILE * fp;
fp=popen(command.c_str(), "r");
if (fp)
while (!feof(fp))
{
if (fgets(buffer, READ_BUFFER_SIZE, fp))
eval+=buffer;
}
pclose(fp);
return eval;
}
string exec_sync_stdout(list<string> & arguments)
{
string eval;
int fds[2];
pid_t pid;
/* Create a pipe. File descriptors for the two ends of the pipe are
placed in fds. */
pipe (fds);
/* Fork a child process. */
pid = fork ();
if (pid == (pid_t) 0)
{
/* This is the child process. Close our copy of the read end of
the file descriptor. */
close (fds[0]);
/* Connect the write end of the pipe to standard input. */
dup2 (fds[1], STDOUT_FILENO);
/* Replace the child process with the given program. */
const char ** arglist=new const char *[arguments.size()+1];
list<string>::iterator si;
int ind=0;
for (si=arguments.begin(); si!=arguments.end(); si++)
arglist[ind++]=(*si).c_str();
arglist[ind]=0; // NULL terminated (char * array)
execvp(arglist[0], (char * const *)arglist);
}
else
{
/* This is the parent process. */
FILE* stream;
char * buffer=new char[READ_BUFFER_SIZE+2];
/* Close our copy of the write end of the file descriptor. */
close (fds[1]);
/* Convert the write file descriptor to a FILE object, and write
to it. */
stream = fdopen (fds[0], "r");
while (!feof(stream))
{
if (fgets(buffer, READ_BUFFER_SIZE, stream))
eval+=buffer;
}
close (fds[0]);
/* Wait for the child process to finish. */
waitpid (pid, NULL, 0);
}
return eval;
}
|