command line for child process
hi
i have created three child process from one parent. And different child has different functions. Child 2 has got function to load file called "wc" to count file1 and and its required to get their files by command line arguments. I can get the files through command line but couldn't get the files when child 2 process start. Any help will be apprecited |
You might need to give examples as I am struggling to understand the issue?
|
Read man page for execvp (or what do you use to launch wc?). You can pass an array of null-terminated strings to it to make it child process arguments. You need to declare the array of pointers, allocate the strings and fill them in the general case.
|
my child //
if ( (newpid = fork()) == -1 ) { perror("Cannot Create"); } else if ( newpid == 0 ) { child_average(); } else { wait_rv=wait(NULL); if((newpid_2 = fork()) == -1) { printf("Cannot Process\n"); } else if (newpid_2 == 0) { child_2(); } else { wait_child2=wait(NULL); if((newpid_3 = fork()) == -1) { printf("Cannot Process\n"); } else if (newpid_3 == 0) { child_3(); } else { parent_code(); } } } // Code got from google to get file from command line #include <stdio.h> int main ( int argc, char *argv[] ) { if ( argc != 2 ) /* argc should be 2 for correct execution */ { /* We print argv[0] assuming it is the program name */ printf( "usage: %s filename", argv[0] ); } else { // We assume argv[1] is a filename to open FILE *file = fopen( argv[1], "r" ); /* fopen returns 0, the NULL pointer, on failure */ if ( file == 0 ) { printf( "Could not open file\n" ); } else { int x; /* read one character at a time from file, stopping at EOF, which indicates the end of the file. Note that the idiom of "assign to a variable, check the value" used below works because the assignment statement evaluates to the value assigned. */ while ( ( x = fgetc( file ) ) != EOF ) { printf( "%c", x ); } fclose( file ); } } } I need to read the file from commandline when child 2 process.. Thank You |
You post a huge chunk of code. You have better chances of getting useful advice if you tell us what it does, what you expect it to do, what it doesn't do and what it does wrong.
|
Might also be nice, as you didn't originally, tell everyone which language you are working in (seeing I thought we were just talking about the command line)?
Also, +1 to what raskin has said, but please put code in [code][/code] tags so we can read and follow the code. |
sorry guys...i just tried to show you what I did.
Basically I have got three child... 1. Child 1 2. Child 2 3. Child 3 when i run the program, child 1 process. lets say print any average number I just don't know how to give command line for child 2.. when child 2 process we have to give the destination of the file through command line I am doing C programming in linux... |
fork doesn't execute any external command. It creates to copies of your process.
Code:
if (fork()) { |
@mesmerize
Use code tags. Code is almost illegible without them. |
Quote:
|
All times are GMT -5. The time now is 05:37 AM. |