behavior of system(<cmnd>) function

V

Vipin

I tried following two programs and I am seeing an unusual behavior
here.
what can be the reason for the same ??


/* ------ main.c ----- */
#include <stdio.h>
#include <stdlib.h>

int main (int argc , char *argv[]){
printf("\nI am in main.");
#ifdef unix
system("./main2");
#else
system(".\\main2.exe");
#endif
printf("\n Tried to invoke executable");
return 0;
}


/* -------main2.c----------*/
#include <stdio.h>
#include <stdlib.h>

int main (int argc, char *argv[]){

printf("\n I am running in main2\n ");
return 0;
}


--> both the progs are in the same diretory
--> I do the following

$gcc -o main2 main2.c
$gcc -o main main.c
$./main

is see the following result

RESULT
--------------------
I am running in main2
I am in main.
Tried to invoke executable


I have the concern that why the second line gets printed in after the
first. If you see in main.c, it was printed before invoking "main2".
shouldn't the behavior of program be exactly sequential.
 
V

Vipin

I tried following two programs and I am seeing an unusual behavior
here.
what can be the reason for the same ??
I have the concern that why the second line gets printed in after the
first. If you see in main.c, it was printed before invoking "main2".
shouldn't the behavior of program be exactly sequential.


I guess, I found the solution.
call to fflush(stdout);
after
printf("\nI am in main.");
in main.c solves the problem.


I got the solution, but not the reason. Both the process share the
same buffer is fine. But shouldn't the buffer be implemented like
FIFO. why the message that gets in first gets out later.

Am i missing something here ??

-vipin
 
J

Jens Thoms Toerring

I guess, I found the solution.
call to fflush(stdout);
after
printf("\nI am in main.");
in main.c solves the problem.
I got the solution, but not the reason. Both the process share the
same buffer is fine.

That's wrong, both programs have their own, independent buffers.
And when writing to a terminal then the rules for flushing out
these buffers to the terminal are

a) when a '\n' is found in the input everything in the buffer up to
and including the '\n' is send to the terminal,
b) when fflush(stdout) is called,
c) when the program ends.

Since the first string that program 1 outputs does not end in a
'\n' it's kept in the buffer of program 1. Then program 2 comes
along, outputs something and this is the first (non-white-space)
stuff you see on the console.

The simplest "fix" would be to put a '\n' everywhere you want
to see output immediately. In the (rather rare) case that you
actually want some output but without a linefeed then you have
to call fflush() to make sure things get output without delay.

Regards, Jens
 
K

Keith Thompson

Vipin said:
I tried following two programs and I am seeing an unusual behavior
here.
what can be the reason for the same ??


/* ------ main.c ----- */
#include <stdio.h>
#include <stdlib.h>

int main (int argc , char *argv[]){
printf("\nI am in main.");
#ifdef unix
system("./main2");
#else
system(".\\main2.exe");
#endif
[snip]

This isn't what you asked about, but you shouldn't depend on "#ifdef
unix" to tell you whether you're on a Unix system. The identifier
"unix" is not reserved to the implementation, and a conforming
implementation isn't allowed to predefine it as a macro.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,954
Messages
2,570,116
Members
46,704
Latest member
BernadineF

Latest Threads

Top