You are on page 1of 9

THE TEST ON CS

(1)
Suppose two threads are created thread1 and thread2.Thread1 executes the function do_work_one
() and thread2 executes the function do_work_two().What is the possible output for the following code:
do_work_one()
{
pthread_mutex_lock(&first_mutex);
pthread_mutex_lock(&second_mutex);
sleep(20);
pthread_mutex_unlock(&second_mutex);
pthread_mutex_unlock(&first_mutex);
pthread_exit(0);
}
do_work_two()
{
pthread_mutex_lock(&second_mutex);
pthread_mutex_lock(&first_mutex);
sleep(20);
pthread_mutex_unlock(&first_mutex);
pthread_mutex_unlock(&second_mutex);
pthread_exit(0);
} ?

Let us assume thread1 starts its execution,

first_mutex will be locked by do_work_one()
second_mutex will be locked by do_work_two()

Then do_work_one() will wait for second_mutex
& do work_two() will wait for first_mutex

They keep on waiting for each other which leeds to deadlock situation.
PR Attribute ID Marks New Marks
Page 1 of 9
2/6/2009 http://10.203.161.13/OES/take1.jsp?s1=494&s2=20532
OS_6 4 4
(2)
//There are two different main functions (alternative defns). Which of them will use cache effectively, if the cache siz
e is small? Justify your reason.

//1st Alternative
int main()
{
int i = 0;
while (i++ < 100)
thred1();
while (i++ < 200)
thred2();
}

//2nd Alternative
int main()
{
int i = 0;
while (i++ < 100)
{
thred1();
thred2();
}
}

//Common Code
int gvar = 0;
void thred1() {
int i = 0;
while (i++ < 200)
{
gvar++;
}
}


void thred2() {
int i = 0;
while (i++ < 100)
{
gvar--;
}
} ?
Page 2 of 9
2/6/2009 http://10.203.161.13/OES/take1.jsp?s1=494&s2=20532

If cache size is small it is beter to go for second alternative.

Reasons:

1)It will creates only 200 threads (i.e 100 threads thred1(), 100 for thred2()).
2)Number of threads are minimum
3)Multiple point of executions are minimum.
4)So thread control blocks are enough to manage the process.

So that overhead will be reduced on cache.

If we select the alternative 1, totally 300 thread are created.(i.e 100 for thred1 and 200 thred2)

The major disadvantage is, 300 threead control blocks are to be created with small cache. Over head
will be increased.

It is beter to choose alternative-2

even they are normal functions, it is beter to go for second alternative.
PR Attribute ID Marks New Marks
(3)
A computer that uses 32 - bit registers stores hex value 1234 at address 0.The data stored in Little
endian format at address locations 00 01 10 11 are ?
00 00 12 34
12 34 00 00
00 12 34 00
34 12 00 00
(4)
Suppose your process was executing the library function malloc and at the same time a signal was delivered for whic
h you have written a signal handler.In the handler code there is a call to malloc .What property the malloc function s
hould have to ensure that no inconsistency of data occurs. ?
Recursive
None
No function should be called within the code of malloc.
Malloc needs to access at least one static variable.
(5) All types of signals can be caught and handled by a program by calling "signal()" system call. ?
True
False
Not Answer
(6)
What is the unix command to print the nodename
(the name by which a system is known to a communication network. ?
su
netstat -rn
none of these
Page 3 of 9
2/6/2009 http://10.203.161.13/OES/take1.jsp?s1=494&s2=20532
uname -n
(7)
You have executed the following function call in our code nice(-
10).What is the correct observation based on this function call.(non super user) ?
The priority of your running process increases.
None of them is a correct observation.
The priority of your running process decreases.
The priority of your login shell decreases.
(8)
Write a program in which the main function calls a function, say yourfunc
(), 1000 times in a loop. The function yourfunc
() executes a for loop for 10,000 times to increment a global variable by 1 in each iteration.Print the final value of the
variable in your main function.Use suitable functions in your code to display an estimate of the total execution
time of the loop calling yourfunc() in main. This to be done in Unix system and not in OES. ?

PR Attribute ID Marks New Marks
OS_14 7 6
(9)
Write a program which creates 2 threads and these two communicate with each other using an array of integers of si
ze 2. One thread
(Writer) writes integers into the array (one by one in individual array elements), the other thread (Reader) continuo
usly reads from the array.

However access to the array should be synchronized between the threads so that the first thread should not write a n
ew value in an array element before the second thread has already read the old value from that array element. It sho
uld be possible for Reader and Writer to access different array elements simultaneously however both of them shoul
d not be accessing the same element together. Please use POSIX compliant primitives in this program. ?
Page 4 of 9
2/6/2009 http://10.203.161.13/OES/take1.jsp?s1=494&s2=20532

PR Attribute ID Marks New Marks
OS_2 7 6.75
OS_6 7 6.75
OS_10 9 8.75
OS_9 7 6.75
(10) A 64 bit processor means ?
There are 64 registers
None of these
It can support 64 MB of main memory (max)
It can support 64 I/O devices
(11)
Create a memory management library which provides 2 routines one for dynamically allocating memory and anothe
r one for freeing the allocated memory) .Use linked-list based approach. Don't use brk/sbrk system calls.
?
Page 5 of 9
2/6/2009 http://10.203.161.13/OES/take1.jsp?s1=494&s2=20532

PR Attribute ID Marks New Marks
OS_17 15 15
(12)
Write a program that creates two timers. One of them expires after every 2 seconds and the other one expires after e
very 1.5 seconds. On expiry of each of the timers the program calls two different functions. These different functions
print the message "timer expired" along with the timer id. ?

PR Attribute ID Marks New Marks
Page 6 of 9
2/6/2009 http://10.203.161.13/OES/take1.jsp?s1=494&s2=20532
OS_15 7 4.5
OS_14 8 5.5
(13)
//There are two threads in a program and they are trying to manipulate a global variable simultaneously. Give com
ments on this program.

int gvar = 0;
mutex gmx;

void * thred1(void * thr_p) {
mutex_lock(&gmx);
int i = 0;
gvar=0;
while (i++ < 200)
{
gvar++;
}
mutex_unlock(&gmx);
return (NULL);
}


void * thred2(void * thr_p) {
mutex_unlock(&gmx);
mutex_lock(&gmx);
int i = 0;
gvar=8192;
while (i++ < 200)
{
gvar++;
}
pthread_unlock(&gmx);
return (NULL);
}


int main()
{
create thread - routine thred1()
create thread - routine thred2()
} ?
Page 7 of 9
2/6/2009 http://10.203.161.13/OES/take1.jsp?s1=494&s2=20532

When thread1 starts its execution, variable(gmx) is locked and thread 1 starts its execution.

Initial value of gvar is 0, it will be incremented by 1 for 200 times and the value of gvar will
be 200.

Then thread 2 starts its execution, the value of gvar will be updated to 8192. and again gvar will
be incremented by 1 for 200 times. Finally the value of gvar will be 8192 + 200 = 8292


Here there is no chance of context switching from thread-1 to thread-2. The variable will be safe
as far as above code is concern.

The context switching may occur if any blocking calls are added in the thread, eg: sleep,
read....etc
PR Attribute ID Marks New Marks
OS_6 4 1
OS_10 2 .5
(14)
//The following code prints an address of a variable. What type of address is this? virtual, physical or cache address
etc.? Justify your answer in short.

int main()
{
int i = 0;
printf("address of i : %u ", &i);
return 1;
} ?
Page 8 of 9
2/6/2009 http://10.203.161.13/OES/take1.jsp?s1=494&s2=20532





It will print the virtual Address.

When the program is converted in to executable file, all the symbols are resolved by the linkeer.
Any program have to be loaded in to main memory for execution. Loader is loads the program with
specified virtual address.

And it is not possible to see the address of physical memory or cache.
PR Attribute ID Marks New Marks
OS_4 3 3
Page 9 of 9
2/6/2009 http://10.203.161.13/OES/take1.jsp?s1=494&s2=20532

You might also like