STAFFORDSHIRE UNIVERSITY

Operating Systems

Portfolio of exercises

Benoît Taine (11029054) 20/11/2012

Apply to the solution of a range of problems, the fundamental concepts, principles and algorithms employed in the operation of a multi-user/multi-tasking operating systems. SUMMARY

WEEK 1 ...... 2

Tutorial questions ...... 2

WEEK 2 ...... 2

Tutorial Questions ...... 2

WEEK 3 ...... 2

Virtual machine exercise ...... 2

Tutorial questions ...... 2

WEEK 4 ...... 3

Tutorial questions ...... 3

WEEK 5 ...... 2

Tutorial question ...... 2

Virtual machine exercises with Linux processes ...... 2

WEEK 6 ...... 3

Tutorial question ...... 3

Virtual machine exercises – simple Windows system programming ...... 5

WEEK 7 ...... 6

Tutorial questions ...... 6

Virtual machine exercise – Linux interprocess communication ...... 6

WEEK 8 ...... 7

Tutorial question ...... 7

WEEK 9 ...... 8

Tutorial questions ...... 8

WEEK 10 ...... 9

Tutorial questions ...... 9

Operating Systems Portfolio of exercises 1/12 WEEK 1

TUTORIAL QUESTIONS

6. How does the distinction between supervisor mode and user mode function as a rudimentary form of protection (security) system? (3 marks)

User mode doesn't allow user to perform some tasks, like system administration or modification, while the supervisor mode has all the rights.

7. What are the differences between software interrupt (trap) and hardware interrupt? What is the use of each? (3 marks)

A hardware interrupt is sent from a piece of hardware, like a pressed keyboard key. A software interrupt is thrown to let a program execute instructions with a higher priority.

WEEK 2

TUTORIAL QUESTIONS

4. What is the purpose of system calls? Why are they necessary? (3 marks)

It gives to the user programs an interface to use an procedure. They are necessary to use threads or forked processes, or simply to use a file opening routine.

WEEK 3

VIRTUAL MACHINE EXERCISE

16. Using an editor (can be the simple editor you used in 1), write a short (two or four line) shell script which applies sort and head to a test file and will give a sorted list of the first five lines in the file. (4 marks)

#!/bin/sh sort "${1}" | head -n 5

TUTORIAL QUESTIONS

12. The ability of a to create a new process is an important capability, but it has dangers. Consider the consequences of allowing a user to run the process below. Assume that () is a that creates a new child process.

int main() { while (true) { fork(); } }

a) What would the consequences of allowing such a process to run? (2 marks)

Operating Systems Portfolio of exercises 2/12 Running a fork bomb program has never been a good idea. The system will try to create an infinite number of process control blocks and crash.

b) Suppose that you are an operating systems designer and have been asked to build in safeguards against such processes. Assume that you have decided that it is inappropriate to reject certain processes, and that the best approach is to place certain runtime controls on them. What controls might the operating system use to detect processes like the above at runtime? (4 marks)

We can limit the number of processes a user is allowed to execute at the same time. On Linux systems, we use the ulimit command or the /etc/security/limits.conf file to configure this number.

WEEK 4

TUTORIAL QUESTIONS

6. Consider the following set of processes, with the length of the CPU-burst time given in milliseconds:

Process Burst time Priority P1 5 2 P2 1 1 P3 3 3 P4 1 4 P5 7 3

At time 0, the processes are queued in the order of arrival P1, P2, P3, P4, P5.

a) Draw four Gantt charts illustrating the execution of these processes using: (8 marks)  FCFS  Non-preemptive priority  SJF  Round Robin FCFS

P1 P2 P3 P4 P5 0 5 6 9 10 17

SJF

P2 P4 P3 P1 P5 0 1 2 5 10 17

Non-preemptive priority

P2 P1 P3 P5 P4 0 1 6 9 16 17

Round Robin

P1 P2 P3 P4 P5 P1 P3 P5 P1 P3 P5 P1 P5 P1 P5 P5 P5 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17

b) What is the turnaround time of each process for each of the scheduling algorithms in part a? (8 marks)

Operating Systems Portfolio of exercises 3/12 FCFS SJF NP P RR P1 1 10 6 15 P2 6 1 1 3 P3 9 5 8 11 P4 10 17 17 14 P5 17 16 16 17

c) What is the waiting time of each process for each of the scheduling algorithms in part a? (8 marks)

FCFS SJF NP P RR P1 0 5 1 13 P2 5 0 0 2 P3 6 2 6 10 P4 9 1 16 4 P5 10 10 8 13 Average 6 3.6 5.6 8.4

d) Which of the schedules in part a results in the minimal average waiting time (over all processes)? (2 marks)

The SJF has the lowest average waiting time.

8. Explain how interleaving of the execution of processes can corrupt shared data values.

The following are common scheduling criteria:

a) I/O boundedness of a process f) Frequency of a process being pre- empted by higher priority process b) Processor boundedness of a process g) Priorities of processes waiting for c) Whether the process is batch or resources held by other processes interactive h) Accumulated waiting time d) Urgency of a fast response i) Accumulated execution time e) Process priority j) Estimated run-time to completion

For each of the following, indicate which of the preceding scheduling criteria is most appropriate

i) In a real-time spacecraft monitoring iii) How often does the process system, the computer must respond voluntarily give up the processor for I/O immediately to signals received from the before its quantum has expired. spacecraft iv) Is the user present and expecting fast ii) Although a process has been receiving interactive response times, or is the user occasional service, it is making only absent. nominal progress.

Operating Systems Portfolio of exercises 1/12 v) One goal of processor scheduling is to vii) Nearly complete processes should have higher minimize average waiting times priorities.

vi) Processes holding resources in demand by other processes should have higher priorities. (7 marks)

I II III IV V VI VII D A H C J G E

WEEK 5

TUTORIAL QUESTION

5. What are the four necessary conditions needed before deadlock can occur? (4 marks)

If the system has mutual exclusions, a hold and wait condition, no pre-emption and circular wait, it could generate a deadlock condition. The solution used to prevent deadlock conditions is to make sure the system won’t have any circular wait.

VIRTUAL MACHINE EXERCISES WITH LINUX PROCESSES

17. Use your forktest.c program as the basis, but modify it by deleting the printf that occur under the if and the else that output the PIDs and replace them with code for the parent to output the series of odd numbers in an infinite loop and for the child process to output the series of even numbers in an infinite loop. (6 marks)

#include #include #include int main (int argc, char *argv[]) { int i; pid_t id = 0;

if (!(val = fork())) for (n = 1; n < 50; n += 2) printf("P- %d\n", i); else for (n = 0; n < 50; n += 2) printf("F- %d\n", i);

return 0; }

23. Up till now the parent process has continued to execute while the child process has executed. We can use the wait() system call to make the parent process suspend itself and wait until it is woken up by the termination of the child process.

So modify forktest4.c to make the parent process wait for the child. You will need to use the wait() system call. (5 marks)

Operating Systems Portfolio of exercises 2/12 #include #include #include #include int main (int argc, char *argv[]) { int i; pid_t id = 0;

printf("Parent PID pre-fork: %d\n", (int) getpid());

if (!(val = fork())) { printf("Parent PID post-fork: %d\n", (int) getpid()); wait(&i); } else execl("/bin/ls", "ls", "-l", 0);

wait(&i); return 0; }

WEEK 6

TUTORIAL QUESTION

4. What is a safe state?

If all processes can safely execute without generating any deadlock conditions, the system is said in safe state.

5. Consider the following snapshot of a system

Allocation Max Available A B C D A B C D A B C D P0 0 0 1 2 P0 0 0 1 2 P0 1 5 2 0 P1 1 0 0 0 P1 1 7 5 0 P2 1 3 5 4 P2 2 3 5 6 P3 0 6 3 2 P3 0 6 5 2 P4 0 0 1 4 P4 0 6 5 6

Answer the following questions using the banker’s algorithm.

a) What is the content of the matrix Need?

A B C D P0 0 0 0 0 P1 0 7 5 0 P2 1 0 0 2 P3 0 6 2 0 P4 0 6 4 2

b) Is the system in a safe state?

Operating Systems Portfolio of exercises 3/12 A B C D A B C D A B C D A B C D P0 0 0 0 0 P0 0 0 1 2 1 5 3 2 P0 0 0 0 0 P1 1 0 0 0 P1 1 7 5 0 P1 0 7 5 0 P2 1 3 5 4 P2 2 3 5 6 P2 1 0 0 2 P3 0 6 3 2 P3 0 6 5 2 P3 0 6 2 0 P4 0 0 1 4 P4 0 6 5 6 P4 0 6 4 2

P0 finished.

A B C D A B C D A B C D A B C D P0 0 0 0 0 P0 0 0 1 2 2 8 8 6 P0 0 0 0 0 P1 1 0 0 0 P1 1 7 5 0 P1 0 7 5 0 P2 0 0 0 0 P2 2 3 5 6 P2 0 0 0 0 P3 0 6 3 2 P3 0 6 5 2 P3 0 6 2 0 P4 0 0 1 4 P4 0 6 5 6 P4 0 6 4 2

P2 finished.

A B C D A B C D A B C D A B C D P0 0 0 0 0 P0 0 0 1 2 3 8 8 6 P0 0 0 0 0 P1 0 0 0 0 P1 1 7 5 0 P1 0 7 5 0 P2 0 0 0 0 P2 2 3 5 6 P2 0 0 0 0 P3 0 0 0 0 P3 0 6 5 2 P3 0 6 2 0 P4 0 0 1 4 P4 0 6 5 6 P4 0 6 4 2

P1 finished.

A B C D A B C D A B C D A B C D P0 0 0 0 0 P0 0 0 1 2 3 14 11 8 P0 0 0 0 0 P1 0 0 0 0 P1 1 7 5 0 P1 0 7 5 0 P2 0 0 0 0 P2 2 3 5 6 P2 0 0 0 0 P3 0 0 0 0 P3 0 6 5 2 P3 0 6 2 0 P4 0 0 1 4 P4 0 6 5 6 P4 0 6 4 2

P3 finished.

We proved that the system was in a safe state using the Banker’s algorithm.

c) If a request from process P1 arrives for (0,4,2,0), can the request be granted immediately?

The sequence (1,5,2,0) is immediately available, so the request can be granted immediately.

Operating Systems Portfolio of exercises 4/12 VIRTUAL MACHINE EXERCISES – SIMPLE WINDOWS SYSTEM PROGRAMMING

14. Now we are in a position to modify the program above. We are going to change it so that now instead of copying a file from one file to another, the code will copy a file but output it to the console window. So it is a little like the DOS Type command.

#include #include #define BUF_SIZE 256 int main (int argc, LPTSTR argv []) { HANDLE hIn, hOut; DWORD nIn, nOut; CHAR Buffer [BUF_SIZE];

if (argc != 2) { printf ("Usage: cat file\n"); return 1; }

hIn = CreateFile (argv [1], GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if (hIn == INVALID_HANDLE_VALUE) { printf ("Cannot open input file. Error: %x\n", GetLastError ()); return 2; }

hOut = GetStdHandle(STD_OUTPUT_HANDLE); if (hOut == INVALID_HANDLE_VALUE) { printf ("Cannot open standard output. Error: %x\n", GetLastError ()); return 3; }

while (ReadFile (hIn, Buffer, BUF_SIZE, &nIn, NULL) && nIn > 0) { WriteFile (hOut, Buffer, nIn, &nOut, NULL); if (nIn != nOut) { printf ("Fatal write error: %x\n", GetLastError ()); return 4; } } CloseHandle (hIn); CloseHandle (hOut); return 0; }

Operating Systems Portfolio of exercises 5/12 WEEK 7

TUTORIAL QUESTIONS

4) Given memory partitions of 200K, 425K, 100K, 700K, and 300K (in order), how would each of the First-fit, Best-fit, and Worst-fit algorithms place processes of 212K, 417K, 112K, and 426K (in order)? Which algorithm makes the most efficient use of memory?

Remember that space left over after a process has been placed in a partition will then become part of the free space available for later allocations. (12 marks)

First fit

200 K 450 K 100 K 700 K 300 K 112 K 212 K 417 K

Best fit

200 K 450 K 100 K 700 K 300 K 112 K 417 K 426 K 212 K

Worst fit

200 K 450 K 100 K 700 K 300 K 112 K 212 K 417 K

VIRTUAL MACHINE EXERCISE – LINUX INTERPROCESS COMMUNICATION

14. Use pipes to communicate between the parent and child process.

Remember the parent process repeatedly writes the alphabet to the pipe using an infinite loop and the child process repeatedly reads from the pipe and actually outputting the characters to the screen. (10 marks)

#include #include #include #include #include

#define SIZE 26 #define ALPHA 97 int main(int argc, char *argv[]) { int i, count, fd; char buf[SIZE]; char ch; int fda[2];

if (pipe(fda) == -1) printf("Error creating pipe\n");

if (fork()) // Parent process

Operating Systems Portfolio of exercises 6/12 { // Repeatedly fill the buffer with the alphabet for (; ;) { for (i = 0; i < SIZE; i++) buf[i] = ALPHA + i;

// Pipe contents of the buffer to child process if (write(fda[1], buf, SIZE) !=SIZE) printf("error writing to file\n"); } } else // Child process { // Read from pipe for (; ;) { if (read(fda[0], buf, SIZE) == -1) printf("error reading file\n");

// Output content of the piped buffer for (i = 0; i < SIZE; i++) printf("%c", buf[i]); } close(fda[1]); puts("\n"); }

close(fda[0]); }

WEEK 8

TUTORIAL QUESTION

4) Consider the following page reference string:

1, 2, 3, 2, 4, 2, 5, 2, 3, 6, 2, 5, 1, 2, 3

How many page faults would occur for the following replacement algorithms, assuming three and four frames? Remember all frames are initially empty, so your first unique pages will all cost one fault each.

 LRU replacement  FIFO replacement  Optimal replacement

(6 marks)

LRU replacement

1 1 1 1 4 4 4 4 3 3 3 5 5 5 3

2 2 2 2 2 2 2 2 2 2 2 2 2 2

3 3 3 3 5 5 5 6 6 6 1 1 1

Operating Systems Portfolio of exercises 7/12

1 1 1 1 1 1 5 5 5 5 5 5 5 5 5

2 2 2 2 2 2 2 2 2 2 2 2 2 2

3 3 3 3 3 3 3 3 3 3 1 1 1

4 4 4 4 4 6 6 6 6 6 3

FIFO replacement

1 1 1 1 4 4 4 4 3 3 3 3 1 1 1

2 2 2 2 2 5 5 5 6 6 6 6 2 2

3 3 3 3 3 2 2 2 2 5 5 5 3

1 1 1 1 1 1 5 5 5 5 5 5 5 5 3

2 2 2 2 2 2 2 2 6 6 6 6 6 6

3 3 3 3 3 3 3 3 2 2 2 2 2

4 4 4 4 4 4 4 4 1 1 1

Optimal replacement

1 1 1 1 4 4 5 5 5 5 5 5 1 1 1

2 2 2 2 2 2 2 2 2 2 2 2 2 2

3 3 3 3 3 3 3 6 6 6 6 6 3

1 1 1 1 1 1 1 1 1 1 1 1 1 1 3

2 2 2 2 2 2 2 2 2 2 2 2 2 2

3 3 3 3 3 3 3 6 6 6 6 6 6

4 4 5 5 5 5 5 5 5 5 5

WEEK 9

TUTORIAL QUESTIONS

3. Assume a system uses 2K blocks and 16 bit (2 bytes) addresses. What is the largest file size that such a system can support using indexed block file allocation with a single level index. Comment on the resulting maximum size in terms of its adequacy for a modern filing system. (2 marks)

The maximum supportable file size is determined by the size of a block and the number of blocks.

( )

12. Explain first-fit, best-fit, and worst-fit methods of allocating space for contiguous files.

 First-fit: Allocate the first space that is big enough  Best-fit: Allocate the smallest space that is big enough  Worst-fit: Allocate the largest space

Operating Systems Portfolio of exercises 8/12 13. What is external fragmentation in a system with contiguous files?

When the free memory is dispatched into small blocks placed between already allocated blocks, the small blocks are not suitable for the programs because they are too small to meet the minimum allocatable size to be used efficiently.

WEEK 10

TUTORIAL QUESTIONS

1. Suppose that a disk drive has 5000 cylinders, numbered 0 to 4999. The drive is currently serving a request at cylinder 175, and the previous request was at cylinder 89. The queue of pending requests, in FIFO order, is 186, 2460, 513, 1764, 942, 509, 1122, 1250, 1260.

Starting from the current head position, what is the total distance (in cylinders) that the disk arm moves to satisfy all the pending requests, for each of the following disk scheduling algorithms?

 FCFS  SCAN

FCFS SCAN

Cylinder Moves Cylinder Moves 175 → 186 11 175 → 0 175 186 → 2460 2274 175 → 186 11 2460 → 513 1947 186 → 509 323 513 → 1764 1251 509 → 513 4 1764 → 942 822 513 → 942 429 942 → 509 433 942 → 1122 180 509 → 1122 613 1122 → 1250 128 1122 → 1250 128 1250 → 1260 10 1250 → 1260 10 1260 → 1764 504 Total 7489 1764 → 2460 696 Total 2460

Operating Systems Portfolio of exercises 9/12