Results 1 to 10 of 12
hello every one, i am reading and i found this task on the internet. well i reading so i can gain some knowledge of it. but if any one can ...
- 08-26-2007 #1
Process Creation Using fork()
hello every one, i am reading and i found this task on the internet. well i reading so i can gain some knowledge of it. but if any one can help me with this so i can understand what they are talking about. well that will help me a lot in guidance of what i am reading .. thanks once more .. here is the task..
REQUIRED READING UNIX manual pages for the following:
fork, system, execl, getpid, getppid, wait, waitpid,
sleep, ps, perror and exit.
Part I
Write a C program that performs the following actions. Be sure to describe all
significant events during the execution of the program, including any failures:
1. Print out the PID of the current and parent process.
2. Fork a child process that performs the following:
1. Announce its creation.
2. Print out its PID and the PID of its parent.
3. Sleep for 5 seconds.
4. Executes the outside executable described in Part II, giving the outside program at least three arguments.
If the exec fails, exit immediately returning -2. You should use the perror() function to print the error.
NOTE: Be sure to include all standard safeguards. ( i.e. an if() statement around the child's code )
If the fork() fails, the program should exit immediately returning -1.
3. Announce the creation of a child and report the child's PID.
4. Show the system's process chart, using "ps -afl" and the system() call.
5. Wait for the child to die and announce that death. Verify that the child died normally and print out the return code that the child process exited with ( see waitpid() man page for information ).
6. Announce that the main program will die and return 0.
PART II
Note: This program is the one that will be executed by the program described in Part I.
Write a C program that performs the following actions, printing all significant events to standard output:
1. Announce the start of it's execution.
2. Announce its PID and the Parent's PID.
3. Announce the name of the executable run (i.e. its name ). The name CANNOT be hard-coded into the child program!
4. Output each argument to standard output, labeling appropriately.
5. Exit, returning 0.
ADDITIONAL NOTES
(1) Please Use 'myscript > script' command to capture all necessary information for handin as described in the previous assignment.
(2) Print your hardcopy by entering cat myhandin | lpr -P yourPrinterPreference
(3) Also store your source files, script file (myhandin#), and the executable file, in the directory: ~CM28/asg#/ where # = Assignment number
Here is a Sample Output from your program:
Script started on Fri Sep 5 18:49:09 2003
Directory: /home/up/user/CM28/asg1>
./prog2a
./prog2a: I'm a process.
./prog2a: My PID is 3888 and my parent's PID is 3881
./prog2a: Forking a child off.
Child: I have been created!
Child: My PID is 3889 and my parents's PID is 3888
./prog2a: I created a child and its PID is 3889
./prog2a: Printing System Process chart using "ps -al"
F S UID PID PPID C PRI NI ADDR SZ WCHAN TTY TIME CMD
0 S 1000 3879 3099 0 78 0 - 331 schedu tty2 00:00:00 script
1 S 1000 3880 3879 0 77 0 - 364 schedu tty2 00:00:00 script
##
Next line is the parent process
##
0 R 1000 3888 3881 0 79 0 - 325 - pts/0 00:00:00 prog2a
##
Next line is the child process
##
1 S 1000 3889 3888 0 79 0 - 325 clock_ pts/0 00:00:00 prog2a
0 R 1000 3890 3888 0 82 0 - 626 - pts/0 00:00:00 ps
./prog2a: Waiting for my child to die
Child: Executing "prog2b" with arguments.
prog2b: Starting to execute.
prog2b: My PID is 3889 and my Parent's PID is 3888
Argument 1 is: prog2b
Argument 2 is: Arg1
Argument 3 is: Arg2
prog2b: I'm ending execution.
./prog2a: Child exited with status code - 0
./prog2a: I is now gonna die
/CM28/asg1> exit
Script done on Fri Sep 5 18:49:22 2003
- 08-27-2007 #2
I assume that this is not a school assignment for you, but rather that you found some assignment online? Our forum rules don't allow us to help with homework assignments.
Not knowing exactly what you're confused about, I'll discuss the fork() function.
What fork() does is that it essentially creates a "child" process that is an exact duplicate of the current process up to that point. This means it has the same environment as the parent, the same open files, etc. The only difference is the process ID (PID). If you know anything about threading, this is how they differ: a thread is run within the context of the same process, sharing its memory. A child process has its own copies of the parent's memory.
When you invoke the fork() function, it returns a different value based on whether it it invoked in the parent process or child process. It returns the child's PID in the parent process, and 0 in the child process. Because of this, you often see code like this:
Does this make sense?Code:... pid_t fork_pid; if((fork_pid = fork()) == 0) { /* code here for the child process */ exit(0); } wait(fork_pid, NULL, 0); /* wait for the child to finish */ /* at this point, the child has terminated */
The functions that you mentioned all have man pages. Just type "man fork", "man getpid", etc. in the terminal to get full documentation on each function.DISTRO=Arch
Registered Linux User #388732
- 08-27-2007 #3
thanks for explaning
as i have told before i have been reading online. i found this page Tuturial
also a man dictionary that have helped me. understand some more..
man ..
hey this previews post is of lot of help..
well as far as i have understand this task is that i have to create 2 C programs and one bash scrip to make it work true. so i if so the case i need to read about bash scrip cause i no have never tried bas scrip. i have done introduction to c programming..
this what i understand . (i break it in to 3 parts)
1(scrip- *dump out time
*Invock parent
*dump put time) scrip saved as .txt
2(C-Programming- *Invock child--- < - -parent
*do stuff
* Do some stuff
*return)
3(C-Programming- *child
* Do some stuff
*return)
- 08-27-2007 #4
Is this an assignment for you? Or did you just find it online? If it's an assignment, I can't give you any answers. If it's not an assignment, you don't need a script (the script is a part of the handin procedures).
What are you having trouble with? What don't you understand? You are correct that you need to write 2 C programs. I just did this, and it's not a particularly hard task once you get past the concepts. So again: what do you need help with?DISTRO=Arch
Registered Linux User #388732
- 08-27-2007 #5
yeah
i think is something like this
hmm i really need to read more, cause its kinda challenging. but yes its lots of fun.PHP Code:#include "sys/types.h"
#include "sys/wait.h"
#include "unistd.h"
#include "stdio.h"
#include "stdlib.h"
#include "linux/gpio_syscalls.h"
int main(void) {
int rtc;
int sec;
rtc=fork();
if (rtc==0) {
printf("Child: Fork\n");
printf("Child: DL1 on\n");
gpioclearbits(PORTA, PA3);
sleep(7);
printf("Child: DL1 off\n");
gpiosetbits(PORTA, PA3);
return EXIT_SUCCESS;
}
printf("Parent: Fork\n");
for (sec=10;sec>0;sec--) {
printf("Parent: %02d\n",sec);
sleep(1);
}
wait(NULL);
return EXIT_SUCCESS;
}
- 08-27-2007 #6
aha
so that means that i no need any scrip, no its not an assignment. hey can u help me i really want to understand it can u plz suggest any good book where i can read from.
thanks
- 08-27-2007 #7
Well, your code does fork correctly, but it does not do what the assignment wants. Do you understand the assignment? Notably, you fail to examine the PIDs of all of the processes, don't use the system() function, don't use the execl() function, don't use the waitpid() function, etc.
As far as a good book, I taught myself the C language by using "The C Programming Language" by Kernighan and Ritchie. K&R are actually the designers of the C language, and this is considered to be one of the best books on learning the language.
Everything else I've just kind of picked up over my years of programming and using Linux. This is a great way to learn things: by just finding assignments online and completing them and seeing what happens. I'm glad to help if you can tell me what you need help with. I still don't understand what you don't understand.DISTRO=Arch
Registered Linux User #388732
- 08-27-2007 #8
thanks
thanks i will try to get the book, yes i agree with you that practicing and lots of interest,and doing tasks that are available online will help, well i am new to Linux, and i have a little knowledge of C programming as i have said that i have done a course of introduction to c programming.
well i don't understand how to get the PID of the parent and the child by using c programming and how to do the 2 programs in c to work in hand. well and you have seen on the c that i provided i really don't have much knowledge of the functions so if u can help me i will really depreciate it . thanks allot for your time.
can you provide me a web page link that will help me.
- 08-27-2007 #9
Alrighty.
In C, we have the Standard C Library. This is a library of C functions available on every system you write C on. We also have the GNU C Library (glibc), which is a library of functions available on GNU systems (Linux is one such system). A fabulous online resource for these libraries is:
The GNU C Library
(this is the manual for glibc, and also documents the Standard C Library functions)
You can also run the command "man <function>" on the commandline, and you will likely receive the documentation for that function. For instance, you might try "man printf", "man fork", etc.
Now then, the beginning of the assignment states:
I suggest reading all of these man pages. When you do so, you will learn that getpid() returns the current process's PID, getppid() returns the parent process's PID, and so on.Code:REQUIRED READING UNIX manual pages for the following: fork, system, execl, getpid, getppid, wait, waitpid, sleep, ps, perror and exit.
DISTRO=Arch
Registered Linux User #388732
- 08-27-2007 #10
thanks for the link
hey i had read somthings about man fork, system,..
thanks for the link i will read on it, hey so what u suggest i need to do so that i can complete this task?


Reply With Quote