![]() |
|
#1
|
|||
|
|||
Program on creating a process!!Hi guys. can u please help me this program. It is as follows:
WAP fork.c to do the following: 1) First print your name and student id; 2) Accept and parse user's input, which may consist of multiple (for simplicity at most 2) shell commands separated by '&' (ampersand) character (for example ls -a & cat fork.c). 3) Create a child process using system call fork; 4) Execute the first command in the child process using system call exec; and 5) Execute the second command, if exists, in the parent process using system call exec. Student ID is like any other ID. User will input it. The fork() function is used to create a new process from an existing process. The new process is called the child process, and the existing process is called the parent. You can tell which is which by checking the return value from fork(). The parent gets the child's pid returned to him, but the child gets 0 returned to him. |
|
#2
|
|||
|
|||
Re: Program on creating a process!!Quote:
What part have you done already? With what part would you like some help? What kind of help? In other words: I respectfully suggest that you Show Us Some Code, And Then Ask Your Questions. Regards, Dave |
|
#3
|
|||
|
|||
Re: Program on creating a process!!i have created the child and parent process. now i'm getting confused in inputting the second command if exists in the parent process using system call exec. also how to accept multiple commands separated by '&'
my work is as follows: CPP / C++ / C Code:
Last edited by admin II : 01-Oct-2006 at 05:01.
Reason: Please surround your C code with [cpp] ... [/cpp]
|
|
#4
|
|||
|
|||
Re: Program on creating a process!!Quote:
Point number 1: The following lines were deleted from the Original Program: CPP / C++ / C Code:
Since the closing brace '}' was deleted in the above lines, your code won't compile. Point number 2: The comment lines from the Original Code are helpful, and, more importantly, the Original Code (from http://www.cs.cf.ac.uk/Dave/C/section2_22_22.html) accompanied a pretty good explanation of how forks and child processes, etc. work. The code itself was copyrighted in 1994 by Dave Marshall. The particular example shows how to create a child process and to make the parent to wait until the child process was complete before returning to the command prompt. You can see lots of good stuff here: http://www.cs.cf.ac.uk/Dave/C/CE.html Did I say it was a "pretty good" explanation? Change that to "really good". Note that the code uses old-style function headers, and in my opinion it should be cleaned up and modernized before you work on it. (Including elimination of gets() in favor of fgets()). Since parse() and execute() don't return anything, I think it's appropriate to declare them with "void" instead of using legacy C's int default. The example works exactly as it should, but part of the learning process is to build on experience and bring something new to the party. In particular, the other stuff that was commented out is the action that will occur if the current process is the parent. So, if I were doing the assignment, the execute() function might look something like the following. See footnote. CPP / C++ / C Code:
My style is a little different from the original author, and you can use whatever style appeals to you (maybe something different from either). So your main program prompts "Command " and gets user input. The parse() function makes the args[i] values point to the separate words on the user comand line. Here's a way to print out what the user entered: CPP / C++ / C Code:
Now, the execute() routine can go through the list and use the ones it needs for the child and parent process. With the actual execvp() commented out as I showed, here's what a run could look like: Code:
How you process the argument list into shell commands is up to you (and is, obviously part of the assignment). I just wanted to show how the example could be used to build the application that you need. Regards, Dave Footnote: refer to previous thread here for some more discussion of forking and a little about execvp http://www.gidforums.com/t-11552.html |
Recent GIDBlog
Developing GUIs with wxPython (Part 4) by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Pipeline freeze simulation | darklightred | C++ Forum | 6 | 27-Jul-2006 20:37 |
| How to read particular memory location ? | realnapster | C Programming Language | 10 | 10-May-2006 10:11 |
| Creating a "varargs" addition program | crystalattice | Python Forum | 2 | 08-Oct-2005 23:06 |
| [TUTORIAL] Calling an external program in C (Linux) | dsmith | C Programming Language | 4 | 22-Apr-2005 14:30 |
| fltk-2.0 cvs | Plumb | FLTK Forum | 20 | 13-Nov-2004 08:10 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The