0% found this document useful (0 votes)
4 views39 pages

Operating Systems ECE355 Tutorial 2 Lab 2 Process States: Eng. Aliaa Eslam

The document is a tutorial for ECE355 on process states, containing a series of questions related to predicting outputs of C programs, calculating printed outputs, and understanding the behavior of child processes and the wait() system call. It also discusses the implications of zombie and orphan processes in operating systems. The tutorial is authored by Eng. Aliaa Eslam and includes contact information for further inquiries.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views39 pages

Operating Systems ECE355 Tutorial 2 Lab 2 Process States: Eng. Aliaa Eslam

The document is a tutorial for ECE355 on process states, containing a series of questions related to predicting outputs of C programs, calculating printed outputs, and understanding the behavior of child processes and the wait() system call. It also discusses the implications of zombie and orphan processes in operating systems. The tutorial is authored by Eng. Aliaa Eslam and includes contact information for further inquiries.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Operating Systems

ECE355
Tutorial 2
Lab 2
Process States

Eng. Aliaa Eslam


Question1: Calculate the number of times hello is printed
Question2: Predict the Output of the following program
Question3: Predict the Output of the following program
Question4: Predict the Output of the following program
Question5: Predict the Output of the following program
Question6: Predict the Output of the following program
Question7: Predict the Output of the following program
Question8: Predict the Output of the following program
Question9: How many process will be created by executing the
following code ?
Question 10
Question11
Question12
Question 13 : Using the below program , identify the values of pid at lines
A, B, C, and D. (Assume that the actual pids of the parent and child are
2600 and 2603, respectively.) what do you observe regarding outputs at
lines B,C? Comment on the results.
Question14: Using the following C program , explain what the
output will be at LINE A
Question15: Predict the Output of the following program
Question16: Predict the Output of the following program
Question17: Predict the Output of the following program
Wait():
the calling process until one of its child process exits or a signal is
received.

Child process may terminate due to any of these:


• It calls exit ()
• It returns (an int) from main
• It receives a signal (from the OS or another process) whose default action
is to terminate.
wait () system call in c :
store the location of the status information of the child
process from the *status parameter.
The function can return :
• Process ID of the terminated child process
• -1 if any process has no child process then wait ()

To return the status code of the child, you have to use


WEXITSTATUS macro

If any process as more than one child process, then after calling
wait (),parent process has to be in wait state if no child
terminates
Question18:Using the following C program , explain what the output will
be at LINE A
Question19: Predict the Output of the following program
Zombies and child processes
• The wait() call returns immediately (with error) if no child processes exist
• If a child process, c, terminates *before* the parent process does, c goes
to the zombie state -
until the parent (or a foster parent) does a wait() on c or explicitly kills c.
• If the parent process exits before a child process does, a grandparent pro
cess (e.g. init) becomes the foster parent
• Init will issue wait() for all those foster child processes
• For our project, we really do not want the system to be inundated by zo
mbies (No new processes can be spawned then.)
• Also, if any child process has an infinite loop or blocked indefinitely, they
will continue to exist even after the main program terminates–
We must be careful in our project not to accidentally inundate the syste
m with orphan processes
wait() is a special case of waitpid()
• pid_t waitpid( pid_t pid, int *status, int opts ) •
• pid < -1
Wait for any child process whose process group ID is equal to the
absolute value of pid. (We won’t worry about this in this course)

• pid == -1
Wait for any child process.– wait( ) can be viewed as this case

• pid == 0
Wait for any child process whose process group ID is equal to that of
the calling process.
wait() is a special case of waitpid()
• pid_t waitpid( pid_t pid, int *status, int opts )
• pid >0
Wait for the child whose process ID is equal to the value of pid
options :
Zero or more of the following constants can be ORed.
- WNOHANG » Return immediately if no child has exited.
- WUNTRACED » Also return for children which are stopped, and
whose status has not been reported (because of signal).
Return value
The process ID of the child which exited.
-1 on error; 0 if WNOHANG was used and no child was available
.
Pervious Midterm problems
How to find me?
Eng. Aliaa Eslam
Teaching Assistant of Computer and Systems Engineering
Electronics and Computer Engineering (ECE) Program
Email: AZayd@[Link]
LinkedIn: [Link]
Room: S7

You might also like