Shortest Time Remaining Algorithm - process

When a scheduler algorithm is developed, many alternatives can be preferred. One of these alternatives is shortest time remaining.
I wonder what happens if these processes enter into the the system:
Arrival Burst
P1 0 3
P2 2 6
P3 4 4
P4 6 5
P5 7 2
0-3 = P1 0-3 = P1
3-4 = P2 3-4 = P2
4-8 = P3 4-8 = P3
8-10 = P5 8-10 = P5
10-15 = P2 10-15 = P4
15-20 = P4 15-20 = P2
Which one is correct execution order ? Left or right

Related

SQL Based Query Using Rank and Window Functions

The following is my dataset.
P A Rank B Rank C Rank
P1 1 1 2
P2 2 6 4
P3 3 4 1
P4 4 5 6
P5 5 2 5
P6 6 3 3
I have ranks of certain products basis three different parameters. There are n no of rows and this is just a sample. I want to create a column in this dataset which gives final rank as follows:
P Code Final Rank Ranking Logic
P1 1 Top A
P6 2 Top B (out of remaining 5 properties)
P4 3 Top C (out of remaining 4 properties)
P3 4 Top A (out of remaining 3 properties)
P2 5 Top B (out of remaining 2 properties)
P5 6 Top C (out of remaining 1 properties)
I need to understand how this can be implemented using SQL, or SQL Procedures.

Aggregate features row-wise in dataframe

i am trying to create features from sample that looks like this:
index
user
product
sub_product
status
0
u1
p1
sp1
NA
1
u1
p1
sp2
NA
2
u1
p1
sp3
CANCELED
3
u1
p1
sp4
AVAIL
4
u2
p3
sp2
AVAIL
5
u2
p3
sp3
CANCELED
6
u2
p3
sp7
NA
first, i created dummies:
pd.get_dummies(x, columns = ['product', 'sub_product', 'status']
but i also need to group by row, to have 1 row by user, what is the best way to do it?
If i'll just group it:
pd.get_dummies(x, columns = ['product', 'sub_product', 'status'].groupby('user').max()
user
product_p1
product_p3
sub_product_sp1
sub_product_sp2
sub_product_sp3
sub_product_sp4
sub_product_sp7
status_AVAIL
status_CANCELED
status_NA
u1
1
0
1
1
1
1
0
1
1
1
u2
0
1
0
1
1
0
1
1
1
1
i will loose information, fo ex. that for u1 sp3 status is canceled. So it's looks like i have to create dummies for every column combination?
Update: You are basically looking for pivot:
out = (df.astype(str)
.assign(value=1)
.pivot_table(index=['user'], columns=['product','sub_product','status'],
values='value', fill_value=0, aggfunc='max')
)
out.columns = ['_'.join(x) for x in out.columns]

Compute weighted average by group in SQLite

I have quarterly data on portfolio holdings, let's call the table holdings,
portfolio date security dollar_amount
p1 03/31/2001 security1 50
p1 03/31/2001 security2 100
p2 03/31/2001 security1 25
p2 03/31/2001 security2 50
p1 06/30/2001 security1 50
p1 06/30/2001 security2 100
p1 06/30/2001 security3 50
p2 06/30/2001 security1 25
p2 06/30/2001 security3 50
and data on monthly returns for each security, let's call it returns
security date return
security1 03/31/2001 1
security2 03/31/2001 -1
security3 03/31/2001 2
security1 04/30/2001 3
security2 04/30/2001 -1
security3 04/30/2001 2
security1 05/31/2001 1
security2 05/31/2001 2
security3 05/31/2001 -1
security1 06/30/2001 2
security2 06/30/2001 -1
security3 06/30/2001 3
security1 07/31/2001 2
security2 07/31/2001 -3
security3 07/31/2001 1
security1 08/30/2001 2
security2 08/30/2001 -3
security3 08/30/2001 2
For each portfolio, here p1 and p2, I want to compute monthly weighted average returns for each portfolio: SUM(dollar_amount * return) / SUM(dollar_amount). However, I want to take into account that there are quarterly changes in holdings, that is the weights should adjust every quarter.
Desired output:
portfolio date return
p1 03/31/2001 1/3*1 + 2/3*(-1) = -1/2
p2 03/31/2001 1/3*1 + 2/3*(-1) = -1/2
p1 04/30/2001 1/3*3 + 2/3*(-1) = 1/3
p2 04/30/2001 1/3*3 + 2/3*(-1) = 1/3
p3 05/31/2001 1/3*1 + 2/3*2 = 5/3
p4 05/31/2001 1/3*1 + 2/3*2 = 5/3
-- rebalancing, i.e. adjusting the weights according to holding data --
p1 06/30/2001 1/4*2 + 1/2*(-1) + 1/4*3 = 3/4
p2 06/30/2001 1/3*2 + 2/3*3 = 8/3
p1 07/31/2001 1/4*2 + 1/2*(-3) + 1/4*1 = -3/4
p2 07/31/2001 1/3*2 + 2/3*1 = 4/3
p3 08/30/2001 1/4*2 + 1/2*(-3) + 1/4*2 = -1/2
p4 08/30/2001 1/3*2 + 2/3*2 = 2
My final query will have to work with 53 quarters of holdings data and thus 159 months. The number of unique portfolios and securities are up to 13,000.
My question is whether there is a meaningful way to do it in a single SQLite query. If not, what do you think is the best way to do it?
The problems for me are
joining only the relevant (monthly) returns data for each quarter, e.g. returns from 03/31/2001, 04/30/2001, 05/31/2001 for the portfolio weights from 03/31/2001; otherwise the data would explode.
that the weighted average returns have to be computed per group, where a group is defined by quarter and portfolio.
The only way I can think of is to query the weighted average return per date and portfolio, such that I would have to loop through all of these combinations. I am aware that this is a computationally costly job, but I am looking for the fastest solution here.
Thanks for your help! I am using Python, sqlalchemy, sqlite3.
This is my take at it but Sqlite doesn't have much support for working with dates so it feels a little inefficient. I could not get it to work with your date format so I hade to change to 'yyyy-mm-dd' but maybe that is installation specific
SELECT portfolio, r.date, 1.0 * SUM(dollar_amount * return) / SUM(dollar_amount)
FROM returns r
JOIN holdings h ON h.security = r.security AND
strftime('%Y',h.date) = strftime('%Y',r.date) AND
CAST(strftime('%m',r.date) as int) BETWEEN cast(strftime('%m',h.date) as int) AND CAST(strftime('%m',h.date) as int) + 2
GROUP BY portfolio, r.date
ORDER BY r.date

Average wait time for pre-emptive priority scheduling

Given the following table for calculating and average waiting time for the processes for priority based preemptive scheduling.
Process Burst Time Priority
P1 10 3
P2 1 1
P3 2 4
P4 1 5
P5 5 2
The gantt chart would be as follows:
| P2 | P5 | P1 | P3 | P4 |
0 1 6 16 18 19
I have following questions:
1) Is the turn around time = 19 units?
2) How do i calculate average waiting time? Is there a formula?
3) What if few processes have the same priority?
I am new to OS. I have viewed some other similar questions but I did not get exactly how to do it.
Given the data,before you have to implement priority based preemptive scheduling, you should know the following facts :-
priorities are usually numeric over a range
high numbers may indicate low priority (system dependent)
associate a priority with each process, allocate the CPU to the process with the highest priority
any 2 processes with the same priority are handled FCFS
Proceeding with this much of knowledge,the required Gantt chart would be the same as what you've drawn:-
| P2 | P5 | P1 | P3 | P4 |
0 1 6 16 18 19
1) Is the turn around time = 19 units?
No, the turnaround time will be 16 + 1 + 18 + 19 + 6 = 60.
Average turnaround time = 60 / 5 = 12.
2) How do i calculate average waiting time? Is there a formula?
Average waiting time is defined as the sum of total time waited before starting of the processes divided by the total number of processes.
Here, average waiting time = (6 + 0 + 16 + 18 + 1) / 5 = 41 / 5 = 8.2.
3) What if few processes have the same priority?
If the few processes will have same priority then the scheduling would be handled using First-Come First-Serve (FCFS) as mentioned in the 4th point above. So, everywhere including Gantt chart, the process coming first will be scheduled first and the other similar-priority process would be scheduled late as it came arrived late.
I hope it is crystal clear from my steps and doesn't need any further explanation.

Priority Preemptive Scheduling

When using Priority Preemptive Scheduling, does a higher priority yield way to a process with a lower priority but with a shorter burst time?
For example, if I had:
Arrival Time Burst Time Priority
P1 0 5 3
P2 2 6 1
P3 3 3 2
Would the Gannt chart look like this?
| P1 | P2 | P3 | P1 |
0 2 8 11 16
Priority Scheduling always selects the process(es) with the highest priority currently ready to run. If there is more than one process having the currently highest priority, you need a second scheduling algorithm to choose among these processes. Non-preemptive Priority Scheduling only selects a new process to run if the running process finished its work or yields (voluntarily) to the scheduler.
Preemptive Priority Scheduling is the same algorithm but if a new process having a higher priority than the currently running process arrives, it gets selected immediately. The new process has not to wait until the currently running process finishes or yields.
In your sample, the Gantt chart for Preemptive Priority Scheduling and 3 being the highest and 1 the lowest priority would look like:
| P1 | P3 | P2 |
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
| p1 | p2 | p3 | p1 |
0....2....8....11...14
taking 1 as the highest priority.
|p1 |p2 |p3 |p1 |
0 2 8 11 14
because preemptive approach will preempt if the priority of the newly-arrived process is higher than the priority of currently running process..