Solution to First Assignment - Operating System | CSE 430, Quizzes of Operating Systems

Material Type: Quiz; Professor: Syrotiuk; Class: Operating Systems; Subject: Computer Science and Engineering; University: Arizona State University - Tempe; Term: Fall 2012;

Typology: Quizzes

2011/2012

Uploaded on 09/29/2012

hnquy
hnquy 🇺🇸

5

(2)

14 documents

1 / 5

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
ARIZONA STATE UNIVERSITY
CSE 430 Operating Systems Fall 2012
SLN 71250
Solution to Assignment #1
1. The answer to this question may depend on the assumptions made. This solution assumes the processor
is a uniprocessor.
At time 19:
(a) P1is blocked since is issued a read command at time 5.
(b) P3is assumed to be ready since it will execute at time 20.
(c) P5is ready since its time slice expired at time 15.
(d) P7is blocked since it issued a read command at time 18.
(e) P8will terminate at time 38 but since we don’t know anything else about P8or whether it
terminates normally, it could be in any state.
At time 34:
(a) P1is still ready (since nothing else has happened to it).
(b) P3blocks at time 20, but its read completes at time 33 so it is once again ready.
(c) P5blocks at time 24 and is swapped out at time 28. At time 34 it is still in the blocked/suspend
state.
(d) P7remains blocked.
(e) P8will terminate at time 38 but since we don’t know anything else about P8or whether it
terminates normally, it could be in any state.
At time 49:
(a) P1is in the ready state because it’s read completes at time 36.
(b) P3is still in the ready state.
(c) P5was in blocked/suspend but it’s write completes at time 40 moving it to the ready suspend
state. It is then swapped back in, so it moves to the ready state at time 44.
(d) P7completes a write at time 48 and so moves from the blocked to the ready state.
2. The solution described here is from our textbook. It is for the Unix System V (SVR4) OS:
(a) The process state transition diagram is given in Figure 3.17. It has 9 states as given in Table 3.9.
It is similar to the seven-state transition diagram in Figure 3.9(b). Some differences include:
UNIX has two sleeping states (“Asleep in memory” and “Sleep, swapped”). These correspond
to the two blocked states in the seven-state transition diagram.
UNIX has two running states. One indicates that the process is executing in user mode, the
other indicates that the process is executing in kernel mode.
The two states “Ready to run, in memory” and “Preempted” are essentially the same because
all processes in these states are in the same queue. The purpose is to differentiate the way in
which the state was entered, either preempted by the kernel, or after the event the process
was blocked on occurs, respectively.
1
pf3
pf4
pf5

Partial preview of the text

Download Solution to First Assignment - Operating System | CSE 430 and more Quizzes Operating Systems in PDF only on Docsity!

ARIZONA STATE UNIVERSITY

CSE 430 — Operating Systems — Fall 2012

SLN 71250

Solution to Assignment

  1. The answer to this question may depend on the assumptions made. This solution assumes the processor is a uniprocessor. At time 19:

(a) P 1 is blocked since is issued a read command at time 5. (b) P 3 is assumed to be ready since it will execute at time 20. (c) P 5 is ready since its time slice expired at time 15. (d) P 7 is blocked since it issued a read command at time 18. (e) P 8 will terminate at time 38 but since we don’t know anything else about P 8 or whether it terminates normally, it could be in any state.

At time 34:

(a) P 1 is still ready (since nothing else has happened to it). (b) P 3 blocks at time 20, but its read completes at time 33 so it is once again ready. (c) P 5 blocks at time 24 and is swapped out at time 28. At time 34 it is still in the blocked/suspend state. (d) P 7 remains blocked. (e) P 8 will terminate at time 38 but since we don’t know anything else about P 8 or whether it terminates normally, it could be in any state.

At time 49:

(a) P 1 is in the ready state because it’s read completes at time 36. (b) P 3 is still in the ready state. (c) P 5 was in blocked/suspend but it’s write completes at time 40 moving it to the ready suspend state. It is then swapped back in, so it moves to the ready state at time 44. (d) P 7 completes a write at time 48 and so moves from the blocked to the ready state.

  1. The solution described here is from our textbook. It is for the Unix System V (SVR4) OS:

(a) The process state transition diagram is given in Figure 3.17. It has 9 states as given in Table 3.9. It is similar to the seven-state transition diagram in Figure 3.9(b). Some differences include:

  • UNIX has two sleeping states (“Asleep in memory” and “Sleep, swapped”). These correspond to the two blocked states in the seven-state transition diagram.
  • UNIX has two running states. One indicates that the process is executing in user mode, the other indicates that the process is executing in kernel mode.
  • The two states “Ready to run, in memory” and “Preempted” are essentially the same because all processes in these states are in the same queue. The purpose is to differentiate the way in which the state was entered, either preempted by the kernel, or after the event the process was blocked on occurs, respectively.

Created

Sleep, Swapped

Ready to Run In Memory Ready to Run Swapped

Asleep in Zombie Memory

Kernel Running

User Running

Preempted

fork

not enough memory memoryenough (swapping system only)

swap in

swap out

swap out

sleep wakeup wakeup

return

preempt

return to user

system call, interrupt

exit

reschedule process

interrupt, interrupt return

Figure 3.17 UNIX Process State Transition Diagram

Table 3.9 UNIX Process States

User Running Executing in user mode.

Kernel Running Executing in kernel mode.

Ready to Run, in Memory Ready to run as soon as the kernel schedules it.

Asleep in Memory Unable to execute until an event occurs; process is in main memory (a blocked state).

Ready to Run, Swapped Process is ready to run, but the swapper must swap the process into main memory before the kernel can schedule it to execute.

Sleeping, Swapped The process is awaiting an event and has been swapped to secondary storage (a blocked state).

Preempted Process is returning from kernel to user mode, but the kernel preempts it and does a process switch to schedule another process.

Created Process is newly created and not yet ready to run.

Zombie Process no longer exists, but it leaves a record for its parent process to collect.

In UNIX, the process table entry defines the state of a process. The contents of the process table entry are given in Table 3.11 and is considered part of the system-level context of a process, i.e., part of the information needed to manage the process. It contains only part of the elements in the PCB in Table 3.5 (mostly for process identification, but also some process control informa- tion). UNIX also has a “U” (user) area containing the remaining process control information and processor state information; see Table 3.12. (b) In the case the OS you selected is multi-threaded, then some differences between the PCB and TCB are as follows. In general, resources are owned by the process and each thread has its own execution state. A few general comments about each category in Table 3.5:

  • Identification: The process must be identified but each thread within the process must have its own thread identifier.
  • Processor State Information: These are generally thread-related. For ULTs, the process can be in a certain state (say, running), but each thread will also have a state (one running, the rest in other states for example). For KLTs, each thread will have its own state. As well, user-visible registers, the PC, and stack pointers are part of the thread context and so this information is thread dependent.
  • Process Control Information: Scheduling and state information would mostly be at the thread level; data structuring could appear at both levels; interprocess communication and inter- thread communication may both be supported; privileges may be at both levels; memory management would generally be at the process level; and resource information would generally be at the process level.
  1. In general, the operating system must save the context of the currently executing process and restore the context of the process scheduled to run next by the dispatcher. Saving the context of a process typically includes saving everything in its PCB, such as the values of all the CPU registers, its memory allocation, etc. Context switches must also perform many architecture-specific operations, such as flushing data and instruction caches. How the OS executes affects the actions taken. In particular, in a non-process kernel, the kernel executes outside of any process. With this approach, when the currently executing process moves out of the run state, the context of this process is saved once control is passed to the kernel. The OS has its own region of memory to use and its own system stack. The OS performs any desired functions; this may include dispatching a process from the ready queue and restoring its context. In general, both a context and a mode switch are required.
  2. (a) The function counts the number of positive elements in a list.

(b) This should work correctly, because count positives in this specific case does not update global positives, and hence the two threads operate on distinct global data and require no locking. Source: Boehn, H. et al. “Multithreading in C and C++,” February 2007. (c) This transformation is consistent with the C language specification, which addresses only single- threaded execution. In a single-threaded environment, it is indistinguishable from the original.

However, in a multi-threaded environment, the transformed version is quite different, in that it assigns to global positives, even if the list contains only negative elements. The original program is now broken, because the update of global positives by thread B may be lost, as a result of thread A writing back an earlier value of global positives. A thread-unaware compiler has turned a perfectly legitimate program into one with undefined semantics. Source: Boehn, H. et al. “Multithreading in C and C++,” February 2007.

  1. (a) For x is 10, the interleaving producing the required behaviour is easy to find since it requires only an interleaving at the source language statement level. The essential fact here is that the test for the value of x is interleaved with the increment of x by the other process. Thus, x was

not equal to 10 when the test was performed, but was equal to 10 by the time the value of x was read from memory for printing.

M(x) P1: x = x - 1; 9 P1: x = x + 1; 10 P2: x = x - 1; 9 P1: if( x != 10 ) 9 P2: x = x + 1; 10 P1: printf( "x is %d\n", x ); 10

(b) For x is 8 we need to be more inventive, since we need to use interleavings of the machine instructions to find a way for the value of x to be established as 9 so it can then be evaluated as 8 in a later cycle. Notice how the first two blocks of statements correspond to C source lines, but how later blocks of machine language statements interleave portions of a source language statement.

Instruction M(x) P1-R0 P2-R P1: LD R0, x 10 10 -- P1: DECR R0 10 9 -- P1: STO R0, x 9 9 --

P2: LD R0, x 9 9 9 P2: DECR R0 9 9 8 P2: STO R0, x 8 9 8

P1: LD R0, x 8 8 8 P1: INCR R0 8 9 --

P2: LD R0, x 8 9 8 P2: INCR R0 8 9 9 P2: STO R0, x 9 9 9 P2: if(x != 10) printf("x is %d\n", x); P2: "x is 9" is printed.

P1: STO R0, x 9 9 9 P1: if(x != 10) printf("x is %d\n", x); P1: "x is 9" is printed.

P1: LD R0, x 9 9 9 P1: DECR R0 9 8 -- P1: STO R0, x 8 8 --

P2: LD R0, x 8 8 8 P2: DECR R0 8 8 7 P2: STO R0, x 7 8 7

P1: LD R0, x 7 7 7 P1: INCR R0 8 8 7 P1: STO R0, x 8 8 7 P1: if(x != 10) printf("x is %d\n", x); P1: "x is 8" is printed.