LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-20-2011, 08:13 AM   #1
mesmserize
LQ Newbie
 
Registered: May 2011
Posts: 3

Rep: Reputation: Disabled
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
 
Old 05-20-2011, 08:20 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,252

Rep: Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685
You might need to give examples as I am struggling to understand the issue?
 
Old 05-20-2011, 08:35 AM   #3
raskin
Senior Member
 
Registered: Sep 2005
Location: Russia
Distribution: NixOS (http://nixos.org)
Posts: 1,899

Rep: Reputation: 68
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.
 
Old 05-22-2011, 11:53 PM   #4
mesmserize
LQ Newbie
 
Registered: May 2011
Posts: 3

Original Poster
Rep: Reputation: Disabled
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
 
Old 05-23-2011, 12:55 AM   #5
raskin
Senior Member
 
Registered: Sep 2005
Location: Russia
Distribution: NixOS (http://nixos.org)
Posts: 1,899

Rep: Reputation: 68
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.
 
Old 05-23-2011, 01:01 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,252

Rep: Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685
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.
 
Old 05-23-2011, 05:13 AM   #7
mesmserize
LQ Newbie
 
Registered: May 2011
Posts: 3

Original Poster
Rep: Reputation: Disabled
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...

Last edited by mesmserize; 05-23-2011 at 05:24 AM.
 
Old 05-23-2011, 06:44 AM   #8
raskin
Senior Member
 
Registered: Sep 2005
Location: Russia
Distribution: NixOS (http://nixos.org)
Posts: 1,899

Rep: Reputation: 68
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..
 
1 members found this post helpful.
Old 05-23-2011, 09:20 AM   #9
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 720Reputation: 720Reputation: 720Reputation: 720Reputation: 720Reputation: 720Reputation: 720
@mesmerize

Use code tags. Code is almost illegible without them.
 
Old 05-23-2011, 09:24 AM   #10
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 5,027

Rep: Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845
Quote:
Originally Posted by raskin View Post
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Difference between Child THREAD and Child PROCESS whho Linux - Newbie 12 02-16-2015 01:22 AM
Command to find parent and child process? newbie01.linux Linux - Newbie 2 04-21-2011 06:33 AM
Under which circumstances a child process creates another child process using fork? mitsulas Programming 3 12-08-2009 09:16 AM
Which is the parent and child process in given command? brownaryan Linux - Newbie 1 06-30-2008 07:17 PM
How to kill a Child and all its subsequent child process in C shayer009 Programming 3 12-04-2007 01:40 AM


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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration