(Although it could be 101 - since there is nothing determining which of the two threads run first, and thus which order the output is, child or parent).Code:011
--
Mats
(Although it could be 101 - since there is nothing determining which of the two threads run first, and thus which order the output is, child or parent).Code:011
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
only thing certain (apart from death and taxes! ) is that the code spawns four distinct processess. The unbuffered output is:
and buffered output is:Code:01010101 /* Unbuffered */
Code:0 1 1
If u ran the program as it is you get:
If the output is redirected to a file, the same result is obtained.Code:01010101
If you put a \n after "%d" you get:
If the output is redirected to a file, this is obtained:Code:0 1 1
Code:0 1 0 1 0 1
The output which goes to a terminal is line buffered, if the output contains a newline then the buffer is flushed. If the line is longer than the buffer, it will be flushed sooner.
The output which goes to a file is fully buffered. This means that it gets flushed only when the buffer gets full even if there are "\n" in the output.
fork() creates a new process by duplicating the calling process; this implies that the data space of the parent process is copied to the data space of the child process. The data space includes the un-flushed buffers.
I have stopped reading Stephen King novels. Now I just read C code instead.
The output depends on when the output is flushed.
If it's after one character, the output is 011
If it's after two characters, the output is 0101
If it's after three characters or termination, the output is 01010101.
So it may really be any of those.
I've found this programming puzzle questions on a site && i thought to share them with you all:
Q1 Write a "Hello World" program in 'C' without using a semicolon.
Q2 Write a C++ program without using any loop (if, for, while etc) to
print numbers from 1 to 100 and 100 to 1;
Q3 C/C++ : Exchange two numbers without using a temporary variable.
Q4 C/C++ : Find if the given number is a power of 2.
Q5 C/C++ : Multiply x by 7 without using multiplication (*) operator.
Q6 C/C++ : Write a function in different ways that will return f(7) =
4 and f(4) = 7
Q7 Remove duplicates in array
Q8 Finding if there is any loop inside linked list.
Q9 Remove duplicates in an no key access database without using an
array
Q10 Write a program whose printed output is an exact copy of the
source. Needless to say, merely echoing the actual source file is not
allowed.
Q11 From a 'pool' of numbers (four '1's, four '2's .... four '6's),
each player selects a number and adds it to the total. Once a number
is used, it must be removed from the pool. The winner is the person
whose number makes the total equal 31 exactly.
Q12 Swap two numbers without using a third variable.
Given an array (group) of numbers write all the possible sub groups of
this group.
Q14 Convert (integer) number in binary without loops.
I have stopped reading Stephen King novels. Now I just read C code instead.
I only bothered to do the quine :P. Quite easy tasks, but I like the self-printing programs. I wrote this one before, so I redid what I memorized:
Code:#define A(x)#x;x a=A(main(){printf("#define A(x)#x;x\na=A(%s)",a);})
Note that C99 doesn't require "return" explicitly:
Code:#include <stdio.h> int main() { if(printf("Hello World!\n")) { } }
Q2 Write a C++ program without using any loop (if, for, while etc) to
print numbers from 1 to 100 and 100 to 1;Code:cout << "1 2 3 4 ... 99 100" << endl; cout << "100 99 ... 4 3 2 1" << endl;
Note that you may not abbreviate the following:Q3 C/C++ : Exchange two numbers without using a temporary variable.
Code:int a, b; a ^= b; b ^= a; a ^= b;
Bit twiddling or simply the following:Q4 C/C++ : Find if the given number is a power of 2.
Code:log2(x) == floor(log2(x))
Q5 C/C++ : Multiply x by 7 without using multiplication (*) operator.Code:x = x+x+x+x+x+x+x; /* or */ x = (x<<3) - x;
Q6 C/C++ : Write a function in different ways that will return f(7) = 4 and f(4) = 7Code:/* the obvious approach or this one: */ int f(int x) { return x ^ 3; }
Where's the puzzle?Code:Q7 Remove duplicates in array
Haire-and-Tortoise approach / Classic Floyd's Cycle Finding Algorithm: Two pointers "slow" and "fast" pointing to the first element. "slow" advances one element per iteration, "fast" advances two. If slow == fast before the end is reached, the list contains a cycle.Q8 Finding if there is any loop inside linked list.
See Q7.Q9 Remove duplicates in an no key access database without using an array
Or better: write a program in C that prints a program in Perl that prints the original C program. Once done, this is easily extended to an arbitrary number of steps.Q10 Write a program whose printed output is an exact copy of the source. Needless to say, merely echoing the actual source file is not allowed.
Too much overhead. Any volunteers?Q11 From a 'pool' of numbers (four '1's, four '2's .... four '6's),
each player selects a number and adds it to the total. Once a number
is used, it must be removed from the pool. The winner is the person
whose number makes the total equal 31 exactly.
Solved in Q3.Q12 Swap two numbers without using a third variable.
Where's the puzzle?Given an array (group) of numbers write all the possible sub groups of this group.
Q14 Convert (integer) number in binary without loops.Code:x;
Greets,
Philip
All things begin as source code.
Source code begins with an empty file.
-- Tao Te Chip
> Or better: write a program in C that prints a program in Perl that prints the original C program. Once done, this is easily extended to an arbitrary number of steps.
Copy that...
About the bit twiddling in the power of 2 question:Code:#define A(x)#x;x a=A(main(){printf("print %c#define A(x)#x;x\na=A(%s)%c;",39,a,39);})
bool power_of_two(unsigned int n) { return n && (n & (n-1)) == 0; }
As to why the output is 01010101 when it's buffered; fork() duplicates the stdout buffer in the child process and their exit is equivalent to calling fflush(stdout) in each of the four distinct processes. That's why the buffer containing "01" is printed out 4 times. When stdout is flushed, the same program prints out 011.