3 Replies - 1143 Views - Last Post: 11 April 2013 - 08:20 AM Rate Topic: -----

#1 hdr  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 150
  • Joined: 15-December 08

How do I exit two child processes in C?

Posted 09 April 2013 - 08:18 AM

Geez. It seems whenever I'm back here, it's always because I have a problem with my schoolwork.

Basically, my task is to "Write a program in C that fork() 2 child and wait for the 2 child to exit,
and then the program will output the exit status of both child.", and I was given this as my refernce:

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

int main(void)
{
	pid_t pid;
	int rv;

	switch(pid = fork())
	{
		case -1:
			perror("fork");
			exit(1);

		case 0:
			printf(" CHILD: This is the CHILD process!\n");
			printf(" CHILD: My PID is %d\n", getpid());
			printf(" CHILD: My parent's PID is %d\n", getppid());
			printf(" CHILD: Enter my exit status (and make it small): ");
			scanf(" %d", &rv);
			printf(" CHILD: Bye!\n");
			exit(rv);
		
		default:
			printf("PARENT: This is the parent process.\n");
			printf("PARENT: My PID is %d\n", getpid());
			printf("PARENT: My child's PID is %d\n", pid);
			printf("PARENT: Waiting for child to exit()...\n");
			wait(&rv);
			printf("PARENT: My child's exit status is: %d\n", WEXITSTATUS(rv));
			printf("PARENT: Bye!\n");
		}

		return 0;
}



Which results in the output:

~ $ ./Exercise1.out
PARENT: This is the parent process.
PARENT: My PID is 6894
 CHILD: This is the CHILD process!
PARENT: My child's PID is 6895
PARENT: Waiting for child to exit()...
 CHILD: My PID is 6895
 CHILD: My parent's PID is 6894
 CHILD: Enter my exit status (and make it small): 1
 CHILD: Bye!
PARENT: My child's exit status is: 1
PARENT: Bye!



My solution to the problem so far is this:

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

void forkChildren (int nChildren)
{
	int i;
	int rv;
	pid_t pid;
	
	for (i = 1; i <= nChildren; i++)
	{
		pid = fork();
		if (pid == -1)
		{
			perror("fork");
			exit(1);
		}
		if (pid == 0)
		{
			printf("\nI am child number %d , and my PID is: %d\n", i, getpid());
			printf("\nAnd my parent ID is %d\n", getppid());	
			sleep (2);
			return;
		}
		else
		{
			printf("\nThis is the parent process. My PID is %d\n", getpid());
		}
	}
}

int main(int argc, char *argv[])
{
	if (argc < 2)
	{
		forkChildren(2);
	}
	else
	{
		forkChildren (atoi(argv[1]));
	}
	return 0;
}


Which results in:

$ ./Exercise1ba.out

This is the parent process. My PID is 6985

I am child number 1 , and my PID is: 6986

And my parent ID is 6985


I am child number 2 , and my PID is: 6987

And my parent ID is 6985
This is the parent process. My PID is 6985



So what I would like to know is how do I implement the exits
in this case, as I'm unsure on where to place it inside
the code. I've searched for a solution in my textbook (The Definite Guide
To Linux Network Programming) and the internet, to no avail, so
any help is greatly appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: How do I exit two child processes in C?

#2 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 653
  • View blog
  • Posts: 2,240
  • Joined: 31-December 10

Re: How do I exit two child processes in C?

Posted 09 April 2013 - 08:35 AM

You shouldn't use return or exit from the child. The reason for this is that exit will perform cleanup and then call the _exit() function. Call the _exit() function instead. Also, have a look at the man pages for the wait() command, if you look at the reference you posted, the wait() function is used to wait for the child to close. The man pages have a few examples to go by as well. Specifically, man 2 wait, should help you.

This post has been edited by vividexstance: 09 April 2013 - 08:36 AM

Was This Post Helpful? 0
  • +
  • -

#3 hdr  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 150
  • Joined: 15-December 08

Re: How do I exit two child processes in C?

Posted 09 April 2013 - 09:15 PM

I tried looking into the man page, but I'm afraid that I couldn't understand it that well. Do you have any other examples that I can check out?
Was This Post Helpful? 0
  • +
  • -

#4 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 653
  • View blog
  • Posts: 2,240
  • Joined: 31-December 10

Re: How do I exit two child processes in C?

Posted 11 April 2013 - 08:20 AM

Ya on the man pages. Type "man 2 wait" without the quotes on the command-line and look towards the bottom.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1