Issue when fork()ing processes


 
Thread Tools Search this Thread
Top Forums Programming Issue when fork()ing processes
# 1  
Old 08-30-2012
Issue when fork()ing processes

Hi guys!
I'll simplify my problem. I have the following code:

Code:
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <signal.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/wait.h>
#define max 25
#define buffdim 50

void p1();
void p2();
void wait_child();
void done(int sinal);

int main(int argc, char** argv)

{
int pid[1], i,j;

    for (i = 0; i < 2; i++)
    {

        pid[i] = fork();

        if(pid[i]==0)
        {
            if(i==0)
            {
            printf("I'm %d and i=0\n", pid[i]);
            p1();
            }
            else if (i==1)
            {
            printf("I'm %d and i=1\n", pid[i]);
            p2(argv[1]);
            }
        }
        else
        {
        printf("P0 : Created child %d\n", i + 1,pid[i]);
        }
    }
    for (j=0; j<2; j++)
    {
    wait_child();
    }

    return 0;
}

void p1()
{
    signal(SIGUSR1, done);
}

void p2(char* file_in)
{
int fd;
int a=8, c=10;

    
    printf("%d e %d.\n", a,c);
    exit(0);
}    


void wait_child()
{
    int death, status;
    death=wait(&status);
    printf("Child %d dead. Status %d.\n", death, status);
}

void done(int sinal)
{
    printf("Done\n");
}

In my head I created a for cicle that supposed to fork 2 sub processes. But, intstead of it it forks 3!!

I putted all these printf's in the middle of the for cicle to understand what he was doing, but even with the print'fs I wasn't able to do so.

what I get running the program is:

Code:
piero@ubuntu:~$ ./correggi test_input a
P0: Created child 1
I'm 0 and i=0
P0: Created child 2
P0: Created child 2
I'm 0 and i=1
8 e 10.
Child 2457 dead. Status 0.
I'm 0 and i=1
8 e 10.
Child 2458 dead. Status 0.
Child -1 dead. Status 0.
Child 2456 dead. Status 0.
piero@ubuntu:~$

Could anyone explain me what is going on ? why does he forks a 3rd process and a -1 process ??? Is the sub process getting inside the for cicle and forking someone else ?
# 2  
Old 08-30-2012
Remember, both copies are identical except for the return value of fork(). There's absolutely nothing stopping them from continuing the loop if you don't stop them yourself.

You should make your children exit() once they're done what they need to do.
This User Gave Thanks to Corona688 For This Post:
# 3  
Old 08-30-2012
Quote:
Originally Posted by Corona688
Remember, both copies are identical except for the return value of fork(). There's absolutely nothing stopping them from continuing the loop if you don't stop them yourself.

You should make your children exit() once they're done what they need to do.
Thanks Corona688. The problem was caused by the first forked process. There was no pause() or exit() so he kept looping.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Making processes using fork

Can anyone help me with this? Create a parent process that gets from the command line n arguments arg1, arg2, ... , argn. The parent will create n/3 son processes, each of them will create a file with the name argi by concatenate the files argi+1 and argi+2. How can i concatenate those... (1 Reply)
Discussion started by: bunicu01
1 Replies

2. Programming

Creating more processes with fork()

Hello people I need help How to make ONE process to create MORE (not one) processes with fork(). I tried several codes but do not work. Thanks (8 Replies)
Discussion started by: nekoj
8 Replies

3. Shell Programming and Scripting

fork processes

Hi, How to count how many processes opened by fork function in perl. Thanks (10 Replies)
Discussion started by: Anjan1
10 Replies

4. Programming

fork(), parent and child processes???

Hi friends, I have a small question regarding unix system call fork, I hope you will solve my problem. Here is the small program $ cat fork1.c #include <stdio.h> #include <unistd.h> #include <sys/types.h> int main() { int pid; int x = 0; x = x + 1; pid = fork(); if(pid < 0) {... (2 Replies)
Discussion started by: gabam
2 Replies

5. UNIX for Dummies Questions & Answers

Conserving processes: execve() vs fork()

Disclaimer: This is just a rainy day experiment. There is no expected "goal" other than to understand UNIX better. After reading about fork and exec, my understanding is that forking, as the UNIX shell does by design, consequentially may sacrafice some speed versus an approach that runs in... (1 Reply)
Discussion started by: uiop44
1 Replies

6. SCO

-sh: fork failed - too many processes in sco unix 5.0.5

Dear experts, I have done a re-installation of sco unix openserver 5.0.5 and managed to create users. The problem am facing is that of one user logging in more than 5 times. How can i overcome this problem. the system give the error below. -sh: fork failed - too many processes in sco unix... (5 Replies)
Discussion started by: njoroge
5 Replies

7. Shell Programming and Scripting

grep'ing and sed'ing chunks in bash... need help on speeding up a log parser.

I have a file that is 20 - 80+ MB in size that is a certain type of log file. It logs one of our processes and this process is multi-threaded. Therefore the log file is kind of a mess. Here's an example: The logfile looks like: "DATE TIME - THREAD ID - Details", and a new file is created... (4 Replies)
Discussion started by: elinenbe
4 Replies

8. Shell Programming and Scripting

fork() and child processes

Hello, How many child processes are actually created when running this code ? #include <signal.h> #include <stdio.h> int main () { int i ; setpgrp () ; for (i = 0; i < 10; i++) { if (fork () == 0) { if ( i & 1 ) setpgrp () ; printf ("Child id: %2d, group: %2d\n",... (1 Reply)
Discussion started by: green_dot
1 Replies

9. Programming

fork() and child processes

Hello, How many child processes are actually created when running this code ? #include <signal.h> #include <stdio.h> int main () { int i ; setpgrp () ; for (i = 0; i < 10; i++) { if (fork () == 0) { if ( i & 1 ) setpgrp () ; printf ("Child id: %2d, group: %2d\n", getpid(),... (0 Replies)
Discussion started by: green_dot
0 Replies

10. Programming

fork()ing hell!!

Hello I am having serious trouble with the fork command, i basically want to create 9 or 10 child processes and store their pid numbers in array while the children stay resident until i kill() them later , i cannot seem to control how many are made as they all seem to create their own children. ... (16 Replies)
Discussion started by: theultimatechuf
16 Replies
Login or Register to Ask a Question