processes and threads assignments le 1 3 5 7 8 9 10 11 13
play

Processes and threads assignments (LE: 1,3,5,7,8,9,10,11,13) 1. - PDF document

Linkping University 2011-01-30 Department of Computer and Information Science (IDA) Concurrent programming, Operating systems and Real-time operating systems (TDDI04) Processes and threads assignments (LE: 1,3,5,7,8,9,10,11,13) 1. Consider a


  1. Linköping University 2011-01-30 Department of Computer and Information Science (IDA) Concurrent programming, Operating systems and Real-time operating systems (TDDI04) Processes and threads assignments (LE: 1,3,5,7,8,9,10,11,13) 1. Consider a computer running an mp3 player , an X-window system , a terminal with bash running top , a clock , and a word-processor . The mp3-player have several things going on, playing music, updating a graphic mixer chart and showing rolling text with title information and playtime remaining. The word processor does spell checking while you write and shows an animated help icon. a) Identify several processes in this system. b) Identify several threads in this system. c) Draw a picture of how the operating system may represent the various processes and threads at a given time. d) How are the threads in your picture related to the processes? Which other process - thread relations may exist? e) What differs between kernel-threads, user-threads and processes? Which data does the OS store about them? 2. Which are the purposes and motivations of using operating systems? 3. A system starts an IDLE thread during boot, and then a thread BILL. The threads are implemented according to the following (we count one instruction per line). IDLE: BILL: ADAM: EVE: while (true) thread_create(ADAM); wait(!EVE); eat(Apple); noop; thread_create(EVE); enjoy(OS X); wake(ADAM); forbid(Apple); invent(MAC); launch(Vista); The operating system will place the new thread in an appropriate queue when it is created or waiting. The operating system manages thread-switches automatically at random times. Assume now that you are in control of the dice, and the thread queues. a) Show a sequence of events (executed instructions and thread switches) that finish all threads while fulfilling the following two conditions: MAC must be invented before OS X, and OS X must be enjoyed before Apple is forbidden. (The instruction wake(ADAM) will move ADAM to an appropriate queue.) b) Show a different sequence of events that break the conditions (MAC is invented after OS X, and Apple is forbidden before eaten). Show the queue contents after each instruction. c) What can you learn from the example? 4. Answer the following regarding processes and threads: a) Define a process. b) Define a thread. c) Motivate the use of processes and threads. d) What is the purpose of a process control block. e) List the possible states (queues) a thread/process can be in during it’s lifetime. 3

  2. TDDI04 2011-01-30 5. Assume a computer have D I/O devices to request data from, P processors to execute on and a main memory that can hold at most M processes simultaneous. a) How many processes can be in the running/executing state simultaneous? b) How many processes can be ready to run, at most? c) How many processes can be in the waiting state? 6. An operating system can be structured in several ways, and in several layers. a) What is the central idea behind a micro-kernel? b) Which are the main subsystems in an operating system, and how are they related (how does the subsystems use each other? Hint: think of which order the subsystems is needed when starting a program.) 7. Interrupts have a central role in operating systems. a) Explain why interrupts are so important. b) How does the operating system use interrupts? 8. When executing code from multiple threads on an uniprocessor the code are interleaved to make it appear each thread is executing simultaneous. This leads to problems when the code share data common to the threads . One thread may be interrupted at any time, and the shared data it is working on may be modified by another thread, and later the original thread continue unaware of the interrupt and modification of data. This may lead to errors. Find all such problems in the code given below. 10: /* Each value in this global array of pointers is set to NULL in main */ 11: void* content[128]; 12: 13: /* Add data to free slot in array. This is called from many threads . */ 14: int array_add(void* data) 15: { 16: for (i = 0; i < 128; ++i) 17: { 18: /* if free, use it */ 19: if (content[i] == NULL) /* determine if index is free */ 20: { 21: content[i] = data; /* save data pointer in free index */ 22: break; 23: } 24: } 25: return i; /* return index used */ 26: } 27: 28: /* Remove data from array. This is called from many threads . */ 29: void* array_remove(int id) 30: { 31: void* ret = content[id]; /* temporarily save the index’s pointer */ 32: content[id] = NULL; /* mark the index as free */ 33: return ret; /* return the pointer that was removed */ 34: } 35: 36: 4

  3. TDDI04 2011-01-30 9. Answer the following regarding system calls: a) Why are system calls needed? Try to think of the benefits on all levels. b) Who requests, and who executes system calls? Why? What is meant by dual-mode operation? What is different from a normal function call? c) How is a system call performed? Try to explain all technical details. 10.The following code uses several processors to sum the values in an array. Using several processors can lead to the same problems as running interleaved threads on one processor. Explain how the calculation may become wrong. 10: /* sum: A pointer to the variable containing the final sum. 11: start: A pointer to the first value in an array to sum. 12: stop: A pointer which the summation should stop just before. 13: */ 14: void sum_array(int* sum, int* start, int* stop) 15: { 16: while (start < stop) 17: { 18: *sum = *sum + *start; 19: ++start; 20: } 21: } 22: 23: /* Some code in the main function is omitted for brewity. */ 24: int main() 25: { 26: int sum = 0; 27: int array[512]; 28: 29: /* Code to initiate the array with data is placed here. */ 30: 31: /* Start four threads to sum different parts of the array. 32: * 33: * thread_create schedules (places in ready queue) a new thread 34: * to execute the sum_array function. As this shecdule is just a 35: * list insert it will return quickly, probably (but not always) 36: * before the new thread even staret execution. 37: */ 38: thread_create(sum_array, &sum, array + 128*0, array + 128*1); 39: thread_create(sum_array, &sum, array + 128*1, array + 128*2); 40: thread_create(sum_array, &sum, array + 128*2, array + 128*3); 41: thread_create(sum_array, &sum, array + 128*3, array + 128*4); 42: } 11.An antivirus process starts a new thread to scan a zip file (compressed archive containing many files) for viruses. The new thread will first open the zip to verify it’s integrity. If the integrity is fine the thread will continue to scan each file within the archive. If the integrity (archive format is unrecognized or faulty) the thread will inform the main process thread of this and then quit. The main process thread will then scan the archive as a regular file, without unpacking the data. In this case the process need confirmation from the thread to determine it’s action. The code below implement a solution that seems plausible. What errors may occur? Which different order of events exists? 5

  4. TDDI04 2011-01-30 10: /* Open the file as a zip file and scan each file inside for viruses. 11: * Set the output parameter bad to true if the zip is invalid. */ 12: void unpack_and_scan(const char* filename, bool* bad) 13: { 14: zip = open_zip(filename); 15: 16: if (open_failed(zip)) 17: { 18: *bad = true; 19: return; /* exit thread */ 20: } 21: 22: /* Iterate each file contained in the zip and scan it. */ 23: for (file = zip_first(zip); file != NULL; file = zip_next(zip)) 24: { 25: scan_regular_file(file); 26: } 27: } 28: 29: /* Scan all files given as argument to main. 30: * > indicate lines important for the assignemnt. */ 31: int main(int argc, char* argv[]) 32: { 33: int i; 34: for (i = 0; i < argc; ++argc) 35: { 36: switch ( file_extension(argv[i]) ) 37: { 38: case ZIP: 39: { 40: > bool bad_file = false; 41: > 42: > /* thread_create schedules a new thread to execute 43: > * unpack_and_scan. As schedule is just a list insert it 44: > * will return quickly, probably before the new thread even 45: > * stared execution. */ 46: > thread_create(unpack_and_scan, argv[i], &bad_file); 47: > 48: > if ( ! bad_file ) 49: > { 50: > break; /* break switch and continue for loop. */ 51: > } 52: > /* Fall through to COM,EXE,DLL action since break is missing.*/ 53: } 54: case COM,EXE,DLL: 55: { 56: scan_regular_file(filename); 57: break; /* break switch and continue for loop. */ 58: } 59: case TXT,BMP,PNG,JPG: 60: { 61: break; /* Considered safe files. Do nothing. */ 62: } 63: } /* end switch */ 64: } /* end for */ 65: 66: return 0; 67: } 6

Recommend


More recommend