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

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

Are they essential????

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

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++.

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.

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?

Could you explain forks to me?

Can you explain google to me?

666

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

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

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?

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.

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?

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?

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?

When I use the header file #include<unistd.h>
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.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.