How else-if and else executing at same time

Here is the program Our teacher discussed in Class.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

int main(void)
{
    pid_t pid;
    pid = fork();
    if (pid < 0) {
       printf("error\n");
       return 1;
    } else if (pid == 0) {
       execlp("/bin/ls", "ls", NULL);
    } else {
       wait(NULL);
       printf("Child complete");
    }
    return 0;
}

The question here is how it executes both statements in else-if and else part.As purpose of these statement if anyone will execute the else will never execute that is the reason python language provide finally statement so it execute always.
Thanks for your time and patience for reading.
Last edited on
my C is rusty, what exactly does the spawned process from fork execute here? Is it making it look like it did 2 things because 2 copies of part of the code run in parallel?
> pid = fork();
Well the moment this returned something >= 0, there are TWO copies of the code running in two different processes.

So yeah, both things happen, but in different parts of the universe.

But from their individual perspectives, only one branch is taken.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
ok.  I suspected as much, but as I said, fork() is a distant memory to me. 

so, c++, then:
if(condition) 
{
   //if condition is true, these happen
}
else 
{
  //these happen if condition above is false
}
and it is THAT SIMPLE.  
you can chain if-statements in the expected ways, an if can be 
inside another if, inside an else, or chain the elses 
(else-if, but else-if isnt a single statment in c++, its just a chain 
of else statement that consists entirely of an if-statement block. )

so
if(condition)
{
  ;
}
else if(other condition)
is really this to c++:

if(condition)
{
  ;
}
else
{ 
  if(other condition)
     {
         ;
     }
}

even though many coders write else if on the same line as one statement, it is not, it is two blocks of code written in a way that tells the reader what is going on. Its really just a shortcut to the above explicit blocks.
It is confusing as crap to c++ coders to move to py which uses whitespace to denote blocks of related code. C++ uses {} and implicit {} to denote code blocks. The implicit ones are ALWAYS one single statement (which can, itself, trigger a block).
if(foo)
cout << foo; //implicit block
cout << bar; //this is NOT inside that IF statement block. This statement always happens.

a side effect of using {} blocks in c++ coupled with another feature of the language (the compiler ignores most whitespace) is what allows the else if on a single line to work in c++.

last one:
if(foo)
while(bar)
{
code; //this is inside the while which is inside the if via and implicit {} block.
}

I personally try to put {} even on single statements so there are never implicit {} code blocks. Its a self defense anti-bug tactic. The else if shortcut is the only place I do not.

syntax aside it works pretty much like py. The problem here was threading and splitting the process, not the conditional blocks.
Last edited on
Topic archived. No new replies allowed.