process and thread answers
play

Process and thread answers 1. There is no exact answer, but from - PDF document

Linkping University 2011-03-28 Department of Computer and Information Science (IDA) Concurrent programming, Operating systems and Real-time operating systems (TDDI04) Process and thread answers 1. There is no exact answer, but from reasoning


  1. Linköping University 2011-03-28 Department of Computer and Information Science (IDA) Concurrent programming, Operating systems and Real-time operating systems (TDDI04) Process and thread answers 1. There is no exact answer, but from reasoning you should be able to come up with something very similar: a) mp3 player, X-window system, terminal, bash, top, clock, word processor b) user threads in mp3: mixer chart, rolling text, playback; user threads in word processor: writing, spell checking, animation; kernel threads: at least one per process, at most one per user thread. c) Discussed on lesson. d) Discussed on lesson. e) Discussed on lesson. 2. Efficient use of resources, Convenience, Standard programming interface 3. a) One possible sequence. Queue when not executing indicated in “tags”. IDLE BILL ADAM EVE <ready> thread_create(ADAM) <ready> thread_create(EVE) <ready> <ready> <ready> wait(!EVE) <ready> <ready> <ready> <waiting> eat(Apple) <ready> <ready> <waiting> wake(ADAM) <ready> <ready> <ready> invent(MAC) <ready> <ready> enjoy(OS X) <ready> forbid(Apple) <ready> launch(Vista) while (true) noop; 17

  2. TDDI04 2011-03-25 b) One possible sequence. Queue when not executing indicated in “tags”. IDLE BILL ADAM EVE <ready> thread_create(ADAM) <ready> <ready> <ready> wait(!EVE) <ready> thread_create(EVE) <waiting> <ready> forbid(Apple) <waiting> <ready> <ready> launch(Vista) <waiting> <ready> <ready> <waiting> eat(Apple) <ready> <waiting> wake(ADAM) <ready> enjoy(OS X) <ready> <ready> invent(MAC) while (true) noop; c) That you run thread create does not mean the thread starts immediately. the creator may even exit before the created thread start execution. You also see that the order of execution between threads may vary, even though the order of execution within one single thread is sequential. 4. a) A program during execution and all information about that executing program: id, memory, space, thread info, open files, execution time, owner, priority, etc. Executes in user mode. Have one address space protected by OS - no process can access the memory of another process. May share it’s execution environment among several user threads. Thread(s) keep track of executing stack and saved CPU context. b) A structure that keeps track of the execution of one code sequence, to be able to switch between different code sequences, obtaining time-sharing of the CPU - seemingly simultaneous execution of the code sequences. Need to save id, execution stack and saved CPU context of each sequence. c) They enable seemingly simultaneous execution of different code, making programs tasks that should go on in parallel easier to manage. They allow efficient use of both uniprocessors (other threads use the CPU when one must wait) and multiprocessors (need enough tasks to occupy every CPU). d) It will keep track of all things (and more details) listed in a). e) It can be ready, executing or waiting. In addition it can be new - just created - but then immediately transition to ready. And it can be terminated - scheduled for deletion by the next thread. The queues that exists are the ready queue and many waiting queues. 18

  3. TDDI04 2011-03-25 5. Require that you think twice! The answer here assume a) P processes can execute - one per real CPU-core. b) Only processes in RAM are ready. M processes can be held in memory, but if they are ready P of them will execute. The OS will keep the CPU busy when possible. Thus M - P processes can be ready. c) D I/O devices probably mean D wait queues - one per device. All M processes can be queued here. 6. a) See figure 4.10 on page 180 in Stallings book. In “Dinosaur” book, see section 2.7.2 - 2.7.3 on page 59 - 62. b) See figure 4.10 (a) on page 180 in Stallings book: thread/process management - > memory management -> disk management. When starting a program the OS must first create thread and process structures, then allocate memory for the program, and then load the program from disk. 7. a) The OS will in fact only execute as a response to an interrupt! Interrupts provide a means of requesting service from the OS - system Call. They provide the OS means to respond to events - not wait for them. b) Mainly for system-call and automatic thread switches. Also to handle virtual memory. And of course to handle other hardware-device events. 8. One possible solution: void* content[128]; /* every pos initiated to NULL in main */ struct lock mutex[128]; /* every lock initiated in main */ int array_add(void* data) { bool found = false; for (int i = 0; i < 128 && !found; ++i) { lock_acquire(&mutex[i]); if (content[i] == NULL) /* determine if index is free */ { content[i] = data; /* save data pointer in free index */ found = true; } lock_release(&mutex[i]); } return i; /* return index used */ } void* array_remove(int id) /* STUDY: Can any sequence go wrong here? */ { void* ret = content[id]; /* temporarily save the index’s pointer */ content[id] = NULL; /* mark the index as free */ return ret; /* return the pointer that was removed */ } 19

  4. TDDI04 2011-03-25 9. a) Processes operate in limited user mode and OS operate in unlimited kernel mode. This separation allow the OS to protect OS code from process modification, to prevent processes from stealing resources (CPU, memory, disk, etc.), and processes from interfering with each other. System calls is needed to allow processes (user mode) to request that the OS perform some action (kernel mode). Since it is the OS that perform the kernel-mode action it will still be in control. Allowed actions, or services, are predefined by the OS and names system calls. The system calls also give the application programmer a standardized interface to functionality. System call details needed to request the transition are normally hidden in a normal (user mode) function to make it easy to use. b) See a) and c). c) Steps to perform a system call bool create(char* filename) WHAT WHO MODE enter create(“kalle”) process user place parameter on process stack process user place CREATE label on process stack process user execute system call interrupt process user switch to kernel mode CPU user save CPU context to kernel thread stack CPU kernel switch to kernel mode thread execution stack CPU kernel jump to interrupt handler CPU kernel start system call handler OS kernel verify that the call is valid OS kernel determine service to execute ( CREATE ) OS kernel locate parameters and check that they are valid OS kernel execute service (create file “kalle”) OS kernel place result where the process will look for it later (in EAX ) OS kernel return from interrupt OS kernel switch to user mode CPU kernel restore saved CPU context from kernel stack CPU user (including instruction pointer and stack pointer) read return value (from EAX ) process user return from create(“kalle“) process user 20

  5. TDDI04 2011-03-25 10. Discussion including answers to assignment 15 a) - c). The following code is in question: /* sum: A pointer to the variable containing the final sum. start: A pointer to the first value in an array to sum. stop: A pointer which the summation should stop just before. */ void sum_array(int* sum, int* start, int* stop) { while (start < stop) { *sum = *sum + *start; ++start; } } /* Some code in the main function is omitted for brewity. */ int main() { int sum = 0; int array[512]; load_interesting_data_to_array(array, 512); /* Start four threads to sum different parts of the array. * * thread_create schedules (places in ready queue) a new thread * to execute the sum_array function. As this shecdule is just a * list insert it will return quickly, probably (but not always) * before the new thread even staret execution. */ for (int i = 0; i < 4; ++i) thread_create(sum_array, &sum, array + 128*i, array + 128*(i+1)); printf("The sum is: %d\n", sum); } In the code above we can identify several variables. In main : locally declared integer, address passed to sum_array , printed sum address to 512 integers, loaded with data, passed to sum_array array local loop variable i In sum_array : address to external integer sum start start address of array stop address just after the array 21

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend