0% found this document useful (0 votes)
6 views

Tutorial Lab 3 OS

Uploaded by

ayshamumu764
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Tutorial Lab 3 OS

Uploaded by

ayshamumu764
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Goal of PKCtxNew.

c
Implement a function to initialize the context for a new thread by checking
resource availability, setting up memory, and configuring the thread’s starting
point. The function will set essential elements like the instruction and stack
pointers for the new thread.

Steps for Implementation


• Memory Allocation: Begin by allocating memory for the new thread,
using the specified identifier and quota. This step ensures that sufficient
resources are set aside for the thread’s execution.

• Setting the Instruction Pointer (eip): Set the instruction pointer to


the entry address provided in the function arguments. This pointer will
direct the new thread to begin execution at the specified location.
• Configuring the Stack Pointer (esp): Assign the stack pointer to the
top of the designated stack memory for the new thread. Note that the
stack grows downward, so the initial pointer should point to the highest
address in the allocated stack space.
• Return the Thread ID: After successfully setting up the memory and
pointers, return the identifier of the newly created thread.

This implementation ensures that each new thread starts with a properly
configured memory space, instruction pointer, and stack, ready to execute from
the specified entry point.

1
Goal of tcb init
Initialize the Thread Control Block (TCB) for all NUM IDS threads by:
• Setting each thread’s state to TSTATE DEAD.
• Initializing two indices to NUM IDS (representing NULL).

Steps for Implementation


1. Define Local Variables
• Initialize Paging: Begin by initializing the paging system with the pro-
vided memory address. This step sets up the essential memory mappings
for managing thread control blocks.
• Iterate Over Each Thread Slot: Loop through each thread slot and
initialize its control block:
– For each slot, set the thread’s state to a default inactive status.
– Ensure both indices associated with each thread are set to a null
representation, which signifies that the thread is not linked to any
others.
– Use a helper function for initializing each thread slot individually,
ensuring a consistent setup across all slots.
• Outcome: After this initialization, each thread control block is set to
a dormant state until explicitly activated, maintaining an isolated and
predictable thread structure.

2
Goal of PTQueueInit.c
Implement functions to initialize and manage thread queues as doubly linked
lists based on indices. These queues should support operations like initialization,
insertion, removal, and retrieval.

Steps for Implementation


1. Queue Initialization
• Begin by setting up the necessary environment and structures for manag-
ing threads.
• Call an initialization routine to prepare all threads in the system.
• For each queue, use a function to ensure it is ready for thread operations.

2. Enqueue a Thread
• Obtain the current end of the queue.
• If the queue has existing elements:

– Link the current end to the new element by updating its next refer-
ence.
• If the queue is empty:
– Set the new element as the starting point of the queue.

• Update the new element’s previous reference and set it as the new end of
the queue.

3. Dequeue a Thread
• Identify the starting element of the queue.
• If the queue has no elements, return an indicator for an empty queue.
• If there are elements in the queue:

– Retrieve the element that follows the starting element.


– Update the queue’s starting reference to point to this next element.
– If there is a subsequent element, adjust its previous reference to in-
dicate it is the first.
– If no subsequent element exists, mark the queue as empty.

• Clear the connections of the removed element and return its identifier.

3
4. Remove a Specific Thread
• Retrieve the queue’s starting and ending elements as well as the elements
adjacent to the one being removed.
• If the element to remove is the starting element:

– Update the starting reference to the following element.


• If the element to remove is the ending element:
– Update the ending reference to the preceding element.

• If the element to remove is in the middle of the queue:


– Adjust the references of the neighboring elements to bypass the re-
moved element.
• Detach the removed element completely by clearing its connections.

4
Goal of PThread.c
Develop functions for initializing the threading system, creating new threads,
and managing thread execution by yielding control between threads in a ready
queue.

Steps for Implementation


1. Spawning a New Thread
• Create a new thread by allocating a context and associating it with a given
entry point and resource limit.
• Mark this new thread as prepared to run by setting it to a ready state.
• Add the newly prepared thread to the queue where ready threads wait for
execution.
• Return an identifier for the newly created thread.

2. Yielding to Another Thread


• Identify the currently active thread and add it back to the queue of threads
waiting to execute, marking it as ready.
• Retrieve the next available thread from the queue.
• If no other threads are ready, avoid unnecessary context switching and
maintain the current thread.
• For an available thread, set its state to running and update the system to
recognize it as the active thread.
• Transition control to the newly set active thread, swapping its context
with the previously active one.

5
Goal of TSyscallArg.c
Implement functions to manage system call arguments and return values within
a user context data structure. These functions facilitate communication between
the kernel and user processes, allowing system calls to access arguments and pass
results back to the calling process.

Steps for Implementation


1. Retrieving System Call Arguments
• Each function should retrieve a specific argument from the data structure
that stores the current process’s state.
• Begin by identifying the active process. For each function, access the
correct entry for that process within the data structure.

• For each argument, retrieve the corresponding register value associated


with that specific argument.

2. Setting System Call Error Number


• Define a function to assign an error code that will be passed back to the
calling process after the system call completes.
• This error code should be stored within the context of the current process,
ensuring that the calling process can access it when the system call returns.

3. Setting Return Values for System Calls


• Each function should set a particular return value within the current pro-
cess’s context, with up to five return values possible depending on the
system call requirements.

• For each function, identify the active process and update the correspond-
ing data entry to store the result that will be passed back to the user
process.
• These return values are stored in specific locations, allowing the calling
process to retrieve them after the system call concludes.

6
Goal of TSyscall.c
Implement system call functions that support user-space operations, specifically
focusing on spawning new processes and yielding control between processes.
Each function facilitates specific system call requests by managing resources
and interacting with process contexts.

Steps for Implementation


1. Spawning a New Process (sys spawn)
• Retrieve the necessary parameters to identify the executable type and
allocate resources for the new process.
• Based on the provided identifier, select a corresponding memory address
where the executable is preloaded. Use designated constants to map each
identifier to a specific process executable.
• If the identifier does not match any available executable, set an error code
for an invalid process ID and return a failure status.
• Attempt to create the new process using the identified memory address
and resource limit.

• If process creation fails, set an error code indicating memory limitations;


otherwise, mark the operation as successful and return the identifier for
the new process.

2. Yielding Control to Another Process (sys yield)


• Use a function to transfer control to the next process or thread in the
ready queue, allowing other processes to execute.
• Set a success error code to confirm that the yield operation completed
without errors.

7
Goal of TTrapHandler.c
Implement handlers to manage exceptions and interrupts that occur during
process execution. The exception handler function primarily addresses faults
such as page faults, while interrupt handler deals with system-generated inter-
rupts like timers or spurious interrupts, each directing control to appropriate
responses.

Steps for Implementation


1. Exception Handling (exception handler)
• Identify the type of exception by accessing specific information in the
current process’s state.
• For a memory-related fault, such as a page fault, delegate control to a
dedicated handler that manages memory allocation or permission checks.
• For any other type of exception, redirect to a general handler that outputs
diagnostic information and terminates the process if the fault cannot be
managed.
• This setup ensures that page faults receive specialized handling, while
other faults follow a general protocol.

2. Interrupt Handling (interrupt handler)


• Retrieve information indicating the type of interrupt, then check if it
matches specific cases, such as a timer or a spurious interrupt.
• For timer interrupts, route to a timer-specific handler to process timing-
related operations.
• For spurious interrupts, direct control to a handler that safely disregards
non-critical interrupts.

• For all other interrupts, apply a default handler that acknowledges the
interrupt and maintains system stability.
• This structure ensures that each interrupt type is directed to an appro-
priate handler, while other cases are managed consistently.

You might also like