Hi

please can anyone run this code for me in Linux .. i want to know what's the output

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

int main(){
  for (i = 0; i < 3; i++) 
  { 
     if (fork() != 0) 
      {
        printf("ONE");
        if (fork() == 0)   { printf("TWO"); }
        else { printf("THREE");
             exit(0); }
       } else printf("FOUR"); 
  }
}

thank you

If this is homework, then tell us what do you think the output will be and I might test it.

[edit] on second look: I already spot one problem: 'i' is not declared, so it won't even compile

If this is homework, then tell us what do you think the output will be and I might test it.

[edit] on second look: I already spot one problem: 'i' is not declared, so it won't even compile

Absolutely right :)

Edit:: Avoid using exit(0); in this case you're better off with return 0;

prabakar@prabakar-desktop:~$ gcc test.c
prabakar@prabakar-desktop:~$ ./a.out
FOURFOURFOURFOURFOURONETWOFOURONETWOFOURONETWOFOURFOURFOURFOURONETHREEFOURONE
THREEFOURONETWOONETWOONETHREEprabakar@prabakar-desktop:~$ ONETWOONETWOFOURONETWOFOURONETWOFOURONETWOONETHREEONETWOONETHREEONETWOONE
TWOONETWOONETWOFOURONETHREEONETWOONETWOONETHREE

I made those corrections stated by others

Alright, now run it again. I'll bet you 20 fictional bucks that it won't output that exact same thing again :) (well eventually it will once every gazillion times or so)

Alright, now run it again. I'll bet you 20 fictional bucks that it won't output that exact same thing again :) (well eventually it will once every gazillion times or so)

prabakar@prabakar-desktop:~$ ./a.out
FOURFOURFOURFOURFOURONETWOFOURONETWOFOURONETWOFOURFOURFOURFOURONETHREEFOURONE
THREEFOURONETWOONETWOONETHREEprabakar@prabakar-desktop:~$ ONETWOFOURONETWOONETWOONETWOFOURFOURONETWOONETHREEONETWOONETHREEONETWOONETWO
ONETWOONETWOFOURONETHREEONETWOONETWOONETHREE
prabakar@prabakar-desktop:~$ ./a.out
FOURFOURFOURFOURFOURONETWOFOURONETWOFOURONETWOFOURFOURFOURFOURONETHREEFOURONE
THREEFOURONETWOONETWOONETHREEprabakar@prabakar-desktop:~$ ONETWOFOURONETWOONETWOONETWOFOURFOURONETWOONETHREEONETWOONETHREEONETWOONETWO
ONETWOONETWOFOURONETHREEONETWOONETWOONETHREE
prabakar@prabakar-desktop:~$ ./a.out
FOURFOURFOURFOURFOURONETWOFOURONETWOFOURONETWOFOURFOURFOURFOURONETHREEFOURONE
THREEFOURONETWOONETWOONETHREEprabakar@prabakar-desktop:~$ ONETWOFOURONETWOONETWOONETWOFOURFOURONETWOONETHREEONETWOONETHREEONETWOONETWO
ONETWOONETWOFOURONETHREEONETWOONETWOONETHREE

Now what was the bet? :D
To tell the truth I did not even care to read the program at first and I would admit that yes, indeed, I cannot assure that, it would be the answer every time.

commented: Here's 20 bucks :( +16

Now what was the bet? :D
To tell the truth I did not even care to read the program at first and I would admit that yes, indeed, I cannot assure that, it would be the answer every time.

I wasn't expecting that... Didn't you just paste the same thing a few times? ;)

heh, how about a screen shot?

Now I am surprised too. Its not changing, $-)

It appears that fork is waiting for the child to finish before continuing with the program, which wasn't what I expected. But after reading the man-pages I must conclude that the parent will wait for the child to die before continuing, so the output will actually always be predictable... I was wrong :(

With that said, the assignment in this question is a real b*stard to do from the top of your head, your teacher deserves spanking :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.