How to use fork()!
Hi, I'm rather new in C programming.
I'm now studying about Operating Systems and have a 3 weeks mini project about "PROCESS".
I don't know how to start it!!
This is a detail :
"Write a C program on Linux(and Windows) to fork() a child process. And have both perform some programming as you like,
but that must demonstrate the benefits of having many processes."
I really really don't know how to program.
Could anyone please explains & suggests or shows some examples about fork() and the detail above.
Thank you very much
PS. Sorry for my bad ENG. ^^
Were any examples listed in the class?
Were you listening (or there)?
Is it just fork(), or are you supposed to use pipe() as well?
Because fork() by itself is just the basics for writing a very simple command line shell.
Fork() allows you to have two isolated actions occurring at (more or less) the same time. Fork is an extremely simple method of doing this, which indicates to me that you haven't put any work into this at all. The members of the board are almost always happy to help debug certain programs and point you in the right direction - but don't expect anyone to actually write the whole program for you.
Do a google search for examples of fork() - they're abundant.
Please be sure to read the fork man page by typing man fork at your command prompt.
Fork makes a copy of your process. When fork returns, there are two identical processes running, and fork returns in each process. In the parent process, fork returns the process ID of the child. In the child process, fork returns 0.
If fork fails, then it returns -1 in the parent process, and there is no child process.
Here is an example:
Each process has its own copy of its data. Its as if you started two identical prgrams from the command line. Certain things are not inherited by the child. Check the man page.
pid_t child = fork();
if (child < 0)
/* fork failed, handle it */
if (child > 0)
/* this is the parent process */
if (child == 0)
/* this is the child process */
That's the easy part.
Now you have to demonstrate usefulness, which can be difficult if you have no programming experience because for a parent and child to work together you need some kind of interprocess communication. Maybe you could fork a couple of child processes, and have each try to read messages from a message queue. The parent would send work requests to some message queue, and the child processes could process requests in parallel. You'd need to know how to use message queues. The man pages for those are man msgget, msgsnd, and msgrcv. Be proactive, read up on unix system programming.
Originally Posted by Salem
Thanks very much ^^
but I'm sorry to say that, there is no lesson about C programming or its example in OS class.
as I said, I'm rather new with C, just a simple loop, input/output, array & some simple functions.
moreover, this is my first time to program in linux, so, everything seem to be new and challenge for me.
by the way, thank you for your suggestion sir ^^
Ps. just have a simple C programming book for windows in my language. T_T
& Sorry for my English again sir.
Thank you for all suggestion :D
BTW, Windows doesn't have a fork() function, so when you're doing it on Windows you'll have to use something else like CreateProcess().
Did you take the required programming language prerequisites for this course?
That's odd that they would give an assignment like that and expect students to know C.
Yes, I did. But Computer Programming I for Windows just teaches us only the basical level
Originally Posted by itCbitC
(Data types, Variables,Expression,input/output,Control Statement, Subprogram, Array, and Pointer).
Another programming courses are VB and JAVA (Basic - Advance).
In fact, there is no prerequisites programming language required for OS course!
I don't know why the lecturer assumes that all students in the class know C for UNIX in
Advance level, so, he gives this assignment to us.
I can say, "I'm now walking with no direction" :confused:
BTW, thank you for all suggestion!
This is certainly out of the realm of beginner programming. It would usually be a third or 4th year computer science/engineering course, where the prof can of course assume everyone has quite a bit of programming experience, even if no pre-requisite has been listed explicitly.