C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 09-21-2008, 11:59 AM   #1
subminimalist
 
MK27's Avatar
 
Join Date: Jul 2008
Location: NYC
Posts: 3,946
fork() question

why does this:

Code:
#include <stdio.h>

int main() {
        short int pid, i;
        for (i=0; i<=1; i++) {
                pid = fork();
                if (pid==0) printf("hello%d\n",i);
                else printf("pid: %d\n", pid);
                fflush(NULL);
        }   
}
produce this:
Code:
hello0
hello1
pid: 4790
pid: 4789
hello1
pid: 4791 
esp. this red bit...
__________________

Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS
MK27 is offline   Reply With Quote
Old 09-21-2008, 12:06 PM   #2
Registered User
 
Join Date: Oct 2001
Posts: 2,110
fork returns a pid_t, not a short int. To print a short int, use %hd. #include <unistd.h> for fork.

>fflush(NULL);

*sad face*
robwhit is offline   Reply With Quote
Old 09-21-2008, 12:10 PM   #3
and the hat of vanishing
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,214
The first child process doesn't exit, so it too runs a loop with i = 1
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
Up to 8Mb PlusNet broadband from only £5.99 a month!
Salem is offline   Reply With Quote
Reply

Tags
fork

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
A small question about fork( ) mlhazan C Programming 4 08-13-2008 08:18 PM
fork() question cstudent C Programming 3 04-25-2008 06:48 AM
Question sorting character arrays: C programming NeoSigma C Programming 3 05-23-2003 09:28 PM
Fork() child process question CrackDown Linux Programming 0 04-17-2003 11:58 AM
opengl DC question SAMSAM Game Programming 6 02-26-2003 09:22 PM


All times are GMT -6. The time now is 09:28 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22