OS Quiz #5

¡Supera tus tareas y exámenes ahora con Quizwiz!

Thread Attributes

-By default the thread is created with certain attributes -Some of these attributes can be changed by the programmers via the thread attribute object -Pthread_attr_init and pthread_attr_destroy are used to initialize/destroy the thread attribute object

pthread_cancel

-Cancel execution of a thread -The pthread_cancel() function shall request that thread be canceled. -Thread's cancelation depends upon 1) The cancelability state of the thread 2) If the thread is in a library method that is a cancellation point.

How does Scheduler view the threads

-Creating a new thread it gets a new identifier in the kernel --> thread id -the PID of the thread is the process ID of the main process that was the first thread in the process -From scheduler's perspective all processes are viewed as threads --> so there is no diff in how the scheduler handles 2 processes with one thread each AND 1 process with two threads

Joining threads and detaching threads

-Joining is a way to accomplish synchronization between threads -The pthread_join() subroutine blocks the calling thread until the specified threadid thread terminates. -The programmer is able to obtain the target thread's termination return status if it was specified in the target thread's call to pthread_exit(). -A joining thread can match one pthread_join() call. It is a logical error to attempt multiple joins on the same thread. -When a thread is created, one of its attributes defines whether it is joinable or detached. Only threads that are created as joinable can be joined. If a thread is created as detached, it can never be joined. -To explicitly create a thread as joinable or detached, the attr argument in the pthread_create() routine is used. The typical 4 step process is: -Declare a pthread attribute variable of the pthread_attr_t data type Initialize the attribute variable with pthread_attr_init() -Set the attribute detached status with pthread_attr_setdetachstate() When done, free library resources used by the attribute with pthread_attr_destroy() -The pthread_detach() routine can be used to explicitly detach a thread even though it was created as joinable. There is no converse routine.

PTHREAD_CANCEL_ENABLE & PTHREAD_CANCEL_DISABLEP & THREAD_CANCEL_DEFERRED & PTHREAD_CANCEL_ASYNCHRONOUS

-PTREAD_CANCEL_ENABLE The thread is cancelable. This is the default cancelability state in all new threads, including the initial thread. The thread's cancelability type determines when a cancelable thread will respond to a cancellation request. -PTHREAD_CANCEL_DISABLE The thread is not cancelable. If a cancellation request is received, it is blocked until cancelability is enabled. -PTHREAD_CANCEL_DEFERRED A cancellation request is deferred until the thread next calls a function that is a cancellation point. This is the default cancelability type in all new threads, including the initial thread. -PTHREAD_CANCEL_ASYNCHRONOUS The thread can be canceled at any time. (Typically, it will be canceled immediately upon receiving a cancellation request, but the system doesn't guarantee this.)

Single threaded process model

-Representation of a process includes its process control block and user address space, as well as user and kernel stacks to manage the call/return behavior of the execution of the process -While process is running git controls the processor registers -Contents of registers are saved when the process isn't running

Thread Cleanup Handlers

-The pthread_cleanup_pop() function shall remove the routine at the top of the calling thread's cancellation cleanup stack and optionally invoke it (if execute is non-zero). -The pthread_cleanup_push() function shall push the specified cancella- tion cleanup handler routine onto the calling thread's cancellation cleanup stack. The cancellation cleanup handler shall be popped from the cancellation cleanup stack and invoked with the argument arg when: The thread exits (that is, calls pthread_exit()). The thread acts upon a cancellation request. The thread calls pthread_cleanup_pop() with a non-zero execute argument.

Thread Detached State

-Thread detached state specifies whether some other thread will want to know the when the thread being created terminates. -if a thread's state is not detached, it is called a PTHREAD_CREATE_JOINABLE thread. That means a thread can use the pthread join api to join this thread and know when it terminates -When a thread is created detached (PTHREAD_CREATE_DETACHED), its thread ID and other resources can be reused as soon as the thread terminates. -The default is PTHREAD_CREATE_JOINABLE. ** Note** - pthread join call will fail if invoked on a detached thread. **Note** - it is possible to change the state of a thread from joinable to detach once it has been created. But if it was created as detached, you cannot make it joinable.

Thread Creation

-inintally main() program comprises a single, default thread; all other threads must be explicitly created by the programmer -pthread_create creates a new thread and makes it executable--> its arguments = thread, attr, start_routine, arg -max number of thread that may be created by a process is implementation dependent -once created, threads are peer -- no hierarchy

Thread Cancelability State

1)pthread_setcancelstate(int state, int *oldstate) - sets the cancelability state of the calling thread to the value given in state. The previous cancelability state of the thread is returned in the buffer pointed to by oldstate. The state argument must have one of the following values: PTHREAD_CANCEL_ENABLE or PTHREAD_CANCEL_DISABLE 2) pthread_setcanceltype(int type, int *oldtype). sets the cancelability type of the calling thread to the value given in type. The previous cancelability type of the thread is returned in the buffer pointed to by oldtype. The type argument must have one of the following values: PTHREAD_CANCEL_DEFERRED or PTHREAD_CANCEL_ASYNCHRONOUS

pthread_exit

Doesn't close files; any files opened inside the thread will remain open after the thread is terminated -There is a definite problem if main() finishes before the threads it spawned if you don't call pthread_exit() explicitly. All of the threads it created will terminate because main() is done and no longer exists to support the threads. -By having main() explicitly call pthread_exit() as the last thing it does, main() will block and be kept alive to support the threads it created until they are done

Memory Sections for the new thread

Each thread has its own stack, and threads in the same process share the same heap -Thread is created in similar way to how fork is implemented but the difference is that a new flag called CLONE_VM is passed --> tells the kernel to not create a new address space -at this point, if you issued get pid call in the main process (the original thread) and the newly created thread the answer will be the same -the new thread is created before the thread library issues the system call to create the thread, it mallocs a memory area from the heap and use that to set up the stack size of the newly created thread

Motivation of Threads

If two processes have a shared address space, so that the data is available to both of them and the changes made by one process can be seen by the other -Two threads share everything in address space except their stack

Cancellation points

POSIX.1 specifies that certain functions must, and certain other functions may, be cancellation points. If a thread is cancelable, its cancelability type is deferred, and a cancellation request is pending for the thread, then the thread is canceled when it calls a function that is a cancellation point.

Terminating threads

Several ways in which a thread may be terminated -thread returns normally from its starting routine -thread makes a call to pthread_exit -thread is cancelled by another thread via pthread_cancel -entire process is terminated due to making a call to either the exec() or exit() -if main() finished first, without calling pthread_exit explicitly itself

Threads Lifecycle

Similar to lifecycle of a process -Creation: two possible states- joinable and detached -Execution: threads have their own stacks -Termination: use pthread_cancel to terminate; a thread can terminate itself by returning from thread function or calling pthread_exit -Joining: a thread can "join" another thread of the same program to identify the state of the other thread -Return value: the "join" operation returns the last known return value of the thread

Multithreaded process model

Single process control block and user address space associated with eh process, but now there are separate stacks for each thread, a well as a separate control block for each thread containing register values, priority, and other thread related state info


Conjuntos de estudio relacionados

CNO Practice Standard: Confidentiality and Privacy

View Set

Teaching and Learning/Patient Education

View Set

Ethos/Pathos/Logos in Patrick Henry's Speech to the Virginia Convention

View Set

Writing and Graphing Equations in Two Variables

View Set

NCLEX Alzheimer's, Dementia, and Delirium

View Set

Personal Finance Test 39 Multiple Choice

View Set