के माध्यम से कमांड निष्पादित करते समय पाइप का उपयोग करके मैं एक नामहीन पाइप को कार्यान्वित करना चाहता हूं, और मुझे अपने किसी भी बच्चे में नहीं, मूल प्रक्रिया में आदेश निष्पादित करना होगा। प्रत्येक "-" एक पाइपलाइन ("|") के लिए एक कॉल के बराबर है, असाइनमेंट का हिस्सा भी मेरे पास यह कोड है। क्या कोई मुझे समझा सकता है कि यह क्यों काम नहीं करता है?माता-पिता
#include <stdio.h>
#include <dirent.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h> // for open flags
#include <time.h> // for time measurement
#include <assert.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
void my_exec(char* cmd, char** argv)
{
int pipefd[2], f;
if (pipe(pipefd) < 0)
perror("pipe creation failed");
f = fork();
assert(f >= 0);
if (f == 0) {
// inside son process - connecting STDOUT to pipe write
if (dup2(pipefd[1], STDOUT_FILENO) < 0)
perror("dup2 failed");
close(pipefd[0]);
close((int)stdout);
} else {
// inside parent process - connecting STDIN to pipe read and execute command with args
if (dup2(pipefd[0], STDIN_FILENO) < 0)
perror("dup2 failed");
close(pipefd[1]);
close((int)stdin);
if (execvp(cmd, argv) < 0)
perror("execvp failed");
}
}
int main(int argc, char** argv)
{
assert(strcmp(argv[argc-1], "-"));
int i;
for (i = 1; i < argc; ++i) {
if (!strcmp(argv[i], "-")) {
argv[i] = NULL;
my_exec(argv[1], &argv[1]);
argv = &argv[i];
argc -= i;
i = 0;
}
}
char* args[argc];
args[argc-1] = NULL;
for (i = 1; i < argc; ++i) {
args[i-1] = argv[i];
}
if (execvp(args[0], args) == -1)
perror("execvp failed");
return;
}
आदेश:
./mypipe.o ls -l - grep "pipe"
रिटर्न
total 24
-rw-rw-r-- 1 omer omer 1463 May 23 19:38 mypipe.c
-rwxrwxr-x 1 omer omer 7563 May 23 19:37 mypipe.o
-rw-rw-rw- 1 omer omer 873 May 23 20:01 nice.c
-rwxrwxr-x 1 omer omer 7417 May 23 19:44 nice.o
-rw-rw-r-- 1 omer omer 0 May 23 17:10 try
जो obviouslly पाइप फ्लॉप काम का मतलब है ... किसी भी विचार?
मुझे यकीन है कि np_exec को प्रत्येक कॉल एक भी बच्चे प्रक्रिया है कि बहस के बाकी पार्स करने के लिए जारी है शुरू होता है, जबकि मूल जनक प्रक्रिया निष्पादित करता दिए गए कार्यक्रम और तर्क (execvp का प्रयोग करके) बनाने की जरूरत है
संपादित करें: मुझे लगता है कि मुझे गलती मिली है: मैं पाइप के "पढ़ने-लिखने" सिरों को स्विच करता हूं।
सही समारोह:
void np_exec(char* cmd, char** argv)
{
int pipefd[2];
int file;
if (pipe(pipefd) < 0)
perror("failed to create pipe");
file = fork();
assert(file >= 0);
if (file != 0) {
// inside parent process - connecting STDOUT to pipe write and execute command with args
if (dup2(pipefd[WRITE], STDOUT_FILENO) < 0)
perror("the function dup2 failed");
close(pipefd[READ]);
close((int)stdout);
if (execvp(cmd, argv) < 0)
perror("the function execvp failed");
} else {
// inside son process - connecting STDIN to pipe read
if (dup2(pipefd[READ], STDIN_FILENO) < 0)
perror("the function dup2 failed");
close(pipefd[WRITE]);
close((int)stdin);
}
}
प्रतीक्षा: परीक्षण किया है और काम कर रहे -
यहाँ कोड है? –
मुझे यह सुनिश्चित करने की ज़रूरत है कि प्रत्येक कॉल np_exec एक एकल बाल प्रक्रिया शुरू करता है जो शेष तर्कों को पार्स करना जारी रखता है, जबकि मूल मूल प्रक्रिया निम्न प्रोग्राम और तर्क (execvp का उपयोग करके) निष्पादित करती है, – mike
"जबकि मूल मूल प्रक्रिया निष्पादित होती है दिए गए कार्यक्रम और तर्क (execvp का उपयोग करके "मुझे नहीं लगता कि यह समझ में आता है: यदि अभिभावक प्रक्रिया execvp को कॉल करती है, तो उसे जो भी execvp कॉलिंग समाप्त हो जाएगा, उसे बदल दिया जाएगा। –