पर बाल आउटपुट पाइपिंग मैं प्रक्रिया प्रक्रिया के साथ संघर्ष कर रहा हूं और बाल प्रक्रिया 'आउटपुट को पैरेंट प्रक्रिया की एक स्ट्रिंग में पाइप कर रहा हूं। मुझे विंडोज़ पर काम करना पड़ा (CreatePipe और CreateProcess और ReadFile का उपयोग करके), लेकिन यूनिक्स पर काम करने के लिए सटीक एनालॉग नहीं लग रहा है।posix_spawnp और एक स्ट्रिंग
#include <spawn.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/wait.h>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
int exit_code;
int cout_pipe[2];
int cerr_pipe[2];
posix_spawn_file_actions_t action;
if(pipe(cout_pipe) || pipe(cerr_pipe))
cout << "pipe returned an error.\n";
posix_spawn_file_actions_init(&action);
posix_spawn_file_actions_addclose(&action, cout_pipe[0]);
posix_spawn_file_actions_addclose(&action, cerr_pipe[0]);
posix_spawn_file_actions_adddup2(&action, cout_pipe[1], 1);
posix_spawn_file_actions_adddup2(&action, cerr_pipe[1], 2);
posix_spawn_file_actions_addclose(&action, cout_pipe[1]);
posix_spawn_file_actions_addclose(&action, cerr_pipe[1]);
vector<string> argmem = {"bla"};
vector<char*> args = {&argmem[0][0], nullptr}; // I don't want to call new.
pid_t pid;
if(posix_spawnp(&pid, "echo", &action, NULL, &args[0], NULL) != 0)
cout << "posix_spawnp failed with error: " << strerror(errno) << "\n";
//close(cout_pipe[0]);
//close(cerr_pipe[0]);
close(cout_pipe[1]);
close(cerr_pipe[1]);
waitpid(pid,&exit_code,0);
cout << "exit code: " << exit_code << "\n";
// Read from pipes
const size_t buffer_size = 1024;
string buffer;
buffer.resize(buffer_size);
ssize_t bytes_read = read(cout_pipe[0], &buffer[0], buffer_size);
while ((bytes_read = read(cout_pipe[0], &buffer[0], buffer_size)) > 0)
{
cout << "read " << bytes_read << " bytes from stdout.\n";
cout << buffer.substr(0, static_cast<size_t>(bytes_read)+1) << "\n";
bytes_read = read(cout_pipe[0], &buffer[0], buffer_size);
}
if(bytes_read == -1)
cout << "Failure reading from stdout pipe.\n";
while ((bytes_read = read(cerr_pipe[0], &buffer[0], buffer_size)) > 0)
{
cout << "read " << bytes_read << " bytes from stderr.\n";
cout << buffer.substr(0, static_cast<size_t>(bytes_read)+1) << "\n";
bytes_read = read(cout_pipe[0], &buffer[0], buffer_size);
}
if(bytes_read == -1)
cout << "Failure reading from stderr pipe.\n";
posix_spawn_file_actions_destroy(&action);
}
उत्पादन होता है:
बाहर निकलें कोड: 0
तो मैं सब कुछ वास्तविक पाइपिंग को छोड़कर काम कर रहा है लगता है यह मेरा कोड है। यहाँ क्या गलत है? मुझे यह भी आश्चर्य है कि एक प्रतीक्षापिप लूप में पाइप बाइट्स पढ़ने का कोई तरीका है, लेकिन जब मैं कोशिश करता हूं, तो मूल प्रक्रिया असीम रूप से लटकती है।
मैंने कभी 'posix_spawnp' के बारे में नहीं सुना था। अच्छे पुराने 'पॉपन' के साथ क्या गलत है? यह एक दिलचस्प syscall/पुस्तकालय समारोह क्या है। यह याद रखेगा :) – sehe
'popen' में आवश्यक लचीलापन नहीं है: AFAICT मैं इसके साथ stderr को रीडायरेक्ट नहीं कर सकता। यह वह कार्य भी है जो 'CreateProcess' API जैसा दिखता है। हेक, एमएसडीएन में '_spawnvp' भी है जो 'posix_spawnp' जैसा दिखता है, लेकिन जैसा कि मैंने कहा, मेरा' CreateProcess' कोड मूल रूप से ठीक काम कर रहा है। यह यूनिक्स पक्ष है जो वर्तमान में सहयोग नहीं कर रहा है :( – rubenvb
मैंने रुचि बढ़ाने के लिए सी टैग जोड़ा। हालांकि कोड तकनीकी रूप से सी ++ है, हालांकि महत्वपूर्ण बिट्स (यानी पाइपिंग और स्पॉन्गिंग) शुद्ध सी – rubenvb