Академический Документы
Профессиональный Документы
Культура Документы
EECE 432
Quiz 1 Solution Oct 25, 2017
Why are those messages in the browser message passing API named cookies? Solution: the
term was coined by Lou Mntulli (programmer) and he exported it from the Unix magic
cookie idiom. A magic cookie is a piece of data kept at the receiver and not used by the
receiver except for sending it back to the sender. It is sender state information kept at the
receiver side. Magic cookies were first used to tell where in a file is the stream reader. They
are kept by the user code as cursors and made sense only at the file system APIs.
What is the total number of processes per one invocation of the program? Solution: 2
processes
What is the total number of threads ran by one invocations of the program? Solution: six
threads. main thread + 4 threads + one thread for the forked program.
What is the maximum possible number of times message secret is gets printed? Solution: at
most once per child thread. So 4 times at most.
symbols pointing to the code segment. Solution: main, try, fail are function symbols
with addresses in the code space.
statements that result in context switching. Solution: fopen, fclose, fprintf, cout, cerr,
create thread are all functions that result in context switching.
symbols that point to the data segment. Solution: msg and err point to literal strings
in the data segment.
symbols that point to a stack. Solution: str in function fail. arg and fptr in function
try. argc and argv are in the stack of main.
List a symbol in the program that points to the heap. Solution: word in try is in the stack,
once it is initialized it will holds an address that is in the heap.
1
const char * msg = " Welcome to thread " ; int
const char * err = " Error : failure to " ; main (unsigned i n t argc , char ** argv ) {
i n t pid = fork () ;
void fail ( const char * str ) { i f ( pid == 0) {
cerr << err << str << endl ; cout << " In child . " ;
exit ( -1) ; FILE * fptr = fopen ( argv [1] , " w " ) ;
} fprintf ( fptr , " abracadabra " ) ;
fclose ( fptr ) ;
void * try ( void * arg ) { } e l s e i f ( pid > 0) {
cout << msg << endl ; cout << " In parent . " ;
FILE * fptr = fopen (( char *) arg , " r " ) ; f o r ( i n t i = 0; i < 4; i ++) {
i f ( fptr != NULL ) { pthread_t th ;
char * word = new char [128]; pthr ead_crea te (& th , NULL , try , ( void
fread ( word , 1 , 128 , fptr ) ; *) argv [1]) ;
fclose ( fptr ) ; }
cout << " secret is : " << word << endl ; } else {
delete [] word ; fail ( " spawn child " ) ;
} }
return NULL ; return 0;
} }
Which process is likely doing more computationally intensive tasks? Solution: P4 spends
more time in the CPU. So it is likely doing more computations.
4 2
Which of these processes is likely most interactive? Solution: P1 has a 10
ratio, P2 has a 6
ratio, and P3 has a 35 ratio. So P3 seems more interactive than the rest.
Present a plausible scenario of how the short term schedule might run processes P1 , P2 , P3 , and
P4 on the cores. Assume all processes are ready at the beginning, assume zero time context
switching and zero delay input/output waiting.
Compute the average waiting time (ratio of time processes spent in the ready queue
versus the number of times they entered the ready queue).
Compute the total idle time for both cores (cycles where a core is not running a task).
Are the cores efficiently utilized in your scenario?
2
thread waits on the condition variable. Otherwise, it removes a task from taskQ using the deq
interface. It executes the task and iterates again.
Consider the following dispatcher worker thread implementation.
Solution: The taskQ is not guarded and that may cause race conditions: enq and deq called
simultaneously.
Solution: With more than one dispatcher, the blocking listen acts like a producer. If it does
not guard the tasks it produces, we might have a race condition as well. To secure that, we might
add a lock for the blocking listen.
3
barrier t e a m R e a d y B a r r i e r [4]; player ( teamIndex ) {
barrier a l l T e a m R e a d y B a r r i e r ; t e a m R e a d y B a r r i er [ teamIndex ]. signal () ;
winner = -1;
whistle . wait ( dummy ) ;
i n t apples = 100; dummy . release () ;
mutex applesLock ; // lock needed to to protect < check
applesLock . acquire () ;
i n t teamAapples [4] = {0 ,0 ,0 ,0}; while ( apples > 0) {
mutex tea mApplesL ock [4]; // release needed : fairness to other
applesLock . release () ; // players
mutex dummy ; // syntactic for whistle // no need for a condition variable
cond_var whistle ; // here since we do not desire the
// suspension ! We are in a competition
team ( teamIndex ) { // and there is no producer ! The apples
// needs three signals to open // are there .
t e a m R e a d y B a r r i er [ teamIndex ]. wait (3) ; // Had the apples been produced while
a l l T e a m R e a d y B a r r i e r . signal () ; // the game is on , we would need a
} // condition variable .
applesLock . acquire () ;
judge () { // applesLock gets released either in
// needs 4 signals to open // the next iteration or after the
a l l T e a m R e a d y B a r r i e r . wait (4) ; // loop exits .
whistle . broadcast () ; // let the game start i f ( apples > 0) {
while ( winner <0) { // judge is very attentive teamA pplesLo ck [ teamIndex ]. acquire () ;
applesLock . acquire () ; // :) always spinning // we need both locks to ensure
i f ( apples > 0) { // the integrity of the count .
} e l s e { // make sure things are correct . apples - -; // apples >= 0 always
assert ( apples == 0) ; teamAapples [ teamIndex ]++;
assert ( sum ( teamApples ,4) == 100) ; teamA pplesLo ck [ teamIndex ]. release () ;
winner = maxindex ( teamApplesLock ,4) ; } e l s e ; { // could wake up judge here
} } // to avoid spinning .
applesLock . release () ; }
} applesLock . release () ;
} }
A. Name the inter process communication mechanism involved in the following commands.
B. Give an example of a forced exit of a process. Solution: segmentation faults cause forced
exits. signal kill respected by the process can be a graceful exit in case the process implements
a signal handler for it. Yet, that is an acceptable answer.
4
D. Which of the following is part of an operating systems kernel.
Compiler Assembler Scheduler Linker Browser
Editor Device driver Memory manager File system Shell
Solution: Scheduler, Device driver, memory manager, file system.
E. A process spawns two threads. What parts of the process memory do the spawned threads
share? Solution: They share the whole address space. Each thread treats its stack space as
a stack, yet it can access the space of the stack of other threads as regular memory.