954,136 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

What are forks????

I need to what are forks and where we use them in C++.
Are they essential????

krnekhelesh
Junior Poster
127 posts since Jul 2007
Reputation Points: 31
Solved Threads: 3
 

I hope that anyone who does not know about this would benefit by this discussion.

krnekhelesh
Junior Poster
127 posts since Jul 2007
Reputation Points: 31
Solved Threads: 3
 

http://www.daniweb.com/forums/thread82074.html

posted just a few below this.

as well as some good examples linked i believe.

Killer_Typo
Master Poster
781 posts since Apr 2004
Reputation Points: 152
Solved Threads: 39
 
Are they essential????

Depends on what you are coding (Generally, I'd say no). Also, check the other forum.

sillyboy
Practically a Master Poster
686 posts since Mar 2007
Reputation Points: 85
Solved Threads: 64
 

I saw that thread. But the explanation was not satisfactory. They explained it from the point of C language.

But this forum is for C++.

krnekhelesh
Junior Poster
127 posts since Jul 2007
Reputation Points: 31
Solved Threads: 3
 

Your questions are related to the fundamentals of forks. What are forks? Where are they used? Are they essential? These questions have been answered (yes from a C perspective), but that does not change the answers.

sillyboy
Practically a Master Poster
686 posts since Mar 2007
Reputation Points: 85
Solved Threads: 64
 

So a fork splits a process into 2 processes. But why? Suppose you write a simple C++ program which prints Hello World on the screen, what is the process there?

krnekhelesh
Junior Poster
127 posts since Jul 2007
Reputation Points: 31
Solved Threads: 3
 

Could you explain forks to me?

krnekhelesh
Junior Poster
127 posts since Jul 2007
Reputation Points: 31
Solved Threads: 3
 
jwenting
duckman
Team Colleague
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
 

Can you explain google to me?

Toba
Junior Poster
192 posts since Jun 2004
Reputation Points: 115
Solved Threads: 5
 

666

jwenting
duckman
Team Colleague
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
 
So a fork splits a process into 2 processes. But why?



networking. e.g you cant send and recieve at the same time so you could recieve in one thread and send on the other

jbennet
Moderator
Moderator
18,523 posts since Apr 2005
Reputation Points: 1,820
Solved Threads: 600
 

Forks don't create new threads; they create new processes.

Rashakil Fol
Super Senior Demiposter
Team Colleague
2,658 posts since Jun 2005
Reputation Points: 1,135
Solved Threads: 176
 

So as a beginner in my C++ programming can I use them to split two process one for writing data into a file and another to read another file?

krnekhelesh
Junior Poster
127 posts since Jul 2007
Reputation Points: 31
Solved Threads: 3
 

No, beginners aren't allowed to use forks. They are a level 2 restricted system call (L2SYS). Talk to your manager about getting certified.

On a more technical note, yes I suppose you could do that, but there is no unfork() as far as I know, so if you want to do something like that and the two need to interact in a meaningful way, you either need to learn how to use shared memory, or how to use threads.

Toba
Junior Poster
192 posts since Jun 2004
Reputation Points: 115
Solved Threads: 5
 

I read that forks can used to call another executable. Suppose I have two programs prog1 and prog2.

Suppose prog1 has the fork code and calls prog2, once prog2 is over will the control return to prog1?

krnekhelesh
Junior Poster
127 posts since Jul 2007
Reputation Points: 31
Solved Threads: 3
 

Forks are very unnecessary for most things not concerned with networking. Your post above is quite redundant, since I would assume you would fork if you wanted to try run prog1 & prog1 concurrently (obviously this isn't entirely true, but will appear that way). If you are willing to wait for prog2 to finish before proceeding with prog1, why use a fork?

sillyboy
Practically a Master Poster
686 posts since Mar 2007
Reputation Points: 85
Solved Threads: 64
 

when i needed to use forks, what i needed was a process to wait for an user input (ENTER in this case) so a parallel process could stop... can that be done?

Nichito
Posting Virtuoso
1,602 posts since Mar 2007
Reputation Points: 424
Solved Threads: 57
 

When I use the header file #include
It says the compiler cannot read the file.

I tried compiling it in DEV C++ it gave me a error message that fork() was not initialized.

krnekhelesh
Junior Poster
127 posts since Jul 2007
Reputation Points: 31
Solved Threads: 3
 

That's because there's no such thing as fork() or unistd.h on your average windows box.

Win32 doesn't have anything like fork() as such, see the hoops cygwin has to go through in order to emulate it (poorly).

If you're doing the typical fork() followed immediately by exec(), then using createProcess is a reasonable substitute.

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You