0% found this document useful (0 votes)
60 views5 pages

This Study Resource Was: Tutorial 5

The document contains three questions regarding threads and processes. Question 1 discusses which threads may run after the current thread finishes if threads are implemented at the user level versus with OS support. Question 2 discusses which threads may run immediately after the current thread finishes under the same two scenarios. Question 3 provides a pthreads program and asks what would be output at two lines if the program is run.

Uploaded by

Jawad Ali
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)
60 views5 pages

This Study Resource Was: Tutorial 5

The document contains three questions regarding threads and processes. Question 1 discusses which threads may run after the current thread finishes if threads are implemented at the user level versus with OS support. Question 2 discusses which threads may run immediately after the current thread finishes under the same two scenarios. Question 3 provides a pthreads program and asks what would be output at two lines if the program is run.

Uploaded by

Jawad Ali
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
You are on page 1/ 5

Tutorial 5

Q2. Suppose that three concurrent processes exist in a system, as described in the following table:

Process Threads within the Process


P1 T11, T12, T13
P2 T21, T22
P3 T31
Suppose that the system uses round-robin scheduling, and that T11 is running when the scheduling
quantum expires. If the threads are implemented entirely at the user level, with no support from the
operating system, indicate which threads might possibly be executing at the beginning of the next
quantum. If the threads are supported by the operating system, indicate which threads might possibly be
executing at the beginning of the next quantum.

If the threads are not supported by the operating system, T21, T22, or T31 will run during the
next quantum.
If the threads are supported by the operating system, then any thread except T11 will run
during the next quantum, assuming not all threads are blocked and scheduling is round-

m
robin.

er as
Q3. This question assumes the same processes and threads as the previous question. Suppose that thread

co
eH w
T11 finishes executing and terminates. If the threads are implemented entirely at the user level, with no
support from the operating system, indicates which threads might possibly run immediately after T11

o.
finishes. If the threads are supported by the operating system, indicate which threads might possibly run
immediately after T11 finishes rs e
ou urc
If the threads are not supported by the operating system, either T12 or T13 will run after T11
finishes. If the threads are supported by the operating system, any of the other threads may
run.
o
aC s
vi y re

Q6. The following program uses the pthreads API. What would be output from the program at LINE C
and LINE P?

#include <pthread.h>
ed d

#include <stdio.h>
ar stu

int value = 0;
void *runner(void *param); /* the thread */
int main(int argc, char *argv[])
{
is

int pid;
pthread_t tid;
Th

pthread_attr_t attr;
pid = fork();
if (pid == 0)
{
sh

Value=10
pthread_attr_init (&attr) ;
pthread_create(&tid,&attr,runner,NULL) ;
pthread_join(tid,NULL) ;
printf("CHILD: value = %d",value); /* LINE C */
} else if (pid > 0)

This study source was downloaded by 100000822060161 from CourseHero.com on 05-06-2021 02:11:18 GMT -05:00

https://www.coursehero.com/file/12935618/Practice-ques/
{
wait (NULL) ;
printf("PARENT: value = %d",value); /* LINE P */
}
}
void *runner(void *param)
{
value += 5;
pthread_exit ( 0) ;
}

Example 1

At t=0 there are three CPU bound processes (no I/O) in the system. The CPU burst lengths are:
30 units for P1, 20 units for P2, and 10 units for P3. The system has three RR queues with the

m
following time slices: 1 for queue1, 2 for queue 2, and 4 for queue3.

er as
Compare RR scheduling (time slice = 1) with MLFQ scheduling.

co
eH w
o.
Example 2
rs e
ou urc
Consider the processes of Example 1 with the following modifications:
(1) There are only two queues with time slices of 2 for queue1 and 4 for queue 2.
o

(2) Process P3 has an I/O burst of 1 unit after each CPU burst of 2 units. After each I/O burst P3
aC s

returns to queue 1.
vi y re

Compare RR scheduling (time slice = 1) with MLFQ scheduling.

Assume that if a process does not complete its time slice when it is preempted, it remains at the
same priority level.
ed d
ar stu

Example 3

Consider the following processes. Each process makes a CPU burst then an I/O burst, another
is

CPU burst, another I/O burst and terminates with a CPU burst. The lengths of the CPU burst and
I/O burst times in milliseconds are given in the following table:
Th

Process CPU-burst1 I/O-burst1 CPU-burst2 I/O-burst2 CPU-burst3 Arrival

P1 2 4 2 2 2 0
sh

P2 2 2 3 3 1 1
P3 1 2 1 1 1 1

The processes are assumed to arrive as indicated. Draw a Gantt chart that illustrates the
execution of these processes using the round robin (RR) scheduling algorithm with quantum = 2.

This study source was downloaded by 100000822060161 from CourseHero.com on 05-06-2021 02:11:18 GMT -05:00

https://www.coursehero.com/file/12935618/Practice-ques/
If an I/O completion and a CPU timeout of two processes occur at the same time, we treat the I/O
completion first.

Soln 1

m
er as
Q1: 123
Q2: 112233 T T T

co
Q3: 111122223333111122223331111222211112222111121111111

eH w
Time: 1 2 3 4 5 6
123456789012345678901234567890123456789012345678901234567890

o.
Completion Times rs e
ou urc
RR P1:60; P2:50; P3:30 Mean Completion: (60+50+30)/3 = 46 2/3
MLFQ P1:60; P2:53; P3:32 Mean Completion: (60+53+32)/3 = 48 1/3
o

Waiting Times
RR P1:30; P2:30; P3:20 Mean Waiting: (30+30+20)/3 = 26 2/3
aC s

MLFQ P1:30; P2:33; P3:22 Mean Waiting: (30+33+22)/3 = 28 1/3


vi y re

Answer 2
ed d
ar stu

The following GANTT chart.


p Process p, p=1,2,3 is running
I Process P3 is blocked on I/O device
Qk Queue on level k
is

T
Q1: 112233 33 33 33 33 T T
Th

Q2: 1 1 1 1 222211112222111122221111222211112211111111
I/O I I I I
Time: 1 2 3 4 5 6
123456789012345678901234567890123456789012345678901234567890
sh

Completion Times
RR P1:60; P2:50; P3:30 Mean Completion: (60+50+30)/3 = 46 2/3
MLFQ P1:60; P2:52; P3:18 Mean Completion: (60+52+18)/3 = 43 1/3

Waiting Times
RR P1:30; P2:30; P3:20 Mean Waiting: (30+30+20)/3 = 26 2/3
MLFQ P1:30; P2:32; P3:4 Mean Waiting: (30+32+4)/3 = 22

This study source was downloaded by 100000822060161 from CourseHero.com on 05-06-2021 02:11:18 GMT -05:00

https://www.coursehero.com/file/12935618/Practice-ques/
1. #include<stdio.h>
2. #include<pthread.h>

3. int global_data = 10;

4. void *PrintThreadData(void *glo){


5. printf("Inside thread function. Global data value %d
\n",global_data);
6. global_data=20;
7. printf(" Inside function again. Global data now is %d \n“
,global_data);
8. }
9. main(){
10. pthread_t mythread;

11. pthread_create(&mythread,NULL,PrintThreadData,NULL);
12. printf("Parent here. Global data is %d \n", global_data);

m
er as
13. global_data=30;
14. pthread_join(mythread,NULL);

co
15. printf("End of parent. Global data is %d \n",global_data);

eH w
16. }

o.
rs e
ou urc
1. [gagandeep@linux PThreads]$
o

./globdata
aC s
vi y re

2. Parent here. Global data is 10


3. Inside thread function. Global
ed d

data value 30
ar stu

4. Inside function again. Global


data now is 20
is

5. End of parent. Global data is


Th

20
sh

This study source was downloaded by 100000822060161 from CourseHero.com on 05-06-2021 02:11:18 GMT -05:00

https://www.coursehero.com/file/12935618/Practice-ques/
How many new processes are created in the following code?
/* process A */
/* ... */
pid = fork();
if (pid == 0) {
pid = fork();
printf("Hi!");
} else {
pid = fork();
printf("Hi!");
if (pid == 0) {
pid = fork();

m
printf("Hi!");

er as
} else {

co
pid = fork();

eH w
printf("Hi!");
}

o.
}
/* ... */ rs e
ou urc
o
aC s
vi y re
ed d
ar stu
is
Th
sh

This study source was downloaded by 100000822060161 from CourseHero.com on 05-06-2021 02:11:18 GMT -05:00

https://www.coursehero.com/file/12935618/Practice-ques/
Powered by TCPDF (www.tcpdf.org)

You might also like