Problem with execution of fork system call if i use \n


 
Thread Tools Search this Thread
Top Forums Programming Problem with execution of fork system call if i use \n
# 1  
Old 12-24-2010
Problem with execution of fork system call if i use \n

hi all,

i tried the following source codes:
fork1.c:
Code:
main()
{
  printf("demo of fork\n");
  fork();
  printf("hello");
}

output:
Code:
demo of fork
hello hello

fork2.c:
Code:
main()
{
  printf("demo of fork");
  fork();
  printf("hello");
}

output(fork2.c):
Code:
demo of fork
hello hello demo of fork

In both the programs chilld and parent process are supposed to execute the statement(here its printf("hello")) which is next to fork call.But why the printf("demo of fork") is getting executed twice.Pls clarify my doubt.

Thanks in advance.

Last edited by Scott; 12-24-2010 at 09:23 AM.. Reason: Please use code tags
# 2  
Old 12-24-2010
It is because printf is "buffered," meaning printf will group the output of a process together. While buffering the output for the parent process, the child may also use printf to print out some information, which will also be buffered. As a result, since the output will not be send to screen immediately, you may not get the right order of the expected result. Worse, the output from the two processes may be mixed in strange ways.
To overcome this problem, we should use newline at the end of each printf as newline causes buffer flush.
OR we may consider to use the "unbuffered" write.
Prototype: int write(int fd, char *Buff, int NumBytes);
This User Gave Thanks to anurag.singh For This Post:
# 3  
Old 12-24-2010
What happens is this:

Code:
// Without \n, this goes into a buffer and isn't printed yet.
printf("demo of fork");
// This creates a precise duplicate of the process -- including buffers.
// "demo of fork" will now be printed twice!
fork();
// this adds more stuff to the buffers of both programs.
printf("hello");
// The buffers will be flushed when the programs exit.  They may not exit
// in any particular order, and may try and print at the same time,
// causing the garble you see.

Code:
printf("demo of fork");
// Force it to print the buffer NOW, without waiting, so the child process
// doesn't get a copy of it in its own buffer.
fflush(stdout);
fork();
printf("hello");

This User Gave Thanks to Corona688 For This Post:
# 4  
Old 12-26-2010
Thanks a lot anurag.

---------- Post updated at 07:02 AM ---------- Previous update was at 07:01 AM ----------

Thanks a lot corona.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Doubt with fork() system call

Hi I wrote a simple fork program to illustrate the fork() system cal. here it is #include<stdio.h> #include<sys/stat.h> #include<sys/types.h> main() { int flag; flag=fork(); if(flag==0) { printf("Child \n"); printf("Process id= %d\n",getpid()); ... (3 Replies)
Discussion started by: badsha6642
3 Replies

2. Shell Programming and Scripting

fork system call and \n

hi, i tried the following source codes: fork1.c: main() { printf("demo of fork\n"); fork(); printf("hello"); } output: demo of fork hello hello fork2.c: main() { printf("demo of fork"); (0 Replies)
Discussion started by: pnirmala
0 Replies

3. Homework & Coursework Questions

fork system call understanding

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: i have a problem in understanding the behaviour of fork . i understood fork as to create a new process and... (4 Replies)
Discussion started by: MrUser
4 Replies

4. Linux

system call problem

hi, where can I find the detail information about the syscall in binary instructions of linux/mips. for example, in linux/mips: li v0, 4140 syscall it's a syacall of "lseek" , but how can I find that which registers will be used in this syscall , and the meaning of the arguments in the... (2 Replies)
Discussion started by: zerocool_08
2 Replies

5. UNIX for Dummies Questions & Answers

fork system call

Hi folks, I want to know how this below program works? #include <stdio.h> int main() { printf("A\n"); fork(); printf("B\n"); fork(); fork(); printf("D\n"); fork(); printf("C\n"); } This is just for example. How this type of programs where fork is used many places, how the... (1 Reply)
Discussion started by: u_peerless
1 Replies

6. AIX

problem in msgctl() system call

Hi, i am using IBM P6 server and OS is AIX 5.3. my code is written in c/pro C. i am facing problem in msgctl() system call.The variables msg_qbytes and msg_cbytes are used to store total no of bytes and current no of bytes in a QUEUE,but it is showing me as ZERO though data are there... (0 Replies)
Discussion started by: ajaysahoo
0 Replies

7. UNIX for Dummies Questions & Answers

fork() system call

Can anyone explain me what really happens when a system call fork() is called ? I like to know what happens internally. Thanks in Advance. - Arun (1 Reply)
Discussion started by: arunviswanath
1 Replies

8. Programming

Problem in system call

Dear Friends, I write a c program to list the directories recursively. For this I write a function called my_readdir to read the content of directory. For this I use read system call it returns -1, then I use readdir system call it gives comment terminated error or segmentation... (1 Reply)
Discussion started by: spmlingam
1 Replies

9. Programming

Fork() system call time?

One more question. How can i calculate the time that system needs to make fork() system call? I need to make it with times function but i really don't know how. :( (2 Replies)
Discussion started by: davidoff
2 Replies

10. UNIX for Advanced & Expert Users

URGENT Help required regarding the use of FORK system call

I desperately wanted one of the UNIX Gurus to help me resolve my problem asap(I have to deliver the code to the client by Monday 08-oct). I have a file with around 5 million records (50 lakhs). Now my original process was taking around 30 hours to read the complete file, process each and every... (4 Replies)
Discussion started by: kkumar1975
4 Replies
Login or Register to Ask a Question