4 Replies - 656 Views - Last Post: 25 January 2009 - 09:34 PM Rate Topic: -----

#1 jynx09  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 25-January 09

problem parsing argc to a child process function

Posted 25 January 2009 - 05:56 PM

Im having trouble parsing a command line argument argc[1] to a function, in which this function is handling all of my child processes
specifically my first child is to do a file copy of the first command line argument to some arbitrary text file... but im having trouble with passing argc[1] to it so it knows that file to copy from

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/wait.h>
#include <errno.h>
#include <stdlib.h>

#define NUM_CHILDREN 3

void parentfunction(int [], int);		// function prototypes
void childfunction(int);

int main ( int argc, char *argv[] )
{
	int childarray[NUM_CHILDREN];
	int parental, i;


	if ( argc != 2 ) /* argc should be 2 for correct execution */
	{
		/* We print argv[0] assuming it is the program name */
		printf( "ERROR :: usage: %s filename\n", argv[0] );
	}
	else
	{
		for (i = 0; i < NUM_CHILDREN; i++)	  // making 3 children with fork
		{
			if((parental = childarray[i] = fork()) == -1)  // error has occoured
			{
				printf("Error occoured with fork()\n");
				exit(99);						// exit status 99
			}
			if(!parental)
			{
				break;
			}
		}			// end of creating children

		if(parental)
			parentfunction(childarray, i);
		else
			childfunction(i);
	}  // end of elese
}	// end of main


void parentfunction(int _childarray[], int n)
{
	int p, exitstatus1, exitstatus2, exitstatus3;

	printf("\nI am the father of the following: %d, %d and %d\n", _childarray[0], _childarray[1], _childarray[2]);
	/* parent is going to wait for all of its children to finsh*/
	exitstatus1 = waitpid(_childarray[0], NULL, 0);
		printf("child, id = %d ended with status %d\n", _childarray[0], exitstatus1);
	exitstatus2 = waitpid(_childarray[1], NULL, 0);
		printf("child, id = %d ended with status %d\n", _childarray[1], exitstatus2);
	exitstatus3 = waitpid(_childarray[2], NULL, 0);
		printf("child, id = %d ended with status %d\n", _childarray[2], exitstatus3);

	printf("\n**Good Bye!**\n");
}


void childfunction(int n)
{
	FILE *in, *out;
	char ch;
	if (n = 0)	  // 1st child *file copy*
	{

	  if((in=fopen(argv[1], "rb")) == NULL) {
		printf("Cannot open input file.\n");
		exit(1);
	  }
	  if((out=fopen("c:\\newcopy.txt", "wb")) == NULL) {
		printf("Cannot open output file.\n");
		exit(1);
	  }

	  while(!feof(in)) {
		ch = getc(in);
		if(ferror(in)) {
		  printf("Read Error");
		  clearerr(in);
		  break;
		} else {
		  if(!feof(in)) putc(ch, out);
		  if(ferror(out)) {
			printf("Write Error");
			clearerr(out);
			break;
		  }
		}
	  }
	  fclose(in);
	  fclose(out);
	printf("\nI am kid #: %d my id is %d\n", n+1, getpid());
	fflush(stdout);
	exit(3);						// arbitrary exit value
	}
}



can anybody help me, or is there a better method to doing this....
i tried it without functions but it got very confusing with the amount of children / how to process them all at once

Thanks for any help in advance

Is This A Good Question/Topic? 0
  • +

Replies To: problem parsing argc to a child process function

#2 jynx09  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 25-January 09

Re: problem parsing argc to a child process function

Posted 25 January 2009 - 07:04 PM

i tried a complelty different method and still no luck
Was This Post Helpful? 0
  • +
  • -

#3 Hyper  Icon User is offline

  • Banned

Reputation: 108
  • View blog
  • Posts: 2,129
  • Joined: 15-October 08

Re: problem parsing argc to a child process function

Posted 25 January 2009 - 09:21 PM

Are you attempting to spawn new instances of command consoles ("Childs") using the argument list parameters?

If you're using Windows, here's one method you could attempt to use:
- http://msdn.microsof...944(VS.85).aspx

Here's a list of console commands (MSDN reference):
- http://msdn.microsof...073(VS.85).aspx

Hope this helps.
Was This Post Helpful? 0
  • +
  • -

#4 jynx09  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 25-January 09

Re: problem parsing argc to a child process function

Posted 25 January 2009 - 09:30 PM

no, inside my child im going to preform a file trasnfer, and the file that is being copied from is specified on the command line

./finename inputfile.txt

but im not sure how to get this into my child function (inputfile.txt) this is coming from the user
Was This Post Helpful? 0
  • +
  • -

#5 Hyper  Icon User is offline

  • Banned

Reputation: 108
  • View blog
  • Posts: 2,129
  • Joined: 15-October 08

Re: problem parsing argc to a child process function

Posted 25 January 2009 - 09:34 PM

I'm not sure you understand the terminology behind "Child process."
Here's something else from the MSDN site you might find useful:
- http://msdn.microsof...499(VS.85).aspx

Redirects input from one console to another (via child console (process)).
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1