LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   command line for child process (https://www.linuxquestions.org/questions/linux-newbie-8/command-line-for-child-process-881789/)

mesmserize 05-20-2011 07:13 AM

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

grail 05-20-2011 07:20 AM

You might need to give examples as I am struggling to understand the issue?

raskin 05-20-2011 07:35 AM

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.

mesmserize 05-22-2011 10:53 PM

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

raskin 05-22-2011 11:55 PM

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.

grail 05-23-2011 12:01 AM

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.

mesmserize 05-23-2011 04:13 AM

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...

raskin 05-23-2011 05:44 AM

fork doesn't execute any external command. It creates to copies of your process.
Code:

if (fork()) {
  /* parent process */
} else {
  /* child process */
}

read about execvp function..

MTK358 05-23-2011 08:20 AM

@mesmerize

Use code tags. Code is almost illegible without them.

schneidz 05-23-2011 08:24 AM

Quote:

Originally Posted by raskin (Post 4364386)
fork doesn't execute any external command. It creates to copies of your process.
Code:

if (fork()) {
  /* parent process */
} else {
  /* child process */
}

read about execvp function..

also, you mite find system() useful.


All times are GMT -5. The time now is 05:37 AM.