views:

178

answers:

6

What is the problem with this code? How to solve it? Parent processes goto in if or child process?

first code produce zombie process or second code or both or non ?

#include <signal.h>
#include <sys/wait.h>
main() {
  for (;;) {
    if (!fork()) {
      exit(0);
    }
    sleep(1);
  }
}

what about this code :

#include <signal.h>
#include <sys/wait.h>
main() {
  for (;;) {
    if (fork()) {
      exit(0);
    }
    sleep(1);
  }
}
+2  A: 

I don't know what the problem is, because I don't know what you want it to do. What it does do is fork a child, which then exits (successfully), every second.

When fork completes successfully, it returns 0 to the child, and the pid of the child to the parent. So it's the child that enters the if body in the first program, and the parent in the second program.

The first creates an infinite number of zombie processes, because the parent lives forever but never waits. The second does not because the parent exits immediately, so the child is orphaned and adopted by init.

EDIT: Addressed update to question.

Matthew Flaschen
I think the problem is a homework question roughly along the lines of "first code produce zombie process or second code or both or non ?"
danben
+1  A: 

Upon successful completion, fork() returns a value of 0 to the child process and returns the process ID of the child process to the parent process. Otherwise, a value of -1 is returned to the parent process, no child process is created, and the global variable errno is set to indi- cate the error.

This program creates child processes every second which immediately quit.

Put another way, the child process "goes into the if." You could check by adding a printf statement inside the if.

Potatoswatter
A: 

RETURN VALUE

Upon successful completion, fork() shall return 0 to the child process and shall return the process ID of the child process to the parent process. Both processes shall continue to execute from the fork() function. Otherwise, -1 shall be returned to the parent process, no child process shall be created, and errno shall be set to indicate the error.

=================

if (!fork()) equals to     if (0 == fork())

What did you mean, not to create processes after one successfully created? Seems like a problem...

AlexanderVX
+3  A: 
  • The code is not C99 code
  • The code does not include the correct headers
  • The code forks a child once a second; the child immediately exits (indicating success)
  • The program doesn't stop until interrupted by a signal
  • Because the code does not wait for its children, and it does not ignore SIGCHLD signals, the process accumulates zombies - processes that would be cleaned up if only their parent waited for them.

    #include <stdlib.h>
    #include <unistd.h>
    int main(void)
    {
        for (;;)
        {
            if (fork() == 0)
                exit(0);
            sleep(1);
        }
        /* Unreached */
        return(0);
    }
    

The alternative code has the parent exit successfully, while the child goes to sleep for a second, and then forks and commits suicide (leaving a new child to continue the process).

Jonathan Leffler
first produced zombie or second ? or both ? or non ?
Pureth
@sb2367: the first code produces zombies because the parent forks off children and never waits for them. The second does not produce zombies; the parent dies, and the 'init' process (PID = 1) collects the dead bodies (more precisely, the shell collects the original parent when it dies; the init process collects the remainder).
Jonathan Leffler
@Jonathan Leffler - I don't mean to tell you what to do but I think when the OP is so blatantly looking for an answer that he can write down and turn in without having to read or understand it, it is not in the spirit of SO to indulge him.
danben
@danben: yeah, it's always a risky balancing equation...and I may have erred on the side of too informative.
Jonathan Leffler
A: 

Can I suggest you comment out the for loop and insert a few printf statements (or step through with gdb). That'll give you an insight into what's going on. Or bound the for loop with something like:

int i = 0;
for ( i = 0; i < 5; i++ )

rather than using an infinite loop so that it is easier to understand/debug.

Also read up on fork() and how it works. Then you'll not just be able to answer the question but understand why these answers apply and be able to avoid making mistakes in your own software.

I would suggest leaving the sleep statement in place.

I suspect the point of this homework is that you try it by modifying it and understand what is and isn't safe to do on your system.

Ninefingers
+1  A: 

From your command line type

man 2 fork

and it will tell you everything you want to know.

nategoose