Senare hade jag i tanke att eventuellt göra något med stdout, men till en början vill jag bara får den att hantera olika program genom att slå på och döda dem.
Hur som helst har jag testa en liknade de du föreslår ovan (koden nedan är tagen från stackoverflow):
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <signal.h>
#include <string.h>
#include <sys/wait.h>
#include <errno.h>
#include <string>
#include <sstream>
using namespace std;
#define READ 0
#define WRITE 1
FILE * popen2(string command, string type, int & pid)
{
pid_t child_pid;
int fd[2];
pipe(fd);
if((child_pid = fork()) == -1)
{
perror("fork");
exit(1);
}
/* child process */
if (child_pid == 0)
{
if (type == "r")
{
close(fd[READ]); //Close the READ end of the pipe since the child's fd is write-only
dup2(fd[WRITE], 1); //Redirect stdout to pipe
}
else
{
close(fd[WRITE]); //Close the WRITE end of the pipe since the child's fd is read-only
dup2(fd[READ], 0); //Redirect stdin to pipe
}
setpgid(child_pid, child_pid); //Needed so negative PIDs can kill children of /bin/sh
execl("/bin/sh", "/bin/sh", "-c", command.c_str(), NULL);
exit(0);
}
else
{
if (type == "r")
{
close(fd[WRITE]); //Close the WRITE end of the pipe since parent's fd is read-only
}
else
{
close(fd[READ]); //Close the READ end of the pipe since parent's fd is write-only
}
}
pid = child_pid;
if (type == "r")
{
return fdopen(fd[READ], "r");
}
return fdopen(fd[WRITE], "w");
}
int pclose2(FILE * fp, pid_t pid)
{
int stat;
fclose(fp);
while (waitpid(pid, &stat, 0) == -1)
{
if (errno != EINTR)
{
stat = -1;
break;
}
}
return stat;
}
int main()
{
int pid;
string command = "ls -l";
FILE * fp = popen2(command, "r", pid);
char command_out[100] = {0};
stringstream output;
//Using read() so that I have the option of using select() if I want non-blocking flow
while (read(fileno(fp), command_out, sizeof(command_out)-1) != 0)
{
output << string(command_out);
// https://linux.die.net/man/2/kill
kill(-pid, 9);
memset(&command_out, 0, sizeof(command_out));
}
string token;
while (getline(output, token, '\n'))
printf("OUT: %s\n", token.c_str());
pclose2(fp, pid);
return 0;
}
Vilket jag använde mig av för att göra vlc test grej med där jag bara skrev om main till:
int main(){
FILE *fd;
Timer timer;
int pid;
fd = popen2("vlc", "r", pid);
if( fd == NULL ){
std::cout << "Shit is going done" << std::endl;
}
while(10000.0 > timer.elapsed()){
std::cout << "Still waiting" << std::endl;
}
// https://linux.die.net/man/2/kill
kill(-pid, 9);
std::cout << "After wait" << std::endl;
pclose2(fd, pid);
}
Problemet är att de nedre koden där jag skrev om main, så dödar den fortfarande inte VLC. Men enligt denna länk är sigkill = 9. Då den koden på stackoverflow använder sig av negativa PID för att döda innebär att den dödar hela process gruppen med -pid, enligt denna länk.
Det var då jag skrev min "ful" kod längst upp. För jag förstår inte varför VLC lever.
P.S här nedan är TIMER koden:
#ifndef _TIMER_H_
#define _TIMER_H_
#include <chrono>
#include <thread>
class Timer {
typedef std::chrono::high_resolution_clock clock;
public:
Timer():start(clock::now()){}
inline void reset() {
start = clock::now();
}
inline void sleep(int n) const{
std::this_thread::sleep_for(std::chrono::milliseconds(n));
}
inline double elapsed() const {
return std::chrono::duration_cast<std::chrono::milliseconds>(clock::now() - start).count();
}
private:
std::chrono::time_point<clock> start;
};
#endif /*_TIMER_H_*/