# FCS 11 Oct

- **Course:** 5102 - Foundations of Comp Sys
- **Recorded:** 2026-10-11 00:00
- **Transcript:** 1947 segments, 16176 words
- **Made by:** deepdml/faster-whisper-large-v3-turbo-ct2

---
Hi, everyone. Very good afternoon. Can one of you confirm? Am I audible? Am I clear? Great. I could see a couple of thumbs up. Thank you so much, guys. Let's wait for another few minutes and then let us get started. I can still see participants are joining. It's worth giving them another one minute. Just check. Great. cool it's time to get started i think we have waited enough for participants to join and it's time for us to get started okay cool so now let me share my screen just give me a minute Okay, hope you are able to see my screen. Can you confirm, please? I just shared the presentation. Okay, great. Thank you so much. So, yeah, I know there is a small piece of activity that is pending in process and system call. I thought let us stick it up in our lab next week. We will continue with that. I thought it's better to get on to the topic, theory topic in the theory class itself. So now we were trying to understand the communication

between application operating system and hardware. So in that journey, we understood that the applications use libraries and binaries to create system calls to communicate with kernel of the operating system. And in turn, kernel understands those system calls and get things done from the resources that are available in the system. So in this, we also understood that these system calls will create processes. And these processes shall have its own identity in terms of process ID. And the communication is getting established between the processes as well. Now, imagine that you are part of an engineering team supporting an e-commerce platform during a major flash sale. Like how now it's going in India, the great Indian scale, something like that, from the e-commerce sites. All of them are running the sale. So thousands of customers like me are browsing products, checking inventory, placing orders, and making payments as well.

At the same time, the application is generating reports and processing ground jobs as well. Some customers receive responses in milliseconds. Others experience delay. CPU utilization is high. Our task today is to understand one important part of this problem. Now, how does the operating system decide which work gets equal to time? Before we discuss, I want you to think like a production engineer. So if you are going to think like a production engineer, the first thing that comes to your mind or what you see is revenue loss. in case if something goes wrong people will not be able to browse people will not be able to purchase people will not be able to pay hence there would be a loss in revenue basically for this type of traffic and this type of revenue only the organization was looking for you cannot miss this opportunity to earn revenue in this period so keeping this in mind and having the background that we're also part of the development team

which developed this application. Now think from the shoes of a production, what could be the possible reasons? For the time being, I'm just going to stop sharing. I'm just going to focus on the chat. and what I'm going to put across Anshu Priya is telling that my sound is low just wanted to confirm is it so? Is everyone experiencing low volume? Okay, cool so what I'm going to do right now is I'm just going to put this scenario into the chat thank you very much guys for immediate response. This is the scenario that I was discussing. So what could be the reason? And before even getting, how does the operating system decide which work gets the CPU time? Before that, what could be the reason? So whatever you think, just go ahead and type in the chat. I'll be looking at the chat at this point of time to see what are the responses that I'm receiving. Just put it in the public chat. Scheduling. No, I don't want to know how the operating system

decide which work gets CPU time. That's not the question. That's anyhow the final question. But before that, if you are a production engineer, what do you think would have caused this delay so the actual question is what could have caused this delay cpu scheduling okay that is a solution but what is the problem so i'm not sharing any screen i'm just following the chat at this point of time chat i believe All of you have access to memory issue, insufficient resources, resource allocation, queue formation, a lot of waiting threads, pending threads, utilization of CPU, perfect, CPU throttling, load balancing issue, possible, probably one of the in the cluster is not receiving any traffic demand is exceeding the resources available to meet the requirement, perfect concurrent requests at same time because of high traffic I agree high context switching overhead, possible CPU priority that's it might be the request

might be in that queue they'll be waiting obviously if it is not getting the processor time then it will be waiting high CPU utilization cost processors to compete for the CPU time that is factual request cancelling request cancelling I'm not sure Krishna can you just elaborate a bit another one more line deciding how to allocate limited CPU time perfect unequal response times and high CPU utilization we need to collect the data then analyze the cause as there can be multiple reasons perfect I'm not sure what is that thread managing thread pool starvation priority task Yes, all are good. Perfectly fine. Now let me go ahead and share my screen, and then let's proceed ahead. So now, as you all said, I can probably make a concise list. Too many requests arriving simultaneously than expected would be the reason. Insufficient CPU capacity, possible. Database or network delays because of the high traffic, pretty much possible. Excessive threads.

What is this thread? And I've seen multiple responses saying that thread throttling, thread scheduling. So what is this thread? That's where we are heading towards. A long-running computation, pretty much possible. If the compute engine is running for a pretty long time, definitely there would be some lag. Tasks waiting in the queue. Inefficient scheduling or synchronization. All these are possible process. And I believe all you have responded will come into one of these. All these are plausible. However, we must distinguish CPU scheduling delay from other sources of latency. A thread can be delayed because it is waiting for a database response. Because it is waiting in an application queue, or because it is ready to run but has not received time. to time. All is possible. So the incoming request comes and received as application queues. The application queues led to worker threads and worker threads got into runnable threads.

The moment it becomes runnable threads, it will compete for CPU and finally once it gets the CPU resource, it will execute. Once it executes, the response will go back. Let us assume that suppose four tasks are ready to run. Let's take a very simple example. Four tasks are ready to run, but only one CPU core is available. All four cannot execute simultaneously on that core. So somebody must decide which task runs first, how long it runs, and when another task gets set up. These three things someone has to decide. That decision-making process is called as CPU scheduling. Today, we will study four classical algorithms, calculate their performance, and then observe real threats on Linux. If you were designing the system, not the application, the system, would you optimize for finishing all work quickly, responding to user quickly, or treating every task fairly? What would you do if you were designing the system?

One is like optimize for finishing all work quickly, response to all users quickly, or treating every task fairly. Actually, the response is, it depends. There is no straight yes or no answer to that. It depends. What it depends on? It depends on the workload and service objectives. Now, by the end of this class, you should be able to explain the difference between your process and the thread. Very important. You should be in a position to calculate some scheduling metrics. Probably get an idea about how to construct a Gantt chart, identify starvation, explain preemption, and recommend an approach for a realistic workflow. That is our objective. So this is the problem we saw. This is the outcome of this session. Explained, some calculations, some comparisons, couple of observations, and finally in a position to recommend. Now, the first thing what we need to discern, whenever the program code or the application runs,

it generates processes. We have seen enough of examples. and these processes create threads and the threads get scheduled and according to the schedule, CPU provides resources to the scheduler. So threads get opportunity to process and then response back to processes and the process will give a response back to the application and application will display the end user on the screen. Sometimes due to delay in this communication chain, we may be expecting the user to wait for more than a minute or so, which probably may not be the case today. Nobody wants to wait for a minute or two, especially when it comes to the critical information about availability and reliability. So this is how it works. Now, abstraction is real systems also include runtimes, kernel scheduling classes, affinity, and resource limits. Now, process versus thread. Recall our previous discussion of processes and system calls. A process is running instance of a program

with its own execution context and resources managed by the operating system if it typically has a virtual address space code data keep one or more tags open files credentials and scheduling related information all this is typically is available with the process. Now, the process provides the environment in which threads execute. So in our previous slide, we saw process creates the thread, and thread gets run with a response back to that. It doesn't mean so that we have to reroute like this. There are other ways to get rerouted and get the status. So now, if I start two independent copies of a program, we have seen that, do I necessarily get two threads in one process, actually the answer is no. Within a process, two threads, it won't open, even though you are opening the same program, it will obviously not going to understand that. Now, what is a thread? A thread is an execution path. A thread is an execution path.

Each thread needs its own program counter, CPU registers, execution stack, and scheduling state. Each one requires this information. And threads belonging to the same process generally share the process code, keep on the other process resources. So now, we have seen what is a thread. Now, those threads can make progress concurrently. Maybe multiple processes, can it make concurrent processing? Yes, they can. On a multi-code processor, runnable threads may execute in parallel, subject to operating system scheduling, and other constraints. So basically, there are two things which we need to distinct. One is concurrency. Another one is parallelism. Very important. Please focus for the next two minutes. We should not be getting confused between the two, concurrency and parallelism. Concurrency is all about multiple tasks make progress during overlapping career. During overlapping period, when multiple tasks are making progress,

then we call it as concurrency, concurrent processing. Then what is parallelism? Say multiple tasks execute at the same instant on different processing resources. This is parallelism. Concurrency is basically an internal goal. Now the parallelism, multiple tasks execute at the same time. If multiple tasks make progress, that means need not be executing at the same instant. They are making progress during overlapping periods, but parallelism is about actual execution at the same time, same extent, on a different processing resource. So if a computer has one CPU core, can I support multiple threads? The answer is obviously yes. It can interleave their execution, although they cannot execute simultaneously on that single core. If it is considered as a single core, then we definitely cannot parallelly process. But if it is a core that accepts simple threads, then definitely, yes, we will be able to run the multi-thread

or parallelism. So it can leave their execution, although they cannot execute simultaneously on that signal. So how to compare? Because without comparing the processes and the threads, we will definitely not be able to understand the differentiation and also the value. Now let us compare process and thread. We understood that the process creates thread. Basically, it breaks up the unit of work and create threads, and these threads are actually submitted for the CPU time. Once they get the CPU time, it will process, and it will give a response back to the process. Then hence the process takes it back and does. So now, how do we compare? We need to compare in a way that we are able to understand it. If you are going to understand threads, threads are often lighter than processes. So that means it can be smaller, It requires only minimal time to process. It needs only fewer CPU time. So we don't need to allocate the CPU time exclusively for anyone else.

So in that case, often lighter than processes, but do not conclude that threads are always better. That is something which nobody will be able to accept it. Shared memory makes communication convenient, but it introduces race conditions and synchronization requirements. Okay. What happens if two threads update the same shared counter without synchronization? A race condition can cause lost updates or inconsistent results. to which thread will clip the value while reporting. We really do not know and we cannot take that chances. So, erase condition can cause lost updates or even inconsistent results. Now, let us distinguish thread abstraction from the mechanism used to schedule it. And user level threads can be managed by runtime or a library in the user space. And kernel level threads are known to the operating system and can be scheduled by the kernel. So if you look at the aspect of process normally separate thread shared within the process.

Execution state process resource context. Thread is basically execution state is one PC registers and stacks. Communication of an IPC, here shared memory plus synchronization, failure isolation, stronger boundary if you are looking at the process. In the thread, a serious fault may affect the process in which the thread belongs to. Now, user threads and kernel threads. In many to one model, that means many user threads mapped to one kernel, a blocking system call can block all those threads. One blocking system call is sufficient to block the entire threads. In a one-on-one model, each user maps to a kernel thread, and the kernel can schedule them independently, which we need not expect. Okay, cool. So now, in one-on-one model, each user thread maps to the kernel thread. The kernel can schedule them independently. but in many to one model many to one model many user threads map to one kernel entity a blocking system call can block all those threads

and there is another model called hybrid model and this hybrid model can multiplex user threads over multiple kernel execution integrals. So basically, ordinary pthreads are generally kernel-visible tasks, NPT. Basically, we can cross-check the result is. Now, we just wanted to understand. We have understood the threads, threads in the user area and threads in the kernel mode. now imagine that thread a is executing a calculation let's assume imagine a thread a is executing a calculation the operating system decides that thread a should stop running and thread b should be executed if that is what we are considered as okay so now the thread a's execution state must be preserved so that it can resume later the operating system selects thread b and restores the state needed for its execution whether you have n threads it's the same concept that is going to work so if the cpu spends time switching between tasks is that time available for

useful application computation okay the switching itself is overhead although the overall system benefits from sharing cpu time among tasks the switching itself is a overhead so we discussed about concurrency and parallelism concurrency will be measured over the operating system may interleave their execution, leave their execution. Anybody? So we understood three main things, the process and the threats, concurrency and parallelism and CPU scheduling. We got introduced to the word CPU scheduling. What will CPU scheduling do? it will schedule for threads, not for a bit. It will schedule for threads. So if you are connected over a thread, then definitely, yes, it would be possible because I'm receiving. So now, before deciding which algorithm is better, there are so many algorithms. Before even thinking of which algorithm, we need to define what better means. Different systems optimize different objectives.

So, there is something called CTWR, that is basically completion time, turnaround time, waiting time, and the response time, all are based on the time. And they have a definition, obviously, and for forecast revenue, we generally use this. So basically, what does it do better? We should actually experience it, I used to take a while. So now, context switch, that's where we are coming into. So there is an event, event is nothing but the request from the application, so basically the timer block yield or wake up and then we do have preserve basically save state needed to resume the current task probably it has been kept on hold by the cpu and scheduler chooses eligible work and it restores next execution context restore means it is restoring what is which application is doing all these things the customer application at this point of time. So now you are selected but what you want to do is you wanted to restore it so

you should be in a position to restore. Now once you restore the next execution context comes into the picture and the next task continues execution. Now before you assume basically the stopped processes or stopped thread where we would like to have his password entered. So quickly what we have done until now is we have also extended our introduction to context switching apart from concurrency and parallelism and got introduced to the threads now what are we going to achieve first and foremost thing is we should we should draw the water and okay now when you're speaking about the context switching we discussed yesterday that the switching is happening between the processes where one is waiting for the resource and another one is expected to complete the session. Okay. So I would like to pass here and stop sharing. Just wanted to check if there are any queries, questions. In mapping our understanding from system call process,

process to threads, threads to contact switching, contact switching to attaining the CPU or memory, whichever is the resource. in our context it is cpu which is of high utilization so we need clarity on will we be able to yes we can but we really need to understand where is it going and how is it executing and how the response is going back to the request request and how successful it is only to monitor this aspect of the entire application there are monitoring utilities and i believe is also available in this environment so if you can then probably last six months data so let me stop sharing let me pause let me see if there are any queries in the that or if there are any hands raised i don't see any hands raised at this point of time and please explain about stack and heap i will do that when it comes and i'm going to take the example so this was the question i received yesterday as well but just have a little patience

so we will definitely speak about it. Process and stats, assignment, I'm not sure, about whom you are asking, but okay, fair enough. Parallelism, as we lost your voice in between, I think I repeated, do you want me to repeat? I don't mind. Parallelism, first concurrency, concurrency speaks about execution. So if you have two trains running, and if you have only one line at some point of time, only one line is available, and both the trains are supposed to pass through that, then one train would be stopped, another train will be allowed to pass through that single line, and then the second train would be allowed to pass through the single line. So the bottleneck here is the single line that is available in between, but both other sides we have double lines. If that is the case, then we really need to get it, but yes, we need to just move ahead and go. But if that is not the case, then we might have to analyze it.

So now, when we are speaking about parallelism, parallelism, it need not have to execute parallelism. Sorry, parallelism, it has to execute the same. That means if you have a single line in between, then we call it as concurrently both the trains are running. and when it has to cross that single line one will stop, other will go and then the other one will pass through but both are running at the same time that is concurrency but when we come to the parallelism both have to execute at the same time that means when it is crossing the single line in between we expect both the train to pass back We will not make one train to wait. So in that case, parallelism is something that it has to execute the same instant. But concurrency is something that it can make progress during overlapping periods. Overlapping periods, as long as we make progress, it's fine. That is what concurrency is. Can we say that multiple threads combine to form a process?

We can say that, but the right way of telling that is, processes create threads and holds them, monitors them. That is the right way to tell. But if you have 10 threads, 10 threads maybe belonging to three different applications. So which one is belonging to you? to you that also will be identifiable but we need to implement them while implementing that definitely we can create logs like how we are capturing the log and see who is actually using what account so now coming back so when we speak about does pressure is different from sorry Thus, preserve is different from cache as usage seems similar. Thus, ensemble or hybrid threads share the same preserve. Okay. First, let me answer the second question. Yes, it preserves. And the first question is, preserve is an activity. cash is a resource it should not be confusing between the resource of the utilization these are two different subjects so reserve and cash reserve is an activity

cash is a resource so can it go yes if the cash is available if it has to be returned in the cash then obviously contact switching is one is on the thread leverage or it can be process level also I did not get your question if you can put it clearly that would really be great concurrency dealing with many tasks at once doing many tasks perfect definition Thank you so much, Kosto. Is a working chef a good example for concurrency? Yeah, you can. Because the working chef, at a given point of time, may not be able to work on two tasks. But if you give the window, then definitely, yes, you will be using that. So now, the chef doing multiple things at a given point of time. So here it is doing. Chef is doing. So doing is parallelism. Dealing. Dealing is concurrence. So chef actually does both. He deals with many tasks. And at a given point of time, he also performs multiple tasks. So, yeah, concurrency is more appropriate to call out for a working shift than parallelism.

So, yes, parallelism also we can consider, but more appropriate is concurrency. Okay, cool. Differentiated by the resources they use. Concurrency on same mission and parallelism on different missions. so I don't want to get extended to that concept, but anyhow, thank you very much for bringing it, Parambi. It would actually confuse. First, if we are able to differentiate within the system, then we can go across the systems. But anyhow, thank you very much for putting it across, something that for the team to chew. Okay. How synchronization is done during concurrency? And can we run, can we run two, okay, let me go back to the message, okay, how synchronization is done during concurrency, and can we run two threads on a single core? Yes, we can, as long as the sufficient resources are available, can definitely run two threads. And synchronization for concurrency, actually it will wait for the last child process to respond.

Suppose if there are 10 child processes created by the process, and then there are threads that has been created by each of the subtasks, it will wait for the last subtask response before even concluding on anything if it receives then probably yes there is no need of any installation as such but yes we need to accommodate it's better to accommodate the way that it works sometimes it's not good for us we may think but just keep in mind every time it is only good for us we just don't know when that's all so now synchronization from the concurrency as I said before it will wait for the last thread for the completion and confirmation if it is hung or it is telling that I will not be able to respond then in that case maybe a couple of more minutes but not after that so coming back here so now synchronization I believe you might have understood and there are new messages let me take over them so can we say is a single all-rounder sports person

oh man a lot of analogies all I would say is one thing that was put forward one is doing things and that's a great example you can apply that for your analogy if that fits there then you are correct anything else I do see Varun is this and Varun I am allowing your mic quickly please sir there was a question when you were explaining the context switching I was having a question with the contact switching happens only on the thread level or it can also happen on the process level? Contact switching, see basically process, actually if you look at it, the process creates threads and threads will get the resource and the contact switching happens at the thread level but reference will be at the process level. It happens at the thread level but the reference will be at the process level. can i say like that that what we have swap in swap out on a process level similarly we have uh contact switching in the thread level or they are two different means we should not do like no

no it is not two different things okay okay that's what i said it actually happens at the thread level but we will always reference at the process level we won't say at the thread level but on the ground if you look at within the system, the contact switching happens with the thread because thread only gets the resources. But it will always be referred to the process because process only creates threads. And multiple threads can be created by a single process. Everything will be referred for the process. That is the reason sometime when you are seeing that multiple requests is coming to the system, you will see there are the same process will have multiple process ids because actually those are all threads and once those threads completes the parent process will parent that is ppid will get closed only after all the child so this parent child is something that can be created by process but also can be created by influenced by the threads that also possible so you should not

understand that thread is a different and process is a different it is better to understand that thread is born from the process now you'll be able to correlate did i answer your query yes sir and One supplementary question. We can limit the number of threads in a process. Is it possible to do that? Yeah, yeah, we can. Those are all configurations. Okay. At the operating system level. But generally, we don't do that. Technically, it is possible. But generally, we don't touch upon the process and thread configurations in an operating system. We don't do that. Unless you are creating an operating system environment for your own application, we won't do that. Instead of controlling the threads from the system level, we will always control it in the application call itself. So the application call will generate process, process to threads. So you will control it, the application call, that is the system calls.

rather than doing at the thread level or the process level are we good yeah thank you okay cool i think yeah vacation just loving your mic quickly please hello sir good afternoon sir i have a question around core so when we talk about core a single core can perform a single task at a time right we cannot have more than one task executing at the single core i i think this is not possible okay so basically what you're trying to ask is if i have a single cpu core will i be able to process only one task at a time is that your question yeah yes yes okay can someone help me with a response in the chat if i have single core will i be able to run only one process at a time or one trust task at a time or can i run multiple that's what exactly vikish is asking all about and sir uh so basically uh if we let's say we have a list of cores i mean uh our cpu has four cores and uh we we have a list of tasks because before getting into four cores let me answer you very clearly in a single core

see it is one core it's not single core okay so we refer it as a single core processor but it's not single core it is one core there is a difference between calling single and one so please refer the dictionary to understand more it's not single core it is one core and one core means it is a resource that is available but that can be split and used by different processes and threads so that means you can execute multiple processes at a given point of time with a one-core CPU also because the CPU will be virtualized and used because not all you know application modules require the whole CPU to process so that case what we will do is we will do it at the application level and from there it will break up and then move towards the network level so that is how it works did i answer your query yeah yeah so so my question is more towards parallelizing the tasks so since i work in a big data environment and we extensively use

this so that's where i'm just trying to get a clarity on so when we parallelize the tasks In each parallel task, we have set of subtasks that we give to the CPU cores to execute the tasks, right? So, each of these subtasks executes on a single core or one core at a time. We cannot have more than one task running at a single core. No, we can. But there is a context switching, right? Once a single task gets completed, then it will switch to another task. We cannot have a... Hold on, hold on. One core, whether it is one core, two core or three core. The core is divided and utilized by the processes. If you are assigning a process to a core, it doesn't mean that it will assign the entire core for that particular process. so it will allocate only that is required and the rest will be available and it can be picked up by a different process and both can coexist that is that is what you know even yesterday i was just asking about this similar thing so i thought it was cleared so you can run multiple

tasks in a single core let me stick to single core so that you will not have confusion We can also run only one task in a single core. It basically depends on how we have designed our application to run. So if you're using Java, for example, then obviously this multiple will work without any hassles because it is managed by the OS layer. So we don't think about it. What we need to think about is how much we have to pay per month. That's all. That is all would be the worry at that point of time. But otherwise, it is also easy to get the feeling that it's like totally free and we are good. That feeling also we may get. So in that scenario, instead of keeping some portion of the CPU idle, we will try to utilize all portion of the CPU with multiple processes. And multiple, since we have covered here, so not only the multiple processes, even the multiple tasks it can perform and close it only thing is it would not be like

a super fast activity by someone who can perform but as core is performing now can perform and close that's the belief so that is aligned one core you can run multiple processes even if you have multiple processes then multiple cores also can talk to each other which normally you know personally I just checked a couple of them this is the response I got from them basically that's what that is all about it did I answer your query yeah yeah yes so sir one more doubt regarding the race condition so if let's say as you mentioned that single core can execute more than one tasks so isn't it we will run into a risk condition if we have a single core and more than one task is trying to get executed that will be avoided we will not get into that risk situation the reason behind that is the scheduling activity happens once the thread is created the cpu is going to schedule okay we have 13 threads as of now in the waiting at a given point of time i can execute

four threads so the first four threads will be scheduled to use the cpu once it is completed the next four and the next two two that's how it might go if you have 10 if all the 10 can fit in yes it can one liners 10 lines it will fit in and we can get a response so either way either way it will work and it will give the throughput the only thing is whether it is going to take that extra one millisecond or not is the question if it is going to refer and then give back your response then there will be some millisecond delay in your response and that's why we started this session with cpu slowness and a response not received we need to have little patience especially in a scenario where something like the huge traffic is expected to come, it is better not to perform anything at least a week before. Make sense? Yeah. Okay. Thank you, sir. Cool. So now, Anuja, I'm just loving your mic. Quickly, please. Thank you so much, sir. Just a small query.

I just posted it written in the chat as well, just to make it So summarize for what I'm asking, uh, like, uh, how the contest switching or the process which happens, uh, when we have a overflow, that is a warm, like auto for Mori or overflow happens when we start the training, like, uh, we go with any more training. So, uh, can you some light how this process and threat used to, uh, do the manipulation behind the scene when it has a dynamic, uh, you can say independently, uh, dynamic programmatic way to switch that particular code, you can say, force from CPU to GPU, like the process have got out of resource from CPU, how and what happens behind the scenes so that the GPU starts taking at the distributed way and the workers are able to take up the job, if you can give some light. Okay, so you're speaking about CPU and GPU, am I right? Yeah, I mean, there would be the switch happens in between them with respect to the process

and the overflow and and the threads yeah yeah okay see whenever there is a workload to be processed let me start from the workload whenever there is a workload to be processed then obviously yes the system call the process will get created and the moment that is getting created the threads will be created Now, we need to allocate resources to the thread. If the workload requires GPU, then it will go to GPU. It won't go to CPU. CPU is only for non-GPU requirements. So, will there be a clash between that same process running in CPU and GPU? I don't see that happening because most of the GPU requests are going to require the GPU processing. So that is number one. And second thing is, when you are identifying that this workload needs to be performed by GPU, it will never think about CPU. So if you are speaking about a context switching, context switching is related to the workload assigned to the interface.

So now, I have connected to the CLI of VM, and then I'm operating inside that. Did I do anything wrong or did I met with, no, that doesn't mean that, that doesn't mean that way. But if the workload is for CPU, it will go to the CPU, if it is for the GPU, it will go to GPU. First of all, there won't be any overlap. So there is no contrast. If there is no contrast, then we expect the patient to heal, get healed soon. But given the age factor, we really need to be, if that is the suggestion from the doctor, then we need to listen to that. So in case if the GPU is full and the workload is also GPU-based, then it will wait for the timeout before even selecting CPU to get it processed. Can we do that? Yes, there are certain commands, which is an overlap, which will work for sure. So that's how it is going to be. So when we are looking at, the way we look at is, what we expect you also to look back at us that's that's the message we always give to anyone right

so it is very similar here so did i answer your query or is it confusing yeah i mean that answer just expanded a bit more towards what i'm looking forward so if in case my program which is actually having some processes which can be done via sapio and the same time that that can also you can say execute some of the heavy data lifting or we can say some serialization process which can be done via gpu at the time but it is being done within the same process thread which is within the same program so does this becomes a kind of a concurrency or a parallel thread which is being happen here two different two different resources right yes yes so uh yeah obviously both sorry the threads are getting executed at the same time but we cannot say it as a concurrent because concurrency and parallelism comes only if the same resource is being used by the process suppose if that division happens at the thread level

then it's not parallel because GPU is a different resource and CPU is a different resource. Any which way it has been designed to work so we can't claim it to be and give a credit for parallelism or concurrency. Two different resources. Okay. Understood. Thank you. Anything else from anyone before we move ahead? I think in this way, only possible multi-threading to execute on . Yes, it is. Okay, cool. So now it's like 3.16. Let's take a five-minute break. Then we will come back, and then we will move ahead and understand how the thread is getting executed, the use of the thing. And we will let us also see how to calculate them. If there are anything that we will be able to calculate. Now, within the scheduling, there is something called priority scheduling also. We will see that. Scheduling is nothing but making threads wait in a queue, but process it as soon as possible. That means it will not leave the CPU idle.

Not even a millisecond. As long as you have tasks to be performed and there is a system called MADE and we do have instruction to perform, it will get performed. There is no way that we will go back with that. So answering these two, let's take a break. For five minutes, it's 3.17. Please come back by 3.22. Max by 3.25. Thank you, guys. I'll also be back by then. Thank you. Sorry, guys. I'm back. Am I audible? Am I clear? Can I have to confirm, please? Okay, great. Thank you so much. Is it audible? Is it clear? Okay, great. Now, when we have workloads, Once a task assigned to a core gets completed, then it will pick another task. At one point, contact switching does not necessarily happen only after a task is completed. Okay, so instead of that, I would rather put it across. Once a task is assigned to a core, it does not mean that the task will be allowed to how much time it takes to get completed. It is pretty much possible that the task

could be put back into the waiting in the same state, whatever state it was at that point of time, and then it will be allocated to the CPU again, and it will continue from that state. That is also pretty much possible to happen. In long or high utilization workloads, This can happen, especially in data engineering and data science. It will happen. And once a core is executing a task operating system, scheduler may either let it finish or interrupt. Yes, that's right. It can interrupt. It is not that it will not be able to interrupt. It will move it to the wait and come back again. Now, let me share my screen. is that here it is okay let me clear it now okay contact switches have a cost we are switching definitely it has a cost and it will interrupt in between interpret counts alongside latency throughput and cpu utilization basically if there is some latency then also it can just push it to the weight and give it to see if the latency improves a little later

and throughput and also CPU utilization. If there are 10 workloads that is parallelly processing now and suddenly the CPU utilization is 85%, then it is pretty much possible that when it reaches the 95, 96%, some of the tasks, some of the workload that is being currently processed may be pushed back, interrupted and pushed back to the wait state so that to maintain the healthy state of the system. Yes, we will have a long, so we need to expand the CPU. However, we are planning to do. Once it is done, then we will see the workload will start executing faster. So for the basic single CPU, the scheduling with no I-O block, there is something called I-O blocking with no I-O blocking. Then let us assume that A is the arrival time, C is the completion time, so C minus A is the total time taken. So that is the turnaround time, that is T. T is equal to C minus A, where C is the completion time and A is the arrival time.

Suppose if A is in the waiting also, then the waiting time is represented by W. So total time spent waiting in the ready queue. Because only if the process is ready, it will go to the wait queue. So suppose if you have the wait, then waiting is nothing but T minus B. And response time, first CPU start time, minus arrival time, response time. Suppose if I have arrived at 3.1, and if I got the CPU at 3.2, then the response time is 1, 3.2 minus 1. one that is one minute that could be that is how we calculate but what is b b is basically burst we can also have a burst burst is what in case if the cpu is getting full and if we have allowed cpu to burst that means adding additional cpu to the same system then when the burst happens then definitely more workload will get assigned from the queue and it will get processed and closed. So that waiting time is basically the time that it has waited to get the CPU

and turnaround time is how much time it has processed. See my arrival time. It is not allocation time, arrival time. Arrival to the queue minus completion time. So basically, completion time minus arrival time will give you the time, the period that it has taken for the turnaround. So when you are scheduling, what will be the criteria? Criteria is basically multiple. There is no single criteria or the single metric that fully describes scheduling quality. No. TP utilization is one of the criteria. Throughput. Jobs completed per unit time. Throughput is another. Turnaround time, completion time. Waiting time. The waiting time is increasing. Response time and the fairness. How equitably service is allocated. These are all the criteria on which the scheduling is going to happen for the B. So three timing equations. Turnaround time, waiting time, and response time. A is arrival. B is a CPU burst. C is completion, and S is first start.

First is CPU start minus arrival. That is the response time. So if arrival time is zero second, at the 0th second, and s is equal to 5. That is, the initial start is in the fifth second. Then the response time is 5 seconds. So you can do it in milliseconds also. I just told in seconds because it is easy to understand. So we'll be able to calculate the turnaround waiting time and respond time from a process timeline perspective. Suppose if I ask you, which resource is normally shared by threads? The same process. Is it program counter, process, thread registers, thread stack? Which one? Or which metric measures the time until a process first receives a CPU service? Is it response time? Yes, it is response time. Time is nothing but how much time it has taken to complete the process. Context switching. If we are clear about these three, we are good as far as introduction to the CPU and CPU scheduling is concerned.

What we have seen is just now, we have seen only the criteria for scheduling. But based on this criteria, how it gets scheduled. so before that this is just a workload comparison you can see this offline but how it gets prioritized so now let us have some input table and process it's not priority but it is process so here the process 1 process 2 process 3 and process for arrival all the four has arrived at the same time that is a zero millisecond first time seven milliseconds four milliseconds one on four milliseconds now there is a priority this p1 is for the process identity this is the priority so now which one will it take obviously priority one will be taken, that is P2. Is it what? It is from 0th second to the 16th millisecond. Here 7 plus 4 is 11 plus 1 is 12, total 16 milliseconds. Here also we have 16 milliseconds. That means no two processes are getting processed. So there is no parallelism, it's only concurrency. Because total duration

required is 16 and it is happening in 16 milliseconds but p1 0 to 7 p2 7 to 11 p3 11 to 12 before 12 to 16 this is basically a simple and predictable q ordering average waiting time and average turnaround time basically for all the four processes what is the average and what is the average turnaround. Now, this is first come first so F-C-F-S. It will execute in the same queue order irrespective of the priority mentioned in there. We have priority 1 here. So if we are following the priority scheduling, then obviously we would have expected P2 to complete, get completed in 0 to 4, but only on the arrival processes it will process. That is, first come first service, execute in the same queue, order. Now, only one CPU we have, that is the assumption, and all processes are arriving at 0, time 0. Context switch overhead is ignored. Suppose if you have the context switch, then probably it will not allow full seven milliseconds for process one.

Probably it will switch the context to bring P2, complete it, and then continue with P3, then continue with that chunks calculations also will happen. So we are deliberately keeping the workload identical. This lets us attribute changes in the results to be scheduling policy rather than changes in the input. So, first come first service, this is the Gantt chart, 0 to 7, 7 to 11, 2 seconds, 11, 12, 12, 12, 12. Now, average waiting time, it says 7.5 milliseconds. How is it? Waiting time for the P1 is 0. Waiting time for the process 2 is 7. Waiting time for the process 3, 11. And waiting time for process 2 is 12. So basically, 11 plus 12 plus 7 divided by 4. So 30 divided by 4 is 7.5 milliseconds. That is the average waiting. Average turnaround time. This turnaround is 7. This is basically 5. basically 11 and the turnaround is 12 for this and turnaround is 16 for this. So basically 7, 11, 12 and 16. If you find an average for this,

you will get 11.5 milliseconds. So the strength is basically simple and predictable queue ordering. No complexity in queue maintenance. but short jobs may wait behind the long jobs on why effect like how you have the process three is waiting until for 11 seconds before it can start processing that disadvantage is available okay fair enough so fcs fcfs is a simple and predictable but a long task can delay several short tasks behind it. This is commonly called the Convoy effect. Where might FIFO-like behavior be used? FCFS. It is like a FIFO-like behavior, first in, first out. Expected is simple job cues or ordered processing and systems where fairness by arrival order is more important. who is paying first, their payment should get confirmed before the next payment can be accepted or processed. Then FCFS actually helps us. But FIFO application queue is not necessarily the same thing as the operating system's scheduling policy.

In application, if you have FIFO first-in, first-out, it doesn't mean that it will be applied in the CPU policy. CPU policy is totally different, and FIFO policy in the application QE is totally different. Now, the second kind of scheduling is SJS. Sorry, SJF. What is this SJF? Shortest available burst first. In this case, whichever is the shortest job first selects the ready process with the shortest CPU burst. So in our example, P3 needs only one millisecond, so it executes first. Then P2, which requires only four seconds, so it is executing second. P4 is third and P1 is the last. Basically, the strength of this is the shortest available burst first. The strength is basically it will minimize the average waiting time. Because start jobs are complicated, so they don't have to wait. The waiting time comes down. And the average turnaround time also comes down. In this SJF, start jobs first. Now, is this the best thing to do?

It looks like because you have the average waiting time and average turnaround time, both of them are done. For this workload, SJF minimizes average waiting because all processes arrive together. the burst lengths are known but production systems do not generally know the exact future cpu bust for every task and sdf is non-preventive srtf we have another srtf a newly ready process can replace the running process if it has a shorter remaining cpu first suppose if p1 is processing and at At that point of time, P5 is coming, process 5 is coming, and it is having a burst time of 3 seconds, then automatically there will be a content switching. P5 will get executed first, given it back, and P1 will continue to run from the behind. So always the reading book will be given preference to get opened or given up. So that way, this is how it works. Now, could a long-running job wait indefinitely under a stream of short jobs?

A long job is running at this point of time, and suddenly there is a stream of short jobs coming from all types, and since we have configed JFs, all the short jobs need to be completed before anybody else can take it. That is something which has been completed last evening, so no worries, we will continue. Now, we have seen FCFS from first services, second is SJF, and the third one is Rarity. So, preemptive SJF is nothing but SRTF and B completes at 4 milliseconds under SRTF rather than 10 milliseconds under non-preemptive SJF. So, basically A and B arrival 0, burst 8 milliseconds. So, A 0 to 2, B 2 to 4, and A 4 to 10. P completes at 4 milliseconds under SRTF rather than 10 milliseconds under non-profit. Now there is something called priority scheduling. Now you have processes. Same P1, P4, P3, P1, P2, P3, P4. Now along with that, a priority also is getting assigned. So the priority scheduling selects the highest priority process

among the ready processes. The exercise, the lower numerical value represents higher priority. So the execution order, where is the priority mentioned? It was mentioned in the list. So the execution order would be P2, P4, P1, and P3. And in priority scheduling, the strength is it can favor urgent or critical work. Because if there is a payment that is being made, we can say that its priority is zero. That means topmost priority among all of the priorities that we have. So even if we are totally down, we are in a critical state, but it should be on the top threshold. So the strength is it can favor urgent or critical work, but low-priority work may be postponed indefinitely. So this example is non-preemptive. Priority scheduling can also be preemptive. It can be preemptive. So once you have the order, then we know that how it is getting P2, P4, P1, and P3 based on the priority. Now, what is the turnaround time and what is the waiting time?

In priority scheduling, it could actually be little more compared to SJF, but it will be lesser than the overall. Now, we also have another, we call it as round robin, that is time sharing. Now, preemptive scheduling allows the operating system to take the CPU away from the running task. Before that, a task has completed its current CPU list. one common trigger is the expiration of a timer based time scale. Now if you are going to schedule in a round robin or a time sharing basis, we will define the quantum. We will define the quantum here, 2 milliseconds is the quantum. And if you look at the average response for all the tasks, it's only 2.75. but small quantum more frequent preemption greater scheduling overhead and large quantum so large quantum suppose if you have a large quantum small quantum very good 2 milliseconds the average response is going to go down very fast but if you have the large quantum behavior approaches

towards the CFS from first the basis. So now, each process receives up to 2 milliseconds in this round robin. If it finishes, it leaves the system. If it has remaining work, it goes to the back of the ready queue. Back of the ready queue. So, basically first P1 runs 0 to 2. When it runs P1, that is the process 1, there are still 5 seconds remaining because the total estimated burst time is 7 milliseconds. Now P2 comes in. It gets 2 milliseconds. Total is 4. So 2 milliseconds is still remaining. So 5 here, 2 for P2. Now P3. P3 requires only 1. So basically 4 to 5. And it completes. So P3 will not come back to remain. But P4 will remain with 2 milliseconds. And P1 will remain with 3 milliseconds. Now again, P2 runs from 9 to 11 and completes. So after that, if you look at it, everything gets completed. To P4 and P1. Everything gets. Now, greatest advantage is we can restrict the queue going up. Okay, cool. So the final execution race will be from 0 to 16 seconds and everything.

But what will be the total response time? What will be the total completion time? That is something that we need to calculate and understand. Now, the quantum is a design parameter. here. If it is too small, the scheduler may spend a longer proportion of its time switching between the task. The switching between the task is also going to come with a cost. Cost what? The time that it is going to take to switch two different processes at that point of time. And if it is going to switch every two seconds, then we can understand how is it practical for us to. And if it is too large, The round robin can approach FCFS behavior for many figures. Now, round robin we have discussed. We know how the time quantum is done. Now we have something called preemption, starvation and aging. Again, preemption alone does not guarantee fairness. Starvation occurs when a task waits indefinitely or for an unacceptably long time because other tasks continuously receive preference.

In that case, it is starving for the CP. That means a low priority batch report, for example, repeatedly postponed by the newly arriving high priority tasks. Now let us assume we have these deals going on and a lot of payments are getting made. So now the payments will get the highest priority, get processed, adding to the cart will be the next priority because the item is getting shortlisted, the next stage is payment only. Now identifying and keeping is another one, probably we can say copper. So it is copper, silver and gold. So we do have copper, silver and gold. So silver is something that we have been asked to pick it up and gold is something that we need to carry because we need someone to carry. So now the starvation comes into the picture when the particular process is waiting for a longer time to receive the signal. Maybe it has been continuously getting switched because higher priority is arriving all the

time. So now, when it waits, then the ageing also comes into the picture. Actually ageing increases the priority of a waiting task over time. It is a mechanism for reducing starvation, although the precise implementation depends on the delivery policy. So we need to distinguish starvation from deadlock. Deadlock means the resources are locked and they are available. But when we are speaking about starvation, starvation is nothing but indefinite scheduling or resource allocation. deadlock involves a set of tasks okay so we need to distinguish starvation from deadlock starvation is indefinite postponement due to scheduling or resource allocation deadlock involves a set of tasks waiting on resources or events that cannot progress under the current conditions it cannot progress that's what it means by deadlock. So we need to release the deadlock and then we can continue with that. So there are three things. One is

in the scheduling algorithms, preemption, a running task can be interrupted. So another eligible task can execute. That is also possible. A task that waits excessively because it repeatedly fails to receive the service, starvation. And to check the starvation we also have the aging capture. but aging increase effective clarity over time to mitigate starvation. Another day we need to mitigate the starvation. There is no point in running out there. So now the preemption, starvation and aging. Now let us compare all the four algorithms. First come first service. All four algorithms calculation has been done. Average waiting, average turnaround and average response for the same process where we have the visibility of the burst. So FCFS average waiting, if you look at it, the minimal is non-preventive SJF. Shortest job burst. 3.75 milliseconds. But when you're looking at the average turnaround, the least, that is also non-preemptive

SJF. 7.75 milliseconds. The average response, if you look at and compare between these four processes, that is 2.75 is something that we have round robin. So the average response is going to be the minimal for round robin. The average turnaround and average weighting is going to be non-permintive. So this is how it compares. So if you are looking for an average weighting, then non-permintive SJF, probably is the chosen one but if you're looking for a higher lower average response then probably you can think of a round robin wherein the quantum is equal to two i don't want so for this workload sjf has the lowest average weighting and turnaround but round robin has the lowest average response these are all the four algorithms that cpu scheduling uses to schedule the workload and avail the cpu services available to it okay perfectly fine now which of these algorithms did linux use when you ran your normal application we ran yesterday that ordinary linux

scheduling is not simply a choice among the four policies the next practical uses linux to observe the real threads and try them to simulate the textbook algorithms explicitly so basically here what we are going to do is we are just going to practically see how the ubuntu is going to to schedule and simulate it. We are going to simulate it. So we will observe actual threads on Ubuntu and compare the behavior with the simplified scheduling models they have calculated. So we are going to have probably there are two one is A and B. Then we are going to observe the threads in a running process. Also, we are going to simulate this paralleling process. Now, let me stop, and let us quickly look at how it works. I do see a mixed response between audible, not audible, was there any interruption in my voice? Was there any interruption? It was clear most of the time, so that means there had been. I'm really not sure, my connection is good and I do have one of the best headsets.

It's it. So, okay, when, I think I'll keep one of you, probably, I will allow the mic of Varun, who's there in the first in the participant list. So, Varun, your mic is enabled. The next time when it happens, please call me out, because I'll not be able to, voice was gone at quantum of two seconds that day. It was loud and clear for me. Okay, fine. I'm really not sure, but I just wanted to know when it goes down so that I can see if there is any opportunity for me to troubleshoot. It would. So this is how the CPU is getting scheduled in the form of threads for the processes that has been generated by the system call has been called by the application. If you are able to follow, if you were able to follow the statement I made, then we are good until now. Can you all please confirm? Are we good? Were you able to make out my last statement, what I meant? Are we good? Yes? No? Maybe? Okay. Great. Perfect. Am I sharing my screen? No, I am not.

Okay, so basically, if we need a better turnaround time, we need SJF, right, sir, if we need a better response time, then, okay, instead of saying we need SJF, we can say that we should prefer SJF, better response time, we will get it to the round robin. But round robin also has another disadvantage, it has the longest waiting period, sorry, longest completion period, turnaround time. It goes around Robin, it goes with the quantum. So that we need to be aware of both advantage as well as disadvantage, constraint as well as benefit. Then we need to decide. We just can't decide with a simple. Okay, cool. now let us look at something okay so let us actually go and observe the threads demo just give me a minute let me bring up my workstation and then let me share my and so it should come up when you talk to hey, cool appearance, let me change the appearance so that you are able to read it clearly when I am pairing, okay

cool, so here is mine so here it is I am going to share this window, hopefully you are able to see my console So now we are here. Let me, we have, we created a directory yesterday, did we? Yeah, it is process call. We will take the same thing. Now let me create a C program. And this C program is basically to understand the threads. So now let me name it as thread-demo.c, and let me have this, ctrl-x, I'm saving the file. cat-thread.demo.c, yes we do have. So now we can go ahead and compile this program. So the name of the program is threads underscore demo. No, it is thread underscore demo like this. Now it is compiled. So now we have thread demo. Let me go ahead and put our hand. ls minus ls pwd. OK. to th or thread demo. Now execute this. So now you just look at the outcome. Observe the lines printed by the two workers. Basically thread one and thread two, two workers. And their output may interleave. And the order can vary between the runs.

The program creates two worker threads in addition to the main thread. Why am I at thread two point before thread one? Even though thread one was created first, creation order does not guarantee execution order. Scheduling, take-up timing, and the system's current state influence when each thread runs. Now, let us observe the next threads. So let me see, let me run this in the background. So instead of running this, let me run this in the background. Background means I'll have to put it in ampersand. So thread one iteration two, it doesn't run because it is giving the output from time to time. So if I give top, it is running in the background. So I can say PID. Still, I've not got the prompt yet. Will this still work? The demo is done. When the process is running in the background, I am not getting the console back, so let me not do that. Now, let us have another Threads Live. Basically, that is going to give us an understanding about a live,

and it is also a C program. Let us create this program. Let me say it as threads underscore lab dot c. Nano, yes, Nano. Now let me have this feed here. This is also a program. But what it does is, there is a while loop. That while loop, we are running a printf for two seconds, and this while is going. Then we have the main key thread, threads are three. So far, there is a far loop here, and then the while loop. While loop is actually passing. And finally, when it is coming out, it needs to return 0. That means return nothing. So I want to save this. Yes, I want to save this. So let me go ahead and compile and run. First, let me, okay, so compile this. Yes, compiling. It has compiled, so no problem. Now let me go ahead and try to run these three together and see what is happening. So now you can see the top command. There is a worker 2, worker 1, worker 3. Those are all the output coming from the background process.

But if you look at the command threads live, that is what you need to do. Now if I want to stop this, I'm supposed to either control C or I can also go for a kill of the process ID. Let me press control C. It has come out. Again, it's continuously, it's live. It is continuously running. And control C, it is out. Oh, still it is running. Okay. See, still it is running, even press. So now, let me give the top. In this top, there is thread live. Let me give a copy and let me paste it here and run it. So now we have two zero zero eight four. So let me kill the process ID. I'll just kill, I'll just say double quote dollar PID, still it'll run until I, sorry, K-I-L-L, let us say dollar P-I-D, double quote. What is that? Something went wrong, so K-I-L, double quote, dollar P-I-D, double quote, enter. Now it is terminated. So until it is terminated, it is going to run. But as long as it is running, it will get aligned, and as of now, it is going on FCFS.

Also, it is living for only short time, and new threads are coming from the same process. So, the main thread is sleeping in pause. While the workers periodically print messages and sleep, This is useful for observing thread identifiers, but this is not a CPU-intensive scheduling benchmark. Because our CPU utilization has never shot up, but only thing is multiple small streaming workloads are coming, and it just displays some message and it goes off. So how many threads did the application create? Process has three workers plus its main thread, so there are four. Actual simultaneous execution depends on CPU cores and scheduling, both. CPU bound loops would generate more runnable work, but measurements would require care. What measurements? The calculations that we are measuring, that requires some care. Okay, fair enough. Now, our environment showed actual execution. We now need a controlled environment in which we can compare FCFS, SJF, and Priority,

all the three, along with round-robin, using exactly the same inputs. Now, let us set the round-robin quantum truth to 2 milliseconds. And we can now have a ready-to-run example, which demonstrates FCFS, non-preventive, non-preemptive SJF and non-preemptive priority, and as well as round-robin. Same, whatever data we have used, arrival 0, burst is 7, and priority is 3 for P1, whatever you have seen in that presentation, it's the same thing. So now, let me clear the screen. We will create a program. Let me create it as, give some name, scheduler demo, underscore demo.c. And let me save it. So this is the run, which we are going to run. Let us save this. So cat scheduler.c. yes it has really good link d1 it's a c program so we need to sorry it is a python program what is that we have ls minus la the name what you are supposed to give is scheduler demo so scheduler demo.c it is not scheduler demo.c it is scheduler demo.py

So now I can run this using Python 3. You can say scheduler demo.py. Now you can see this. FCFS, P1, P2, P3, P4. It just calculates the averages. CAD completion, turnaround, and the waiting response. So, Gantt chart, in which preference it has taken, FCFS, P1, P2, P3, P4, SJF, P3, P2, P4, and P1, and priority, basically, P2, P4, P1, and P3. So if you look at the least completion time, the average is 7.75. It is the same table that we have seen. This is just an introductory simulator. Check the output against the manually calculated values. If they don't match, do not immediately assume that the code is wrong. Check the arrival time handling, Q ordering. time-quantum, and time-breaking rules. One limitation is the simulator models keep you berser as uninterrupted work. Real applications can block for input-output, wake-up later, and contend for the logs as well. These behaviors make production scheduling more complex.

So, it is a complex one, the scheduling, but it is an important one. With four different approaches that we have, we will be able to choose the one which is necessary for us. Now we can also look at something, industry case study. The industry case study, let us assume that an e-commerce platform is experiencing a flash shift. Same thing, same scenario. Product searches and payments are becoming slow. Which two? Product searches and payments are becoming slow. TP utilization is high. Meanwhile, background jobs are generating recommendations and reports. It is supposed to. Suppose if I'm clicking on a product, the organization wanted to actually reduce or even have no impact on payments. So the priorities are there. So you are the engineering team, you have 12 minutes to identify the likely issue, recommend what to measure and propose improvements. Now, this is a hypothetical case study. do not attribute it to a specific company or a production incident

no definitely not but this is something that I just want you to understand I'm just going to stop this sharing I'm going to paste it in the chat so I haven't heard from Varun so I think we are good so infinitive loop due to while 1 yeah that's right perfectly fine that was created purposefully so now this is what i was talking about so for each of the workload suppose if it is a report generation requirement is eventual completion without completion that the report generation is not going to be active to have the eventual completion, starvation or excessive day is something that you want. So take this as a hypothetical and now I'm going to give you five questions which is basically around these parameters. So what I would do is let me test it separately so that there is some clarity so this is the situation you are in and put it separately now this is the scenario you have so just let me see if this comes as a table

it is not how do i do that i think the only way is you need to expand your chat window then only it gets aligned no doesn't okay let me do one thing let me just see if i can give this as a table as an image i think it should be possible so this is the workload this is the requirement and this is what we are expected to do this is the table so we are able to download the image and see the image If not, please let me know. And then I want you to individually answer these questions. You can have a discussion, one or two of the group or whatever you have. I'm okay with that. But these five questions I want you to answer. How much time do you guys require to work on? So look at the situation, look at the requirement, and look at the questions. So does 20 minutes sufficient for you? 15 minutes, okay. What do you guys suggest? Take a minute and then confirm back. Basically, I'm just looking for, are we good with 20 minutes?

Yes, no, maybe. Not receiving any response. so I think everyone is on to the questions I believe. Okay, fair enough so I'll run a 20 minute clock timer and in case if you have completed before that, then you just give me the answers on my one-on-one chat personal chat and that will also indicate to me that you have completed. There is definitely when I am back we will definitely discuss, so yes Akshay, if they use one for parity scheduling payments, okay, Vijwal also is giving the same, sorry, so Satendra Kumar, which workloads need response latency search and demand processing that is correct which workloads background jobs can tolerate delays that's right uh could exercise thread creation improve performance thread creations and jobs background process can help in case of customer facing requests okay that is perfect apis db calls how might priority scheduling affect background jobs maybe we can fine-tune background jobs only exponential details in real time

okay let me stop sharing okay what metrics would you inspect before making a change that's what i was looking at okay great um utilization contact switch rate downstream service latency background jobs priority okay great so wow studio has written down that's great perfect you have put so much of effort in writing the question as well as writing the answers great perfect nice to receive so many responses here we have 250 plus i don't deny but going through all these responses i think better i will save them i will go through that perfect network logs interesting okay perfect guys looks like we are in line yeah here and there some deviations from what you are going to look at but major this is perfect okay guys so any queries any questions anything that you couldn't understand not only in this small assignment but also overall what we covered today any queries any questions please raise your hand if you have anything fair scheduling okay i will do that

Anything else from anyone? Vikash is asking about fair scheduling. Wow, Reshma. Perfect. Who will choose? Is there any update on the assignment? It's not yet opened. I'll follow up. The application thread has nothing to do with the CPU code process. If you are going to control the thread via the application you are writing, then it is application threads. application threads will get into independent processes and then it will get processed. If you're going to control, then you will control that the process is not going to create any threads. Your application threads will become processes. But if you're speaking about thread per core, I'm really not sure what do you mean by thread per core. If you can just, me one more line of what it is then i can definitely explain lscpu okay physical i'm receiving a lot of things okay let me take one at a time let me address that fair scheduling ask first uh fair scheduling is a scheduling approach in cpu

that aims to distribute the CPU time equitably among competing tasks. That is the definition. But the objective is to prevent one task from monopolizing the CPU. If you are going to follow the priority, then high priorities are going to monopolize the CPU utilization time. Lower priority might not even get its share. while other runnable tasks receive too little CPU time. We want to prevent that situation. Imagine three employees sharing one mission in a factory. If one employee occupies the mission continuously, the other two cannot make progress. A fair allocation policy gives each employee a reasonable opportunity to use the mission. The same principle applies to the CPU scheduling also. when multiple tasks are ready to execute. The scheduler decides how to share the available processing capacity. Fairness does not necessarily mean that every process receives exactly the same CPU time. It does not mean that.

The appropriate allocation may depend on priorities, weightages, resource limits, and the scheduling policy. We may also limit the resource for processes. Let us view an example of three processes competing for one CPU. Assume a single CPU core and three continuously runnable processes. Continuous. Each process has enough work to keep running. It's runnable. It is always runnable. That means the application is live. If three processes have equal priority and equal entitlement, then the allocation for one compared to the another, if it is lower, then it is unfair. But per CPU allocation means a CPU share per process, 100% divided by three, that is 33.3% of the available CPU time. So suppose if the allocation is 12 milliseconds, all the three processes are supposed to get 4 milliseconds each. The sequence may be here and there, but out of the 12 milliseconds, all the three is supposed to get 4-4 milliseconds each.

Now it could be P1, P2, P3. three. We can do a time sharing round robin. Is this the fair? Overall 12 milliseconds, every process will get 4 milliseconds. Seems to be a fair scheduling. But a fair scheduler considers how much CPU service each runnable task has received and how much CPU service It is entitled to receive, it's the combination of both. So basically it's not about only 12 milliseconds, three processes, four milliseconds each, but does all the three processes requires four milliseconds in that 12 millisecond? That question also comes in. Suppose if one of the process requires only two milliseconds to process, the workload is not even, then how the fair scheduling works. If the workload is not even, for example, process 1 is requiring 6 milliseconds, process 2, 4 milliseconds, and process 3, 2 milliseconds. Then can process 3, all are having equal priority. So in a fair scheduling, all the parameters which is defined should be considered.

So burst out of 12 milliseconds, 6, 4, and 2. Now, if you allocate 2 milliseconds by round robin, the P3 will get completed. In an equal priority, now P3 gets completed in 2 milliseconds, but P1 and P2 will be able to complete at the end of 10 and 12 milliseconds. second so is this a fair it's definitely not so fair scheduling is often associated with proportional share scheduling it's proportional not equal in which cpu capacity is distributed according to the task ways if three runnable tasks has weights of two one and one then the idealized CPU shares is 50% because the weightage is 2, that is process 1 the other two process weightage is 1, 1 that means 50% of the CPU share should go to the process 1 25% for process 2 and 25% for the process 3 now this way it will be considered as fair. How the priority in between multiple tasks being decided? It is not decided. It is basically assigned. The priority is assigned when the system call is made.

So, not a predefined dictionary. sorry, it is while we send the request, we will assign all these waitages and priorities. There is no definition within the libraries, okay, these type of tasks requires higher priority or something like that. It is something that we send along with the system call. And generally, system call, the application threads, earlier there was a question about application thread and thread per core the application threads along with the request it is giving it to the system called it will give the priority also to the system call and that's way it gets conveyed to the processes and threads as well so now coming back to fair scheduling versus round robin basic principle round robin is giving each ready task at time quantum in turn but pair scheduling it is the allocation of cpu service according to a fairness policy the fairness policy how it is built with all the defined parameters it will be built on the go but allocation is based

on weightages previous service eligibility and the consumption if this is how it decides the fairness in the cpu scheduling so vikesh i see that you have raised your hand i'm going to love your mic yes go ahead vikesh yes yes sir so you are explaining about the fair scheduling so i have a question around that so basically uh when we do the fair scheduling mostly in the big data applications, we highly depends on the data size. So let's say if we have 10 partitions and each partition is having 10 MB of data. So we have to make sure that each partitions is getting executed exactly at one core or one slot of the CPU. Then we check for the data skewness. So if some of the tasks having a lot of data to be processed, then accordingly it has to have more number of slots that needs to be allocated. So that's where the fair scheduling comes into picture. So maybe in that context, I think it highly depends on the data size.

Please do correct me if I'm wrong. No, from the data perspective, what you are saying is 100% right. Yeah. Okay. And one more thing I just wanted to add is the criticality. Maybe the volume of data is high for one of the application, for one of the source. Other source, the data volume is less. doesn't mean that the lesser volume data will get lesser amount of CPU time. Not that way. Suppose if that data is critical, then the fairness comes with the criticality. So now you need to allocate appropriate CPU time for it to complete within the time. Suppose there are two sets of data you are receiving. One is a streaming data coming from a stock exchange. It's all small units of data coming in chunks. And it comes as soon as the transaction happens at the exchange. Now, this data has to be returned to the database as soon as possible, maybe near real time. This is a set of data that is basically coming from a customer.

The customer is sending you the data of their application. That data is basically the system logs, application logs that is coming to you for the data engineering and data analysis. The stock exchange codes are coming to you for storage of the data and providing the data to your customers and customers. So these two are coming, working in parallel. So then should I allocate more CPU time for the customer's data, basically the system log, application log that is coming? It will be huge. Stock exchange quotes will be very, very less. So how do I allocate? So that's where exactly the weightage come into the picture. Suppose if I give one weightage for the stock exchange data, and if I give 0.58H for system logs and the application logs from the customer, then basically the stock exchange codes will receive double the CPU time. I'm not speaking about CPU size. CPU is always measured with time, how long you are using the processor.

So the CPU time will be double for the stock exchange codes. to the data received is comparatively very, very small. That is how it goes. So fairness, it is not based on one criteria. You have to see all the criteria that is appropriate for the transaction. That is how the fairness goes. Yeah. That makes sense. Yes. Thank you, sir. Anything else from anyone? Yeah, it is real-time as per the program's requirements. Sir, this is an issue, I guess. Please. Okay. Yeah. Okay, yeah, yeah. The same thing you're answering. Yeah. So, the fairness policy is stitched on-the-go. When I say on-the-go, it is not that as in when the workload is received. Whenever the workload is defined. because the workload will be defined and then only the workload will be received. In the previous example of stock exchange codes and the system logs from the customer, the weightages and all will be defined first even before the data is implemented to be received.

So when I say on the go, upon the definition, not while receiving, hope it makes sense. because by looking at the data it will not come to know it's not a human will not be able to identify but at the time of definition okay this is my source of the data it has a weightage of 2 this is the source of my data this is the weightage of 1 customer connection weightage 1 stock exchange connection weightage 2 so then the appropriate ratio will be provided as and when the data is received. So definition then process. That is how it goes. Vijay. Did I answer your query? Yeah. Just to make an input there too. So in case, if I say for a similar kind of example, when we do any transfer of one data to another drive, like from say, from a mobile device to a hardware device, like a physical CPU physical system itself so the real-time computation which actually happens to give a fair input or a fair idea that this is a time that has to be taken or going to be take

right that this process will be completed so isn't the same thing that we're that we can encapsulate that this is a fair information that comes from a cpu know wobbling in between Can you just repeat if you don't mind, Anuji? I think you went on mute. Got disconnected. No, he's there. Okay. Anything else from anyone? I do see 251 coming to 209 as soon as we struck at 5 PM. That is prompt. I'm sorry for the voice right now. Is it okay then? Yeah. Yeah, better. Okay. So what I'm just putting across. If I'm just making a data engineering question, like we are transferring from data from one physical device to another physical device, now it actually went ahead and takes out a real-time computation that what would be the time I would be required for each process to take this thread to complete via some task if I just break down the step by step that we just discussed a few minutes back. So it has to make a real-time policy for that

and all these real-time, you can say, activities, like behind the scenes, the copy itself is a program, right? It's a program. Understood. How it works, Anuja, is basically, it's only a definition at the first place. 2 is to 1 weightage, that example. Now, 2 is to 1 is the CPU time that we are supposed to divide between these two processes, these two threads. Now, one, let us assume that the CPU is available for the next two hours. Out of the two hours, we have to break it up into three portions. Two portions is supposed to be available for the support, and one portion of the time will be available for the logs that we are receiving. That is how it goes, one is to two. So basically, 1 by 3 means out of 2 hours, it is 40 minutes, and 80 minutes is for the quotes. Now, if there are no quotes coming at this point of time, now the logs will start occupying the CPU, maybe up to 60%, 65%, 70%. It will get processed.

But the moment we receive a quote on the another connect, the 65% that whatever is the current utilization for the logs, what CPU will do is it will drastically reduce. It will bring it to 33% and it will allow the quotes to go through first. And suppose for next 20 minutes, we are receiving quotes. So basically up to like 20% it will allow because it depends on the threshold what you have defined. Let us assume that you have defined the threshold at 60%. So that means 20% will go to continuous logs that we are receiving, which was using 60% earlier because that's the threshold. Now it will come back to. That is where exactly the processes will be made to wait even though it is ready. Now, 20% will be given to the data that we are receiving as logs. The quotes that we are receiving will be allowed to use up to 40%. So, overall, 60% is the limit. Now, assume that quotes are not heavy. Earlier in the peak period, we were receiving.

So 40% of CPU given to quotes and 20% of CPU given to logs. Now, after the business period is over, the quotes we stopped receiving. Now, automatically, these logs will pick up to utilize the CPU time. This is the fairness. Fairness is not only not allowing someone to over-utilize, but also we need to ensure that overall, we are not under-utilizing. Now, the quotes are not coming after 3.30 p.m. No quotes will come because no business is happening in the stock exchange. Now, the first process will continue to start up its utilization. It will start completing all its pending data to be processed. It can go up to 60%. Now, though by weightage it is not fair, but from the under-utilization perspective, it is fair. When there is no data for top quotes, then we can allow the other data to utilize that process of time. That is the fairness, Anuji. So did I answer your query? Yeah, you answered, sir. Just to make one quick thought about this.

So in case, once this process is reached to 60% and a user has initiated another application or another program, so down there, does the switch of the context, That is the process which will happen with some, you can say, new policy again, or the CPU will have its own decision making. Now you are talking about a third process, am I right? Yes, it's the third process. Okay, so now let us assume we can't be ignorant about what can happen practically. Yes, there could be another process that can get started apart from these two. then that is having no weightage defined. So as per the FAIR policy, it will be given the share of the time. By reducing the system and application log processing that is happening up to 60% now, it will probably get down by another 10% or 12%. And just 5-8% will be given to the new process. because we don't have the first process, any data coming in. Now the third process, we are receiving data,

so it will get its fair share. If there is no weightage, then the default weightage will be zero. That means the ones which has the weightages, to them we will provide the fair share and the available share will be given to the unweighted data. Make sense? But they will get it. But they will get it. So this weighted process is a different application altogether, which is running behind the scene, like completely in background, that takes a control of weight distribution or weight analysis that where I have to give. KPU scheduler. KPU scheduler is the one which manages that thing behind the screen. OK. Okay, sir, I understand. Thank you. Thank you, sir. Anything else from anyone? Yes, sir, Soumya, I see you have raised your hand. I'm allowing your mic. Please go ahead quickly. Sir, your screen is not visible. No, I'm not sharing anything. I'm just answering the queries. No, sir, that time when you were speaking at that time also,

it was not available. No, that time also I was just answering fair scheduling. Okay. I was not sharing anything. Okay. I thought, sir, it is not visible to me. No, no, no problem. No problem. Anything else from anyone? Okay, guys, have a great evening. See you next Saturday for the lab session. Take care. Bye-bye.

