![]() |
|
#1
|
||||
|
||||
How a child process past a value to parent process?The following program need LiINUX Fedora Ajunta
to compile and RUN: I got this question: Write a program call Sum.c which will do the summation of 1 to n where n is an integer larger than 1, given by the user. (a) Invoke Sum.c from the child process. (b) Discuss the method which allows the communication between two processes. (c) Use the method identified in (b) so that the Sum process will send the result of the summation to the parent process. The parent process then output the result. I had done a) and b), but i need guidance for the part c) Here's my code: Main program: CPP / C++ / C Code:
Sum.c: CPP / C++ / C Code:
So how to return the value from sum.c into main program? This is a C language right? I only start learning from C++, so any C code i wrote it wrong please correct me. Thanks a lot. Regards, Reny |
|
#2
|
|||
|
|||
Re: How a child process past a value to parent process?Quote:
Since your code includes the header <shm.h>, my guess would be that you are supposed to used shared memory. There are actually several ways to effect inter-process communications. One place to start learning is: http://beej.us.guide. Yes, that's the same "beej" who maintains the excellent "Beej's Guide to Nework Programming". Just click on the ipc link. Among the topics covered there is a shared memory link You might look for other explanations and sample code in places like http://linuxgazette.net/104/ramankutty.html Quote:
Before getting into shared memory, I respectfully suggest that you make sure you understand exactly how the execv and forking stuff works. Here are my examples for the "sum" and the "main" programs (no shared memory, yet): CPP / C++ / C Code:
Now, here is the main program: It forks a child. The child runs the target. I made it so that for test purposes you can give it the name of a target that doesn't exist as well as trying other target programs at several locations on your system. The default target is /tmp/sum. Note use of standard library definitions for the return values. This code will be as portable as you can make it. (Note that even though the return data type is int, operating systems like Linux actually make a 16-bit value available to the calling program, and it is big-endian (!), so you must, in general, resist the temptation to use the return value of the target as a way of having the forking program get a value from the target. Now, here is the main program: CPP / C++ / C Code:
I have deleted the ipc and shm headers for this program, and I have shown the only headers needed for my Linux installation. You should compile both programs on your system with gcc -Wall -W -pedantic to make sure you get absolutely no compiler messages of any kind before proceeding. Your exact header names and locations may differ. Now, after putting the "sum" executable in my /tmp directory, here are a few runs of the main forking program (I called it "z"): Code:
Then, after reading about shared memory, you will be ready to rock and roll! Regards, Dave |
|
#3
|
||||
|
||||
Re: How a child process past a value to parent process?thx dave, i owe you a lot, now i need a lot of time to swallow it.
|
|
#4
|
||||
|
||||
Re: How a child process past a value to parent process?Dave, i'll like to give some of my feedback to you:
1) ya, i did hand up of assignment, but i got a very bad viva, and a bad result. 2) i check bec my problems, and i found that i'm totally dun understand the C language and of course the share memory segment. 3) yes, i'm not talented in this field, as always. 4) Finally i had chosen not to skip this problem, and hopefully you're kind enough to reguide me again. The following i'll post you the full version of my assignment |
|
#5
|
||||
|
||||
Re: How a child process past a value to parent process?2. (a) Program Assign2.c is a skeleton of a mini shell. A mini shell is a program which waits for the command from the user and execute the command.
(i) Compile and run Assign2.c and Test.c. Insert a system call in the if statement which will create a child process. Use a system call which will display the process id of the child, and also the parent. Include in your report. (ii) Once the child process is successfully created, it should then execute the executable of Test.c. Insert the appropriate system call to enable this. (iii) Execute the ls command from your mini shell. Copy and paste the output into your report. Explain what happen. 3. Write a program call Sum.c which will do the summation of 1 to n where n is an integer larger than 1, given by the user. (a) Invoke Sum.c from the child process. (b) Discuss the method which allows the communication between two processes. (c) Use the method identified in (b) so that the Sum process will send the result of the summation to the parent process. The parent process then output the result. These are the example code my lecture gave us: Assign2.c CPP / C++ / C Code:
Test.c CPP / C++ / C Code:
|
|
#6
|
||||
|
||||
Re: How a child process past a value to parent process?And Also, I had read through the guidance you gave me for the shared memory segment, ya...those 0644, "arg" at the execv...not understand it's function.
So, i'll try to read again and again, hopefully wont be feeded so much. |
|
#7
|
|||
|
|||
Re: How a child process past a value to parent process?Quote:
The "arg" of the execv function is like the argv in main: CPP / C++ / C Code:
Or, its exact equivalent: CPP / C++ / C Code:
So argv is an array of pointers to char. Each item pointed to is a C-style "string": a null-terminated sequence of chars in memory. The final pointer value is NULL (So argv is a NULL-terminated sequence of pointers.) If you want to call your child process with arguments, then you put the arguments into an array and use the array name as the second argument to execv. According to the C standard, you don't have to supply any arguments (and the child process may or may not use them even if you do supply them) So in general for a child process named "test", in the current directory, If you want to pass an argument: CPP / C++ / C Code:
If you don't want to pass any arguments to the child, you could do someting like: CPP / C++ / C Code:
Or even: CPP / C++ / C Code:
Regards, Dave Last edited by davekw7x : 05-Apr-2007 at 12:40.
|
|
#8
|
||||
|
||||
Re: How a child process past a value to parent process?Yes, i got it, what's the use of command line?
and y we must pass 2 char pointers into execv? What would happen if i pass *target into execv only? thx for your patience. |
|
#9
|
||||
|
||||
Re: How a child process past a value to parent process?oh ,is the command line exactly the word we going to type in for a terminal to execute our command?
|
|
#10
|
|||
|
|||
Re: How a child process past a value to parent process?Quote:
Yes. Regards, Dave |
Recent GIDBlog
Welcome to Baghdad by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| problem with spawnl() and system() | balajiramani | C Programming Language | 3 | 25-Jan-2007 07:49 |
| Program on creating a process!! | sac_garg | C Programming Language | 3 | 01-Oct-2006 09:22 |
| [TUTORIAL] Calling an external program in C (Linux) | dsmith | C Programming Language | 4 | 22-Apr-2005 13:30 |
| apache php no longer working (MX??) XP?? | ChicoMendez | Apache Web Server Forum | 5 | 30-Aug-2004 10:51 |
| Assign label to child process? | 7eleven | C++ Forum | 0 | 31-May-2004 21:43 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The