c - Create process and anonymous pipe -
update question: have been able create process , programs compiled. however, run new problem. when try pipe source program filter program. doesn't seem feed in input sink program. there no error message. have test of standalone program using pipe operator in windows' cmd.
i'm trying small project learn anonymous pipe , create process. created 3 small standalone programs called source, filter, , sink. these 3 compiled , run fine. here's descriptions 3 standalone programs.
source: obtains source text-file filename commandline, opens file,and reads , copies file contents 1 character @ time directly standard output(stdout). when file has been copied, source terminates (closing of open file handles).
filter program not utilize filename commandline parameters. instead, filter reads text file standard input (stdin) , writes standard output (stdout) copy of input upper-case letters converted lower-case. filter must designed read 1 character, convert it, output it, , loop until incoming data finished.
sink program obtains destination text-file filename commandline, opens file writing, , reads characters 1 @ time standard input file (stdin) , writes each incoming character directly destination sink file.
next i'm driving main driver program separately creates 2 pipes , spawn 3 independent child inputs , outputs configured carry out indicated concurrent execution , dataflow. this:
- srcfile -> source -> pipe1 -> filter -> pipe2 -> sink -> destfile
the driver program requires 2 command line parameters:
c:\> driver.exe srcfile destfile
where srcfile existing data text-file, , destfile filename of new destination file created sink application.
here's code driver program. it's not finished yet. encountered hiccup when trying create process source program.
updated code:
#include <windows.h> #include <winbase.h> #include <stdio.h> #define delay_a_while() {volatile long j; for(j = 1; j< 10000; j++) ; } int main(int argc, char *argv[]) { handle hpiperead, hpipewrite, hpiperead2, hpipewrite2; startupinfo startupinfosource; startupinfo startupinfofilter; startupinfo startupinfosink; process_information procinfosource; process_information procinfofilter; process_information procinfosink; security_attributes pipeattributes; security_attributes pipeattributes2; char cmdline[200]; pipeattributes.nlength = sizeof(security_attributes); pipeattributes.lpsecuritydescriptor = null; //ignore pipeattributes.binherithandle = true; //child can inherit //create first pipe if (!createpipe(&hpiperead, &hpipewrite, &pipeattributes, 0)) { fprintf(stderr, "error creating pipe: %d\n", getlasterror()); exit(1); } sprintf_s(cmdline, 200, "source.exe %s", argv[1]); printf("create process: %s\n", cmdline); getstartupinfo(&startupinfosource); startupinfosource.dwflags = startupinfosource.dwflags | startf_usestdhandles; //mapping startupinfosource.hstdinput = getstdhandle(std_input_handle); startupinfosource.hstdoutput = hpipewrite; startupinfosource.hstderror = getstdhandle(std_error_handle); if (!createprocess( null, cmdline, null, null, true, create_new_console, null, null, &startupinfosource, &procinfosource)) { fprintf(stderr, "error creating child process: %d",getlasterror()); exit(1); } closehandle(hpipewrite); closehandle(procinfosource.hprocess); closehandle(procinfosource.hthread); pipeattributes2.nlength = sizeof(security_attributes); pipeattributes2.lpsecuritydescriptor = null; //ignore pipeattributes2.binherithandle = true; //child can inherit //create second pipe if (!createpipe(&hpiperead2, &hpipewrite2, &pipeattributes2, 0)) { fprintf(stderr, "error creating pipe: %d\n", getlasterror()); exit(1); } getstartupinfo(&startupinfofilter); startupinfofilter.dwflags = startupinfofilter.dwflags | startf_usestdhandles; //mapping startupinfofilter.hstdinput = hpiperead; startupinfofilter.hstdoutput = hpipewrite2; startupinfofilter.hstderror = getstdhandle(std_error_handle); sprintf_s(cmdline, 200, "filter.exe"); printf("create process: %s\n", cmdline); //filter getstartupinfo(&startupinfofilter); if (!createprocess( null, cmdline, null, null, true, create_new_console, null, null, &startupinfofilter, &procinfofilter)) { fprintf(stderr, "error creating child process: %d", getlasterror()); exit(1); } // int exitstatus; // getexitcodeprocess(procinfofilter.hprocess, &exitstatus); closehandle(hpiperead); closehandle(hpipewrite2); closehandle(procinfofilter.hprocess); closehandle(procinfofilter.hthread); getstartupinfo(&startupinfosink); startupinfosink.dwflags = startupinfosink.dwflags | startf_usestdhandles; //mapping startupinfosink.hstdinput = hpiperead2; startupinfosink.hstdoutput = getstdhandle(std_output_handle); startupinfosink.hstderror = getstdhandle(std_error_handle); sprintf_s(cmdline, 200, "sink.exe %s", argv[2]); printf("create process: %s\n", cmdline); getstartupinfo(&startupinfosink); if (!createprocess( null, cmdline, null, null, true, create_new_console, null, null, &startupinfosink, &procinfosink)) { fprintf(stderr, "error creating child process: %d", getlasterror()); exit(1); } closehandle(hpiperead2); closehandle(procinfosink.hprocess); closehandle(procinfosink.hthread); return 0; }
the program compiles fine. however, when try creates process, fails , exits. cmdline
value when parse in "source.exe test.txt", used execute standalone source program. can explain why createprocess
fail? because parse in wrong parameter?
the problem can see here possibility source.exe app not located in same directory driver.exe located. tried code , case when createprocess failed.
Comments
Post a Comment