LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   fork(), different output with/without redirection (https://www.linuxquestions.org/questions/programming-9/fork-different-output-with-without-redirection-507078/)

acker 12-03-2006 07:42 AM

fork(), different output with/without redirection. RESOLVED!
 
hello,

[LATER EDIT:]
Better check for a shorter and cleaner example: http://www.linuxquestions.org/questi...84#post2528684
and the answer.
[/LATER]


Code:

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


int
main(void)
{
    int i = 0;

    for (i = 0; i < 2; i++) {
        fork();
        if (i == 0)
            printf("After f %d\n", getpid());
        sleep(1);
    }

    return 0;
}

Code:

acker@dido /tmp $ ./a.out
After f 19961
After f 19960
acker@dido /tmp $ ./a.out | cat
After f 19966
After f 19966
After f 19964
After f 19964

If i redirect the output to a file I get the same output as with the pipeline. Any ideas or pointers (to a specialised list/forum) are welcomed.

randyding 12-03-2006 09:51 AM

You're doing several things completely incorrect, its hard to know where to start. May I suggest a book that I like to tell people about, it covers in great detail this subject with lots of examples.
Advanced Programming in the UNIX Environment by W. Richard Stevens.

Your main problem is you are not checking the return code of fork().

acker 12-03-2006 10:19 AM

Quote:

Originally Posted by randyding
You're doing several things completely incorrect, its hard to know where to start. May I suggest a book that I like to tell people about, it covers in great detail this subject with lots of examples.
Advanced Programming in the UNIX Environment by W. Richard Stevens.

Your main problem is you are not checking the return code of fork().

What things am I doing so wrong?

My example is short and I think it should do the following:
fork the main process in two (parent, and child_1) and then each process should fork in two also: parent, child_2 + child_1, child_child_1).

If it will return (-1) I will not have this number of processes but I don't care cause in this simple example is not the case.

randyding 12-03-2006 11:34 AM

Hi,
With fork(), it returns 0 in the child and the pid of the child in the parent. If you don't read the return code of fork() there is no reason to call it because your program doesn't know if you are the parent or the child.
That's why I suggested the book because it goes into the theory of what the useful applications of fork() are, and there is no useful purpose if your software doesn't know when you are running as parent or child process after forking.

Calling fork() in a loop like that is very strange, and I'm not surprised strange results happen. I've never seen that done before and can't understand what purpose it serves because it doesn't do anything useful.

acker 12-03-2006 12:13 PM

Quote:

Originally Posted by randyding
Hi,
With fork(), it returns 0 in the child and the pid of the child in the parent. If you don't read the return code of fork() there is no reason to call it because your program doesn't know if you are the parent or the child.

I don't want to know if i'm the parent or in the child. For me they are just processes. (in this example). I may have other mechanisms to synchronize but that is beyond the problem presented here.

Quote:

That's why I suggested the book because it goes into the theory of what the useful applications of fork() are, and there is no useful purpose if your software doesn't know when you are running as parent or child process after forking.
Educational.

Quote:

Calling fork() in a loop like that is very strange, and I'm not surprised strange results happen. I've never seen that done before and can't understand what purpose it serves because it doesn't do anything useful.
For example
Code:

int
main(void)
{
  3; 4; 5;
  return 0;
}

compiles ok because is a valid C program (warnings if -Wall is used). Does nothing but does not have "strange effects" although it may seem strange.

Ok, code rewritten ;)

Code:

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


int
main(void)
{
    int i = 0;
    int res = 0;

    for (i = 0; i < 2; i++) {
        res = fork();
        if (res == -1) {
            printf("Error, can't create the little baby\n");
            exit(1);
        } else if (res == 0) {
            if (i == 0) {
                printf("Kid: after f %d\n", getpid());
            }
        } else {
            if (i == 0)
                printf("Parrent: after f %d\n", getpid());
        }
        sleep(1);
    }

    return 0;
}

Approximately same output...
Thanks for trying to help me but I think the "problem" is somewhere else (in my incorrect understanding of something or/and a race condition of some sort/buffer issue).

I think I will try the gcc list (although this is not a compiler issue; hope I won't get too burned :) )

--ack

acker 12-03-2006 12:28 PM

GOT THE ANSWER.. yuppy!! :)
 
Code:

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>

int
main(void)
{

    printf("Y\n");
    fork();
    printf("X\n");
    return 0;
}

Code:

acker@dido /tmp $ ./a.out
Y
X
X
acker@dido /tmp $ ./a.out | cat
Y
X
Y
X

I believe now that is some buffer related stuff. Still need to know exactly what's happening.

This code has the same output with or without the "| cat| in the command:
Code:

int
main(void)
{

    fork();
    printf("X\n");
    return 0;
}


[LATER EDIT]
After realizing this I've found
http://www.linuxforums.org/forum/147741-post6.html . I do have the book but it's almost 700 pages so.. :)

Thanks and have a fluffy day ;)

randyding 12-03-2006 03:42 PM

Yes indeed, after reading the other example that makes sense. Good find.


All times are GMT -5. The time now is 06:20 PM.