fork(), could you explain!!


 
Thread Tools Search this Thread
Top Forums Programming fork(), could you explain!!
# 1  
Old 10-30-2011
fork(), could you explain!!

Dear friends,
We are learning UNIX system programming as part of our course. I came across this simple program, which the teacher didn't explain well enough. could you please explain this program fully

Code:
#include <sys/types.h>
#include <stdio.h>
#include <unistd.h>
int main()
{
int pid;
int status = 0;
if(pid = fork())
{
pid = wait(&status);
printf("%d\n",pid);
}
else
{
exit(status);
}
return 0;
}

And why does this program give an output of 912???
Thanks
# 2  
Old 10-30-2011
When fork() is invoked the system creates a new process which is nearly an exact copy of the process invoking the function. The new process begins execution of the same binary (programme) starting with the statement immediately following the fork().

So, following the fork(), the programme that you posted is being executed in two different processes. The most noticeable difference is that in the parent process, the process that invoked fork(), the return value is the process id (PID) of the newly created process; in the child, the return value is zero.


Given that, the statement
Code:
if( pid = fork() )

will evaluate 'true' in the parent and 'false' in the child. That means, the parent will execute the wait() call to wait on the child to finish execution. In the case of your example, the child will just exit with a zero (good) return code.

The return from the wait() function is the process ID of the process that finished. If the parent forks off more than one, knowing which one finished might be important. In your sample programme, the PID of the child is what is printed to stdout. If you run the process two times in a row it shouldn't print the same thing.

Hope this helps a bit.
# 3  
Old 10-30-2011
Quote:
Originally Posted by gabam
Dear friends,
We are learning UNIX system programming as part of our course. I came across this simple program, which the teacher didn't explain well enough. could you please explain this program fully

Code:
#include <sys/types.h>
#include <stdio.h>
#include <unistd.h>
int main()
{
int pid;
int status = 0;
if(pid = fork())
{
pid = wait(&status);
printf("%d\n",pid);
}
else
{
exit(status);
}
return 0;
}

And why does this program give an output of 912???
Thanks
Your program is calling printf(("%d\n",pid) from only one location (and from the if() block where its testing for the value of pid only after the expression pid = fork() is evaluated); so no mixture output, a simplest case of such fork() examples.

Who is executing that if() block ???. Its the parent (remind that fork() is a system call returns two times after a single call to it and it returns the parent (the callee to fork()) with the process id of the child it just created and a zero to that child. Hence that if() block is being executed by the parent only and its printing the process id it had received and collected in the variable pid of your program.

The output is just the process id of the child process created.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

How I can explain this?

Hi friends! I'm learning UNIX and I have a small question. Working with Shell, i put the name of one executable (in c language) + one number and it says this: $ gcc misterioso_4.c $ ./misterioso_4 6 got: , I can not find an answer in the manual because I havent applied any variable.... (5 Replies)
Discussion started by: dakota
5 Replies

2. Shell Programming and Scripting

Can someone explain this for me?

Can someone do me a favour and explain the following for me: ((r=$RANDOM%$n+1)) I know what $RANDOM does but what is % sign and what does it do with %$n+1? (2 Replies)
Discussion started by: bashily
2 Replies

3. Shell Programming and Scripting

anyone can explain this?

why the case 2 will happen ? , ' should stop the history substitution ,shouldn't it? case 1 # echo "123"|sed '/123/!d' 123 case 2 # echo "123 > 456 > 1 > "|sed '/123/!d' -bash: !d': event not found case 3 # echo "123 > 456 > 12 > "|sed '/123/'\!d 123 # bash --version (1 Reply)
Discussion started by: justlooks
1 Replies

4. Shell Programming and Scripting

Explain $# please

I'm trying to follow a script and I see it begins with this: if ; then if ; then print "blah $0 blah blah " exit fi fi What does $# mean? I found out that $1 refers to the shell environment and the last argument that was entered or passed in the previous command. I couldn't find $#... (2 Replies)
Discussion started by: MaindotC
2 Replies

5. Shell Programming and Scripting

can any one explain this example

hi all i have an example i want one help me to understand cause i tried to test it but almost fail and i don't know how can i solve this problem " the main idea to read from two files and replace something from one to another " but i don't understand why it fail all time $ cat main.txt... (4 Replies)
Discussion started by: maxim42
4 Replies

6. UNIX for Dummies Questions & Answers

Please explain this

if then echo "Syntax: $0 <sid> <COLD/HOT> <DEST>" exit fi if --------------what does this mean??? echo "Syntax: $0 <sid> <COLD/HOT> <DEST>"---pls explain this as well (2 Replies)
Discussion started by: appsdba.nitin
2 Replies

7. Shell Programming and Scripting

please explain the below

could u please convert the below statement to shell script ---------- logdir=/smp/dyn/logfiles/cpm/pgm/pgIm $logdir = $logdir ."/pgIm${toDate}*"; ---- could u please explain the below clearly grep -i adding $logdir | grep -iv equation | awk '{print \$NF}' | sort -u | sed -e... (1 Reply)
Discussion started by: mail2sant
1 Replies

8. Shell Programming and Scripting

Please can any one explain this ${0##/}

I did not understand what is ${0##/} PGM=${0##/} TMP=/tmp/${PGM}.$$ Please explain me. (2 Replies)
Discussion started by: gadege
2 Replies

9. UNIX for Dummies Questions & Answers

oh fork() and vfork() someone explain the difference?

Can somebody explain to me the differences between fork() and vfork() system calls using C programs which I can implement in the UNIX environement? (1 Reply)
Discussion started by: lvkchaitanya
1 Replies

10. Shell Programming and Scripting

can anyone explain this?

:start /@~/{ h s/\(.*\)@~.*$/\1/ s/@~$// s/@~/\ /g p g s/.*@~\(.*\)/\1/ } //{ N s/\n/ / b start } (2 Replies)
Discussion started by: djkane
2 Replies
Login or Register to Ask a Question