nabs
new

Posts: 1
|
 |
« on: August 25, 2007, 03:52:53 pm » |
|
Process Creation Using fork()
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
|