I have a linked list containing n processes and these processes are sorted in an decreasing order of priority to run. So , the 1st process i.e. 1st node of linked list has the maximum priority, then the 2nd node and so on.
At one time instance I can only run 8 processes. What I want to do is that I want 6 highest priority and 2 lowest priority to run at one time instant.
What I had done is that I rearranged the linked list where the first six nodes will be having highest priority and the next two nodes with lowest priority. Then further 6 nodes of high priority and 2 low priority and repeating this until all nodes are covered. But this does not do what I want i.e. if any of the 6 highest priority process ends then another high priority process should take it's place and if a low priority process ends then a low priority may take its place.
How can I implement this? (also this is my first question so if there are any problems in the way I had put my question please point it out)
I can solve this problem with 2 methods.
Anyhow use this list as deque. Deque is nothing but a data
structure like queue where we can push and pop elements from both top as well
as bottom of queue. Now we will maintain 2 counters, High-counter
and Low-counter. High-counter will keep track of 6 High priority
processes will Low-counter will keep track of 2 Low priority
processes. So High-counter will have max value 6 while Low-counter
will have 2. At start, initialize both counters with zero.
Now while High-counter < 6 , pop top element from dequeue and
increment the High-counter. Again while Low-counter < 2 , pop bottom
element from dequeue and increment the Low-counter. If any High
priority process completes, we will decrement the High-counter and
similar for low priority process. Now since, Counters are less than
their respective maximum values, we will use a while loop and repeat
previous two while loops until Deque is empty. Also note that here popping process means running them.
Use the similar approach but this time with semaphores in place of counters.
I am not claiming, my solution is 100% correct but still it's close to the answer. Also tag the question with operating system so as to attract better answers.
Related
I'm implementing a GAS Program, but I have a doubt on how I can schedule only one vertex to the next round.
What I'm trying to do involves calculating the probability of going to the next vertex and going to the one with the highest probability. So, assuming I calculate the probability and update the probability value inside each VS,
in the Scatter phase, how can I check all possible next vertex values and schedule only the one with the highest value?
In other words, I need my frontier at t + 1 to be only one vertex. How can I do this?
I got it working by scheduling all vertices in the Scatter phase, and then, in the nextRound method, iterating through the frontier and then selecting the vertex with (in my case) the highest probability. Then I reset the frontier setting only the selected vertex using the resetFrontier method.
I am asked to write a program,an abstraction of runtime scheduling algorithms in C.The one I'm having problem with is "priority scheduling".It takes runtimes from the user like the other algorithms,except it also takes priorities of these processes.What troubles me is that I can't relate priorities with runtimes. How am I supposed to make a relationship between the priority and the runtime?According to algorithm,it runs the one with highest priority first.I just need to know how to make this connection,thanks.
EDIT :-
Store the priorities in an array and sort out the array as per decreasing priorities and map each priority with it's timing! Then,simply display the sorted array from starting and it's mapping with the process-time.
If the process are added at run-time, the you need to adopt greedy-algorithm to solve that problem. Select that process with highest priority in increasing order of incoming time.
Check this to learn more Interval Scheduling Algorithm
BTW,for implementing the real-scheduling algorithm :-
If the first process is currently running and then there comes a new process with the same or lesser priority level, then you should add just the new process to the FIFO data-structure(queue) so that it gets executed at the next(for equal/lesser priority).
If the first process is currently running, and a new process with a higher priority arrives and requests memory, you must pass the current process' upcoming instruction to the stack and execute the higher priority process and then return interrupt to execute the upcoming instruction!
I hope you are getting confused with the DATA-STRUCTURES to implement in a peculiar manner. Also,there is significant use of priority(higher)!
For an assignment I need to implement Dijkstra's algorithm using a priority queue implemented as a min heap. I already have an implementation in which I use a distance table and an unordered array of the unprocessed vertexes. (As described towards the bottom of the page here).
An example input file is given below, where the first line is the number of vertexes and each line after is: source, destination, weight.
3
1 2 3
3 2 1
1 3 1
My initial thought was to treat each edge in the graph as a node in the heap, but that isn't right because one of the test files has 15677372 edges and I can't even make an array that big without an immediate segfault. After implementing the method with the array of unprocessed vertexes it seems that I need to somehow replace that array with the heap, but I'm not sure how to go about that.
Could anyone point me in the right direction?
Typically, in Dijkstra's algorithm, your priority queue will hold the nodes in the graph and the best estimated distance to that node so far. The standard technique is to enqueue all nodes in the graph into the priority queue initially at distance ∞, then to use the queue's decrease-key operation to lower them as needed.
This is often completely infeasible from a memory standpoint, so an alternate interpretation is to keep the queue empty initially and then seed it with the start node at distance 0. Every time you process a node, you then update the distance estimate for each of its adjacent nodes. You do this as follows:
If the node is already in the priority queue, you can use decrease-key to lower the node's distance.
If the node is not already in the priority queue, then you insert it at the required priority.
This keeps the priority queue small for graphs that aren't absurdly dense.
Hope this helps!
How are binary trees implemented in C/MPI ? And in particular, how can I gather on a processor the missing parts of the tree?
Say I want to divide a rectangle in N sub-rectangle, N being the total number of processes.
I start with a division in two sub-rectangles (along a direction and at a position that are given by a certain rule, never mind here). The left rectangle is assigned to processes [0,mid_process] and the right sub-rectangle is assigned to the other processes.
Then I do this recursively for inside these two sub-rectangles until I have, eventually, only one process left in the current sub-rectangle.
By doing this, each process is going to have a "local" part of the tree, which consists of the path, from itself to the root node.
However, later one, once the tree is built, I want each process (i.e. each final sub-rectangle) to identify which are its neighboring processes.
Assuming I have the whole tree locally, this can be done by traversing the tree, and for each leaf, look at its spatial extent and compare its boundary with the ones of the sub-rectangles we're looking the neighbors for.
This thing is I'm not sure how I send and receive the missing parts of the tree.
The figure here shows an example of a decomposition among 11 processes, and its associated binary tree.
Let's say I'm process 2. Locally, the tree I have in memory consists in the following nodes : [root], [0-4], [2-4], [2], where the numbers inside [] are the ranks of the processes. The neighbors of the process [2] are [0],1, [3],[4], [5], [8].
Problem: There are N people and S slots. Each person has a list of slots in which he is busy.
We have to find an algo to find a slot in which all of them are free.
I already know an algo whose complexity is O(NS). Need a better algo.
You are free to maintain different data-structures dynamically (they will be updated whenever a meeting is scheduled) which can be used to finally find a free slot.
Keep a slotcounter for each slot. for each persons busy slot add one to that slots slotcounter; for all peoples busy slots.
Any slotcounter which is still zero after accounting for all peoples busy slots is a a counter for a slot in which all people are free. Probably an O(k) algorithm.
Instead of counting you could set up a bitmask/bitset where person N's bitmask has bit S set in all positions where they are busy. A bitwise OR of all the peoples bitmasks will have zero bits corresponding to all the free slots.
UPDATE:
The way you state the problem, you don't have to track the people just keep an array of slot occupancy indicators. Initially all are marked free; as you go through each persons busy slots mark the appropriate occupancy indicator as busy. When you are done, any of the arrays indicators that are still free is your answer.
Edit:
Algo will work assuming we have a sorted list of slots for each person
Now you have N-lists of size S (maximum) each. Merge sort this list: http://en.wikipedia.org/wiki/Merge_algorithm
Typical merge sort (using heap) will result in complexity NlogK, where N is the total # of elemets in all 'K' lists. However, if your version, each element in the N-lists are bounded to be between 0 and s-1. Therefore, the complexity is also bounded by S*log(N). Iterating through the N-lists with a heap size of maximum 'N', bounded by S-iterations. Makes sense?
So, overall complexity = N*log(S) + S*log(N)
This is assuming the original list is sorted, else complexity goes up to N(SlogS)
Generate a bitmask of size S with the bits set for when S is busy. Bitwise OR all bitmasks together, then extract the bit that isn't set.
Bearing in mind that there might well not be a solution, the Hungarian Algorithm will provide the closest answer