LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 12-03-2006, 08:42 AM   #1
acker
Member
 
Registered: Apr 2004
Location: Timisoara, Romania
Distribution: Debian
Posts: 90

Rep: Reputation: 15
Cool 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.

Last edited by acker; 12-03-2006 at 01:39 PM. Reason: RESOLVED!
 
Old 12-03-2006, 10:51 AM   #2
randyding
Member
 
Registered: May 2004
Posts: 552

Rep: Reputation: 31
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().
 
Old 12-03-2006, 11:19 AM   #3
acker
Member
 
Registered: Apr 2004
Location: Timisoara, Romania
Distribution: Debian
Posts: 90

Original Poster
Rep: Reputation: 15
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.
 
Old 12-03-2006, 12:34 PM   #4
randyding
Member
 
Registered: May 2004
Posts: 552

Rep: Reputation: 31
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.
 
Old 12-03-2006, 01:13 PM   #5
acker
Member
 
Registered: Apr 2004
Location: Timisoara, Romania
Distribution: Debian
Posts: 90

Original Poster
Rep: Reputation: 15
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
 
Old 12-03-2006, 01:28 PM   #6
acker
Member
 
Registered: Apr 2004
Location: Timisoara, Romania
Distribution: Debian
Posts: 90

Original Poster
Rep: Reputation: 15
Thumbs up 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

Last edited by acker; 12-03-2006 at 01:38 PM. Reason: found the answer
 
Old 12-03-2006, 04:42 PM   #7
randyding
Member
 
Registered: May 2004
Posts: 552

Rep: Reputation: 31
Yes indeed, after reading the other example that makes sense. Good find.
 
  


Reply

Tags
fork, linux, programming


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Output redirection??? ed_po Linux - Newbie 3 06-13-2006 01:17 PM
what is output redirection? LinuxPadawan General 5 05-18-2005 01:53 PM
Output redirection rmartine Linux - Newbie 2 01-25-2005 07:31 PM
output redirection to another directory spyghost Linux - Software 3 09-21-2003 10:28 AM
Output redirection legtester Linux - General 4 07-07-2003 03:36 PM


All times are GMT -5. The time now is 11:12 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration