Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

WHY?!

Name: Anonymous 2012-06-13 13:02


#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main(int argc, char **argv){

    int cnt = 0;

    for(int i = 0; i < 10; i++){
        fork();
        cnt++;
    }
    printf("PID:  %d\n", getpid());

    printf("Count:  %d\n", cnt);

    return 0;
}


Copy. Paste. Compile. Run.

I only want 10 childprocess. Why is this?

PS: Yes, I'm new to programming

Name: VIPPER 2012-06-13 13:16

I dont know why you want 10 child processes.

Name: Anonymous 2012-06-13 13:20

You're forking in the middle of a loop. The first forked instance is going to go through the loop nine more times itself; the second eight more times; etc.
Think before you write.

Name: Anonymous 2012-06-13 13:54

>>3

I should rather read man pages (but I get you).

autofacepalm.bmp

[quote=Man Pages]The fork() function shall create a new process. The new process (child process) shall be an exact copy of the  calling  process  (parent  process)[/quote]

What I understand is that childp. make at their time more chilps. How is this not a infinite loop?

And still, I want to create 10 childsp. for any reason. How do?

Name: Anonymous 2012-06-13 13:57

Aren't you supposed to check if you're a child process or the parent before you go into the loop to make a child of your own?

Name: VIPPER 2012-06-13 13:59

>>4
If the return value of fork() is 0, it means that the current process was forked.

Just do if (fork()) keepforking(); else exit(0);

Name: Anonymous 2012-06-13 14:08

Change your loop to something like this:
pid_t pid[10];
for(int i = 0; i < 10; i++){
        if ((pid[i] = fork()) < 0)
            /* Handle error. */
        else if (pid[i] == 0)
            /* Do child process stuff. */
        else
            /* Do parent process stuff. */
        cnt++;
    }

Name: Anonymous 2012-06-13 14:10

int main(int argc, char * argv[]){
    int cnt;
    int amChild = 0;
    for(cnt = 0; cnt < 10; ++cnt){
        if(!fork()){
            amChild = 1;
            break;
        }
    }
    if(amChild)
        printf("PID:  %d\n", getpid());
    else
        printf("Count:  %d\n", cnt);
    return 0;
}

Name: Anonymous 2012-06-13 14:18

>>8
DOESN'T ANYBODY CHECK FOR ERRORS ANY MORE?

Name: VIPPER 2012-06-13 14:40

>>9
I install a signal handler for every signal, the handler then restarts the program if one is caught.

Name: Anonymous 2012-06-13 14:45

>>8

So the break stops the childps. from making their own children? That easy? I don't get it quite well :-/

Name: Anonymous 2012-06-13 14:47

>>9
DOESN'T ANYBODY CHECK FOR DUBS ANY MORE?

>>10
Normally I just print an error message and then either kill the program or tries to continue, depending on what the error does. If I can, though, I try to actually handle the error - for example, if malloc returns NULL because of ENOMEM and I'm using memory for caching non-vital data, I free some of the cached data and try malloc again.

Name: Anonymous 2012-06-13 14:47

>>11
FUCK YOU.

Name: Anonymous 2012-06-13 15:02

>>11
It's because when you call fork(), the OS copies the process to a new part of memory* and then starts it at the instruction immediately following the call to fork(). So, when you call fork() in your example, the child process starts at cnt++;, goes back to re-evaluate the loop (i < 10) and then does another loop. Putting break fixes it because fork() returns zero to the child process (hence the if (!fork())), but it returns the PID of the child to the parent process. So, in the child process, if (!fork()) evaluates to true, so it executes the break instruction and exits the loop. In the parent process, if (!fork()) evaluates to false, so it continues in the loop.

* Actually, on most UNIXes, the memory is shared, and the kernel doesn't copy anything until one of the processes tries to write to the shared memory, which is called copy-on-write. You don't need to worry about it, though, because it's completely transparent.

Name: Anonymous 2012-06-13 16:26

>>9
No. What are you going to do? Print a message? If the system is that f**ked for resources, there's not much you can do. The first question that you should ask is if there is anything you can do about the error.

Don't change these.
Name: Email:
Entire Thread Thread List