Looks like we have lesser participant today. I just waited for 10 minutes so that to see if more participants are joining, maybe because of the Navaratri deliberations that are going on, pretty much possible. Okay, Neeraj, from where I can get this example to understand the system call? Okay. Why it is showing us 21-27 of Saturday, I'm really not sure. Okay, I am reading old message, I believe. Okay. The examples what we are going to see today that will help you to understand the CPU call and the GPU scheduling. First, let me go ahead and share my screen. Once you are able to see my screen, please let me know. Hope you are able to see my screen. Can one of you confirm, please? Okay, great. Let me clear the screen. I just kept it active by giving a ping command, and also another screen I just changed. Are you able to see which is ping command is going on? on okay let me stop that as well and clear the screen now what are we going to understand today in the next two hours basically we are going to create a c program which is going to create multiple threads and we are going to observe the execution and while observing we are also going to observe a CPU-intensive process and its resource consumption using Linux commands. Ubuntu is the environment that we are going to use. And apart from that, we are also going to see a Python program, and that Python will execute FCFS, SJF, and priority scheduling. we will also observe the round robbing against the same workload. That is the objective of this session, which we have understood as a theoretical session last Sunday. Okay, cool. Let us first prepare our environment just to check that our environment is pretty much good to go. So I'm just going to check if my system is up to date. And then I'm also going to see if I have the essentials for Python and processes. So the second line, if you look at it, I'm trying to install buildEssential, Python 3, and the proc.ps packages. so it is working on it and it is trying to connect it connected and it is checking and around a kb it has downloaded and it is building the package list and it says that all the three are available build essential python 3 and prop ps is available already installed in my system and there is nothing to update at this point of time okay good we have an environment in place now let us see if i have a gcc compiler in my system how can i check it gcc is the command if i say give me the version of it if it is installed we will get the version otherwise it will suggest how to install so i'm getting the version number uh gcc for ubuntu 15.2 so that means i have the gcc part of my system and also i'm just going to check if python 3 is available and i'm going to ask for the version if it is available it is going to say the version the version that is available in my system is 3.14.4 and i just also wanted to check if i have the process command that is ps command it should be there but yes ps from proc ps-ng 4.0.4 so now we are good now do i have htop htop is an utility in uni which will actually help us to see the details of the system in a better way. So here, if you look at it, it is showing us from 0 to 1, the tasks, 54 tasks are running, 44 threads are running, and the load average is 0.07 at this point of time. and it has been up and running for the last four hours. And this is the memory, total memory available of this. This much is actively occupied and no swap memory is consumed at this point of time. It says 0k. Just wanted to confirm, is it legible? Is it readable? Can you just give me a thumbs up? If not, I can increase the font size so that it is legible and readable. Okay, great. So this tool is what we are going to use to observe the CPU utilization and as well as number of threads that is running. Great. So GCC, it will compile the C program. Pthread is basically a POSIX thread threading library, which we will use to create and synchronize threads. Python 3, we are going to use it to execute the Python program. PS is going to display the process and the thread information. Htop provides an interactive process and thread monitor. Instead of htop, we can also try to use top, which doesn't have color combination. And this is going to be a textual representation, but the data is the same. So whichever you are comfortable, you can use or probably we can see in either of the one or both. okay so now i do have a directory called fcs session 10. this is something which is already having a python program which is the scheduler underscore demo dot python and we also have a program called threads underscore cpu dot c don't worry about the content of these programs I will be carrying it as a PDF in your model and I also will paste it in the chat if possible in the end of the session. And by using the GCC, I have compiled this program. Now, what is this program? First, let us go through the C program. So, let me clear the screen and then use the nano utility to open up the threads underscore cpu dot. So, now if you look at it, I have defined the POSIX. when we are using the definition 46SE source 20809L so what does it mean? it basically requests 461-2008 interfaces from the system headers 20809L is what we are going to use and we are also So including packages, something like header file, something like pthread.h, standardinput.h, standardint.h, standardio.h, and time.h. And I'm also defining the thread count. I want six thread counts. And let this program run for 60 seconds so that it will give me an opportunity to observe the behavior. Okay, cool. So we are using POSIX threads, commonly called pthreads, and this is the standard interface we will use to create multiple execution paths within one Linux process. Great. Now, what we are trying to do next is, we have seen the thread count. So if I change the thread count from six to eight, will the application create eight threads in total yes it creates eight threads plus the main thread okay so when i say thread count is equal to six this is without including the main thread so totally we should see seven threads one is the main thread and that main thread will create six sub threads so we should be able to observe seven in total now we are next defining the thread argument in this program so here i am telling type definition is suspect and integer id so integer id stores the workers identifier but defines a data structure and type def allows type def allows the structure to be referred as thread arc that is defined so this entire definition is referred as thread arc I'm just trying to explain this in case if somebody who's not having the background of C program, it will benefit them as well. Now, we have the next line, static double. Starts with static double that actually defines a helper function that returns elapsed time as a floating number. so again i'm using the structure time speed time spec now clock get time is clock monotic and whatever is the current time and what i'm expecting to return i'm expecting the return in the double integer format that is the floating now tv underscore sec as right from now from the start and now n second now to start the difference I am just adding these two and I am returning back now strut time spec now is basically declares a variable for the current time but this one declares a variable for the current time and now dot tb underscore sec minus start dot tb underscore sec this one basically calculates the difference in whole seconds and this is basically calculates in nanoseconds and I'm dividing it by 1 e 9 this converts nanoseconds to seconds so overall the function returns the elapsed during this one actually defines the variable id and we are referring it as thread arc and this one is going to provide us once it has started it will provide us the elapsed duration okay we need a way to stop each worker after a fixed period a monotonic clock is suitable for measuring elapsed time because the system clock can be adjusted that's the reason i'm using monotonic clock to look at it i'm just using a monotonic clock which will not get adjusted this is the system time but the actual system time whatever is displaying in my screen 1955 for example this can be adjusted I have an option to adjust this but I don't have an option to adjust the running system time so elapsed time be perfect now I'm just defining a worker function here what is this worker function is going to do it is going to Execute for each thread. Here I'm using the thread.org reference. This is basically going to convert the pointer to the expected argument type. What is that? Data to ID. That is what it is going to, this is the data. It is going to, if you look at here, I'm mapping it to ID. So thread number will get it from the ID and it will start records. Basically, the next thing, where do we have the start, will actually start records the start time for the particular thread. So for each thread, we have a start time. And if you look at the volatile counter is equal to zero, I'm initiating this with zero. And volatile tells the compiler that accesses to this object are observable and should not be optimized away as if they were irrelevant. So that is what this volatile will tell to the system. and this printf function is actually going to print the thread number and the status. Now, each worker has its own local counter. With this, I have created a local counter for each thread and the counters are not shared between the workers. So that I have ensured with this static void worker function. Volatile is not a general threat synchronization mechanism. Here the counter is local to one worker and is used only to keep the CPU workload meaningful. So volatile in general, we might have not observed that it is being used, but here we are using it only because each counter has its own timer. So we are good. Now, the next is we are going to execute the CPU-intensive work. Until now, what we have done? We have using the POSIX 2008-1. We have included all the header functions. We have defined the thread count, six and three as the time second. And we have defined the thread arg type definition. And we have also defined a worker function, which is going to have a thread count and as well as clock for each thread. Now comes the function. Let me go down to the integer main. So this main is the processes entry point. and if you look at it the threads the threads the thread handles returned by the threading API so this is the main function and this is going to return which one this is going to return the main thread function and args this args is basically going to store the argument structure for each worker. Now, these arrays are in the main function's stack frame and remain alive until the worker threads are joined and function. Until then, it will be active because it is under the main function. Now, what we are doing is we are creating a for loop. the for loop for loop actually tries for how many times whatever is the value of the thread count here I am defining threads as the thread count which we have defined as 6 threading arguments thread arg is the type definition and argument is also arguments of thread count that is 6 for initializing with zero unless it is lesser than six i plus plus it will start from zero so lesser than six zero one two three four five six threads will get created and we are increasing it cool this p thread create in the next line integer rc p thread create what it is going to create it is going it is requesting creation of a thread how this null this null selects the default thread activity and worker is the function that the new thread executes which one worker We have defined the worker here. And this will come and will execute for every cycle in this for loop. So now, if the arguments of I, the arguments of I passes a pointer to that particular worker's argument structure. And finally, the RC will return the, stores the return code, and a non-zero result indicates that the thread creation failed. That's why we have given, if the thread has failed, then it will have a non-zero function, and we are going to get an error message, and that's where exactly a printf I'm using with a standard error, pthread create failed with the timestamp. Now it will return. So this far loop is completed. And the error branch reports the failure and terminates the process. Moment it creates return 1. That means the moment it fails, it will come out of this loop. So now pthread create makes the worker eligible to execute. It does not guarantee that the worker will execute immediately or in creation order that we have not defined whether it has to create sequentially 1 2 3 4 5 6 threads that we have not defined since we have not defined then it can create we will see the output what exactly happens worker will execute immediately are in creation order that is not guaranteed okay cool now we will wait for the workers and how by using the p thread join function where is the p thread join function p thread join function is here in this for loop i'm creating another for loop for the same for each thread i want thread join threads of i i is equal to 0 until it is lesser than 6. And I am incrementing as per the for loop. pthread join threads of i. And the final message is printed after the workers have completed. That is return 0. Once it is completed, this print off is going to print all workers are completed. and we are going to return zero, which indicates normal program termination. Now, this is the program we are going to use to understand how the threads and threads are created, threads are run, and how it is going to impact the CPU utilization. If this small program can create six threads, And if it can use the entire CPU, what if you have like 600 threads from your actual program, where will it get the CPU? CPU will get divided. So we have to plan in such a manner that the CPU is available for our program. And that is where exactly the infrastructure architecture comes into the picture. Now, when somebody says that I'm going to run my web service in three virtual machines or 300 virtual machines, how is that decided? Basically, with these resources utilization function only. Will they get tested, Sundar? Yes. As part of the test, the resource utilization will be captured and it will be analyzed. Okay. I have written a full-fledged e-commerce code. As a developer, what we think? Oh, I'll have to write the code. Write the code. It is functioning properly. Functional test is successful. Oh, my code is ready. That's what we think as a developer. But that's where it doesn't end. It doesn't end there. Where it goes then? The next is the performance testing. The performance testing, functional testing, is going to certify the effort of a developer. Has the developer written the code appropriately? Whether it is running successfully? That is what the functional testing is going to give us a confirmation. But the performance testing is the one which is going to confirm whether the code written by us as a developer is effective and efficient. Are we handling the threads? Are we limiting the threads? Or are we limiting the resource utilization? As a developer, we don't think all those things. No problem. the testing team will come back and say this is the feedback this is where the improvement is required so that parameters will help us to modify our program and that is how most of you would be doing even today if you are a developer if you are into the QA you might be understanding already what I am trying to say the performance testing what you observe what is the resource utilization when it reaches the peak is the program altering its behavior or not or the program hangs. If the program hangs then there is no point in pushing that to the production because in production we don't have a control. The testing environment at least we have a control on giving how much traffic has to hit. You can say 100 requests per second 1000 requests per second that is where the certification comes we certify that this program can effectively support 1000 user requests per second and that's where exactly the operation statement the traffic okay what to be done if it exists if it exceeds thousand requests per second then automatically we go to the concept of providing more resources. How does that happen? In the cloud environment today, we do have a concept called scaling and that too auto scaling. So in the auto scaling, there are two types of scaling available to us. Because if there are any cloud engineers or who have worked with the clouds, they'll be able to understand this faster. We do have a concept called auto-scaling. And that auto-scaling gives us two options. One is, how do we want to scale? Whether do we want to scale vertically or do we want to scale horizontally? So the vertical scale is something that is related to increasing the resources or decreasing the resources provided to a virtual mission. That is vertical scaling. Instead of modifying the resources available in a virtual mission, if you are going to add a virtual mission, if you are adding a virtual mission, then automatically CPU is going to be provided and if that virtual machine is also used for handling the traffic, that CPU is also going to be available. So what is the role of autoscaling here? The role of autoscaling here is basically to provide the resources, the application, and this is how it relates right from development till operations. Development, code has to be perfect, no errors. testing, functional testing and performance testing. Are they going to stop there? No. Some of the organizations are also going to perform a security testing. So the testing activities, if they list down 7 or 8 types of testing they are performing, then we can understand that they are looking at 7 or 8 aspects of the application. Now, the security testing is the third which we are discussing now. The security testing is going to look at, are there any loophole for somebody to enter into the environment? Or if they enter, what they will be able to perform? Are they taking control of the system? Or are they only able to take the control of only the application? Will they be able to put some unrealistic and non-productive and non-customer traffic into the system? Will that data be recorded? So it depends. It depends on the depth of security testing. I'm just trying to explain you the security testing in a very, very normal user's language. now this security testing is going to confirm that our code is secured how they are able to get that because the application is what it is going to do, let me close this program, this application when I am going to run it is going to open up the resources it will start utilizing CPU it will start utilizing memory it will start utilizing that storage it will open up the ports it will also have ids process ids because we are creating threads and in turn it will create processes so there will be a system call out because application cannot access the resources directly so the operating system comes into the picture Now, let us go ahead and compile and see what happens. So for compiling, I'm going to use the command gcc. I'm just giving all the options. I'm giving the program name as threads underscore cpu.c. I'm asking it to output the compiled program in the name threads underscore cpu. perfect it did not give me any error messages so that I can understand that my code is correct now Sundar what is this hyphen ball hyphen vextra these all enable useful compiler warnings not only error messages even I want to know if this compiler can understand any warnings Warnings will actually work. It will function. But it is a warning for us that our code can be optimized. This hyphen pthread enables the appropriate threading compile and link support. So when this code is getting compiled by the GCC, I am also telling I want the pthread function and its details so that I can access it. I-O threads, as I said before, is going to get the output in this executable name. Okay, perfect. Now I can go ahead and run. What I'm going to do is I'm just going to run this thread CPU and enter. As you can see, there are six threads that have been started because of that for loop and I was using number of threads now you can see it is not in order when the threads are getting created it need not be in the order because once the thread is created once that particular thread functionality is completed it is going to give the output back to the function which has created the thread. That's all its job. Now, if the function needs to wait until all the six threads are completed, then it will wait. It will take the throughput. It will take the returning value from all the six threads and it will combine that and then it will go ahead with the processing. So I had given 60 seconds so now the thread 1 is finished you can see the counter that is 257 3915 but thread 5 also finished with the counter 256 415 1 and finally it says that all workers are completed. Now let me execute this again and let me go to my another thing. Here let me use ps-p in C. There is a sudo, there is a bash running here 22682 and ps is nothing but this particular commands process id 22682 perfect now i don't know what is this threats.cpus process id i can say pid is equal to dollar exclamatory if it is still running as it is still running now if i say echo pid it is showing that means here it might have finished So, I gave a, no, oops, it's not capturing, no problem, oh, it has completed. So, now let me run this in the background, and then let me capture it. How can I capture it? I can capture it by running the command pid dollar exclamatory, which is going to capture the thing. oops what did I do okay and enter now you can see it has come up and it is showing that this is the PID now let me go back here and then echo PID which is still PID okay no problem what is the pid here 23924 so it is it will run for 60 seconds so what i can do is i can give ps hyphen and then i can say hyphen p i have selected that and enter now you can see the command which has created these threads this threads underscore ppu that is our c program and this is the process id 23924 for the one of the six threads now like that if you look at it now you can see seven threads so what i said one main thread will create the another six threads the main thread will call out so now the process id that is holding this is sub process id for these things this is our process id of threads cpu and if you can see the process id is common for all the subthreads but subthreads process id are totally different and unique starts from 24 27 28 29 30 31 and 32 so one of these is the main thread and the rest is six of the sub threads now here all workers are completed okay perfectly fine so threats cpu is also done now let us run it again let me clear let us run it again and now let us see in the top command let this run here echo pid 23995 now let me go back here i'm just going to give a command top hyphen h this host hyphen p which process id i'm just going to say 23995 now you can see so this gives a better picture in terms of how much cpu percentage is being utilized by it and also how much memory this 33 if you look at it almost everything is similar out of the cpu that got allocated for this particular thread 33.6 percent or 33 percent 32.9% is being utilized. And what is the memory used? 0.1 percentage. So thread ID or LWD, identifier of an individual thread. The CPU utilization we saw and the memory utilization also. Now this has exited. How can I get to know? I'll go back here. All workers are completed. Whether the thread is currently running, runnable, sleeping, or in another state, subject to the display tool and platform. So, now let us see the behavior with H-top. Because we saw H-top as well. So, we can also see that with H-top command instead of the top command. so now let me again go back and then run this sorry let me run this and capture the process id now the process id is 24057 now i'm coming here oops now i'm coming here htop i'm just going to This is this host, then hyphen P, which process this one. So I'm just getting only that process in the H-top. Now you can see I have two CPUs, 0 and 1. Both the CPUs are at 100%. The load average is 3.63. Tasks is 56 overall running. And out of that, 2 is running. out of 47 threads two is running and if you look at here this is my master that is a parent thread which has created six sub threads now who has created this the root user and this is the primary now you can see all have become red because my processes are released almost all the processes were in the completing stage. So now if you look at it, there is only one which is running. This is from the whole system perspective, 41 threads. If you look at now, this has become 41 threads. It was 47 because we created six different threads. So now actively, now if I press this and if I don't give this, enter this, Now you can see there are 41 threads available here. Some with system D, some with message bus, some with crony, the time synchronization, some with syslog, some with the user root. Now the CPU, if you look at it, zero just now it is utilizing. And one, I do have two CPUs, zero and one. Memory, this much memory is free at this point of time. And uptime is 5 hours, as I told before. Load average is 1.27. Average is 2.26. And the current is 1.17. And the previous is 1.45. So it refreshes every 3 seconds, every 4 seconds. This gets refreshed. I would like to pass here. Let me stop sharing this. So we have seen process identification. And we have also seen the number of threads. Because of the thread, how much resource utilization. And also we saw the scheduling state of the thread. Why are there multiple thread entries for one process? Why might one thread show more CPU activity than another? Can threads execute simultaneously on multiple cores? These are all the questions that comes to our mind. Why are there multiple thread entries for one process? because we have requested to create multiple threads. And this is what exactly happens when we are writing an application using the code. Whether it is Java, whether it is Python, whether it is Scala, whether it is Spark, any language that provides threading facility, multi-threading, by default it acts on it. but if you want to take a control we will write the code accordingly. If you just wanted to leave it to the default behavior of the platform platform, essentially what I am trying to tell here is the language that we are using. That's the platform. If you are going to use Java, then JVM is going to execute your code. If you are going to use Python the Python platform is going to execute the Python 3. If you're writing Scala, then Scala will run your program. So those settings, default settings, will come into the picture. And it will automatically create multiple threads, basically to fasten the speed of the application. Because once you have received the user request, then you should give the response back to the user as soon as possible. that is where wherever it is possible we create multiple threads that means our workers are going to create multiple threads work and give it back to the process that has created the threads so that the process can rearrange its output maybe one thread is basically to check what is the data we have now another thread to check if there is a space another thread maybe from the input from the thread one if it is not available insert it if it is available update it or if the code says that irrespective of whether it is there you add a record all these things will use the threads number of threads we can control in our code If not, we can use the automatic or the default setting. Okay, I understand, Advait. Use echo $pid instead of echo $pid. The $ expands the variable. Thank you so much. I understand. Oh, that was my bad. Thank you very much, Advait. Great. at this point of time i would like to pause and check if there are any queries no i just stopped sharing the screen amit any queries any questions please let me know i you can type the question in the chat also i can i do okay i do see abhishek singh okay that is what i was trying to do i will increase the font size because some of you said thumbs up so i went ahead so no problem i will increase the font for my windows okay cool now let me look at okay i do see in akshi rai in akshi rai i have allowed you please go ahead yes actually it was using completely both the course and actually if you're talking then probably you're talking on mute okay cool guys is it only i'm not able to hear her or is it something that you're hearing her because i do see her mic is okay fine let me disable the mic probably she is not around i'm not sure okay minach she's some problem with my audio actually you did not unmute how i see yeah look okay now i see there is some issue with your mic okay fair enough not a problem you can type whatever is your question when actually i can stand it okay so no questions can i assume we are good from this particular explanation so if you're good please respond with a thumbs up to my message are we good is there any hard limit on number of threads we can create no there is no hard limit it is basically you know the resource availability if you have more cores because here I have assigned for this particular virtual machine only two cores there are two cores available so it is using two cores there when the CPU was at 100% why the VM did not crash when the CPU at 100% actually the CPU did not reach 100% before it reaches the 100% the function of the POSIX is basically to pause the thread is there any scheduling method on thread also like we are going to see that next Python program instead of C we are going to use a Python program and we are going to observe and if it is still not clear we will definitely and servers for MEP. Okay, cool. Great, guys. So the next is basically a code that will simulate PPU scheduling algorithms in Python. When we are seeing that, we will do that. So basically, we are going to do four processes. P1 to P4. and then we are also going to define the priorities and we are going to observe the CPU bust. So here our assumption is only one CPU but I have two coupes. So two CPUs, all processes will start at 0 minute, millisecond. Contact switch overhead is 8 node. contact switching is ignored and a lower priority number represents higher priority if I have not shared the screen yet I am going to share it I am just giving the assumptions good only thing is that when I gave RV good message I received only 7 responses out of 227 that is not motivating fair enough because when 227 is there we are good then at least there should be some 25% response is what I was expecting fair enough 15 is there good see the response I am asking not for my satisfaction you can definitely give a thumbs down if in case if it has not helped you to understand because end of the day it is my duty and my responsibility to give an example that will make you understand so by response that is what I will conclude if my example has given you a good understanding then it's good for you and if you are satisfied you should respond and it doesn't mean that you have to give only thumbs up if it is not clear give a thumbs down definitely I will give you the mic access so that I can clarify your question. Cool. So now, before getting into the Python example, where we are going to see Gantt chat as well, let's have a five-minute break. I'll just put a timer of five minutes. I'll not be able to pause the recording. Not able to. so that's the reason i'm just going to run the clock that will help us to basically understand how much time we have cool with the five minute break we will come back oops where is it i'm not able to locate okay let me create a new hi guys i'm back am i audible am i clear i hope you give me a thumbs up so that i can understand that i'm audible i'm clear okay great thank you let me stop sharing let me share my full screen now hope you are able to see my screen okay now let us let me go back clear the screen actually in the background it is still running let us not worry about that that it is not going to consume any resources only the background process that it is going to run it is not going to create the thread again because both the far loop is completed so i'm not killing that thread because i just wanted to convey that if a function is in the memory and it is running unless a function is activated resources are not going to be consumed okay great now let us look at the python program and then understand the output with TPA schedule and processes. So cat, what is my Python program? It is scheduler demo dot python. So it's going beyond the screen. No problem. So now if you look at the program what we are doing is from collections import DQ. What is this? It's a double-ended queue. DQ is nothing but double-ended queue. In this program, it represents the ready queue used by first-come-first services and routing problem. This matches the basic PFO behavior needed for both the algorithms. The operation append as well as pop left allows us to add processes at the end and remove the oldest process from the front. That is what it is going to help us. And we are defining the processes. PID is what P1. Whatever is the PID. For arrival is 0, burst is 7. And priority is 3. So as I said before, we are creating four processes with different priorities, 1, 2, 3, 4. And a different burst level, 7, 4, 1, 4. So each dictionary represents one process. This is one dictionary. This is defining one process with its information. Now, the quantum is equal to 2. This sets the round-robin time, quantum, to 2 milliseconds. The simulator uses milliseconds as its abstract unit of time. It is not measuring actual wall clock CPU execution. It is measuring actual consumption. Now, the next what we are trying to do here is, we are trying to define the simulation with policy and quantum is equal to quantum. So if I make this 5, it will use for my 5 milliseconds. So that is something which I can modify. So I've defined the variable and calling the variable value here. Now each invocation, the function accepts a scheduling policy and an optimal quantum. This simulate is going to simulate different methods, FCFS, SJF, Priority, and Round Robin. Now, let us see one by one. Each invocation starts a fresh simulation. So one algorithm executes. Execution does not affect another algorithm. that's the reason for processes now if you look at here I'm defining the jobs here I'm defining the jobs I'm allocating the values that has been defined in each dictionary enumerate processes provides the input position of each process and star p star star p copies the original process fields into a new dictionary order preserves the input order for the deterministic tie-breaking reminding tracks how much cpu time is still required based on the burst we have first start is none that records the first time the process executes once it executes this man will become the value and the completion records the process when process is finishing. Now, in the next, we are trying to create the arrival queue. Pending sorted jobs values and key lambda. What is this informing? This actually sorts processes by arrival time and by their original input order. That's what it is performing. Now, ready is equal to dequeue. Next arrival is equal to zero. Time is equal to zero and Gantt chart is null. Ready contains the processes that have arrived and are eligible for CPU service. Once it is ready only, the CPU will get allocated. And now, next arrival identifies the next process. We have seen it in detail. So it will be in the queue. The next process that has not yet been admitted to the ready queue. Time is the current simulated time. It is initialized as a zero. and the Gantt stores the execution intervals for later display. I'm just capturing the Gantt values as well. So the ReadyQ contains processes that are ready to execute. Not every process that exists in the system. A process waiting for Ivo, for example, would not be continuously eligible for CPU execution. So now, this while statement is going to start the execution, run the simulation. This loop continues until all processes have completion times. All the four processes, when it gets the completion time in order, until then, I want it to run. That's the condition I have given here. Now, this loop continues until all processes have completion times. The next block admits newly arrived processes. The next while loop. This actually allows the newly arrived processes. Perfect. Now, if you look at the next arrival, next arrival is lesser than length of pending and the pending next arrival arrival is lesser than or equal to time ready append and next arrival plus plus is equal to one the program checks the next arrival time and adds every process whose arrival time is lesser than or equal to to the the current simulated time. If no process is ready, that is what we are adding the code here. If no process is ready, the simulated CPU advances to the next arrival time. Instead of executing a non-existent process. This matters when using non-zero arrival times. We are using non-zero arrival time, so it matters. And the next one is basically to choose the next process, which is in ready. So if policy in FSFS, round robin, first come first serve basis, or round robin, PID is equal to ready.equalipt. For FCFS, the selected process runs until CPU bust completes. For round robin, the process runs for at most one quantum, whatever is the quantum we have defined. We have defined two here, at least two. And SJF, here we have, if it is not of FCFS or RR, then if the policy is SJF, SJF selects the process with the shortest CPU best among the ready processes if there is only one ready process then automatically the CPU is going to get allocated if there are two SJF processes are ready among the ready processes SJF is going to select the process with shortest CPU now the tie-breaking fields you can have same you can have same if there is going to be a tie-breaking results when two processes having equal burst length priority scheduling uses the same approach but selects the lowest numerical priority so we have given the burst arrival and the order And when it is ready, it removes the FPI. Now, if it is not FCFS, if it is not round robin, sorry, if it is not round robin, if it is not SJF, then we are checking if the policy is of a priority schedule. If the policy is of priority scheduling, then we are trying to give again the priority arrival and order. Here, if you look at it, SJF based on the burst, arrival, and order. But in case of priority, we are telling priority, arrival, and then order. So, the implementation of SJF and the priority here is non-preemptive. Once selected, the process completes its remaining CPU bust before the scheduler selects another process. If it is SJF. Now, what are we going to do next? we are going to record the first CPU execution time. Recording the first CPU execution time. Job of first start and job of first start is equal to the time. If it is not overwritten when the process executes again, this is necessary because response time is based on the first CPU service, not the last time the process resumes. Very important thing. Whenever the CPU is allocated to a process and the process is scheduled and it is executing, it is pretty much possible that a particular process can be passed because there could be another priority process that might get ready. so this process would be passed rarity would be executed and then the previous process which was passed will resume so we should not be calculating the resume time we should have the actual start time without which if you calculate the duration then we will not be able to see the actual output So that's why we are recording the first CPU start time here. And then we are determining the execution duration. Here, if you look at if policy is equal to RR, duration is equal to either minimum of either quantum or job of reminder. Whichever is minimum. That is what we are recording. Else, duration of job of reminder. If it is not round robin, then it is, we are directly taking the remaining so that our overall execution duration is perfect. Now, how are we doing that? We are doing with recording the interval start time. We are also taking advance the simulated clock. reducing the remaining CPU requirement that is what we are because we don't have to calculate the remaining record the interval in the Gantt chart so I am using this Gantt append command and for that particular PID for this ID record start and the duration for example if P1 executes from time 0 to time 2, the simulator will record the P1, process ID P1, whatever it is, and it will say start is 0 and the time is 2. This is how it is going to record. If P1 executes again later, the program adds another interval rather than combining the intervals. That is how the simulator shows preemption and subsequent resumption in round robin. Let us see if we are able to observe that. If any process got, let me increase my, that was one of the, okay, click settings, Let me increase to 12. Okay. Hope it is much more legible and clear. So now what I'm doing here is I was in 8. Complete the processor, return it to the ready queue. You are just checking if it is completed. After the CPU interval ends, the simulator admits processes that arrived during that interval, then it decides whether the selected process has completed or not. If job remaining is equal to is equal to zero, that is what we are assessing here, then job completion is equal to 10. Else if policy is equal to rr, ready.append. This line is going to append the pause and redemption for the round robin. Now, after that, we are calculating the performance metrics. The lines, turnaround, waiting, response, all are basically calculating the job completion minus job arrival. That is, T is equal to T minus A, completion time minus arrival time. The waiting period, if at all it is there, It is doing the turnaround minus job of first. W is equal to T minus B. And the response, first start minus job arrival. And we are appending these results with PID, completion, turnaround, waiting, and response time. So we are calculating the performance metrics here. and we are returning it to the Gantt and grade. Now, this produces a readable sequence of execution interval. The average matrix is calculated using the sum. So if you look at the sum function, it is actually calculating the average weighting, average turnaround and the average response. Now, it is actually, the same pattern is used for turnaround and response times as well, if you look at it. And it runs for all four policies. How? Here it is. Then, compare all four policies in the same work tool. For policy, I'm putting into a loop for all the four, display results of policy. This makes the comparison reproducible and avoids manually changing the process table for every algorithm. Cool. Now it's time for us to execute the simulator. Let me clear the screen. Now let me go ahead and execute by using Python 3 compiler. What I'm going to run is scheduler underscore demo.py. so now if you look at here for each process it is displaying for policy FCFS for policy FCFS for policy SJF for policy priority and round robin we have the Gantt charts if you look at 0 to 7 P1 7 to 11 P2 11 to 12 is p3 12 to 16 is p4 it also has the complete 7 milliseconds turnaround is also 7 milliseconds so the waiting is 0 response is immediate because waiting period is 0 now for the process 2 it is 11 but waiting was 7 milliseconds that's why the response is also 7 but turnaround was 11 milliseconds if you look at P4 waiting was 12, turnaround is 60 response is also 12 milliseconds so the exhibition time would be 4 milliseconds, now if it is SJF, we have the average waiting, turnaround and response. Now let us go back here. We have the Gantt chart for round robin also. If you look at the process 1 then it took 16 milliseconds to complete turnaround is 16 milliseconds waiting was 9 but the response was immediate. Irrespective of response being 0 that is immediate there had been a 9 millisecond waiting period but the turnaround is 16 so overall is 7 milliseconds is the process time and that is what is the average waiting period is the 7.25 average waiting turnaround for RR is 11.25 and average response is 2.75 so if you look at it The maximum response time is for P4, that is 5 milliseconds. But it has waited for 9. Total waiting period is 9. So the average, if you look at it, for waiting, turnaround, and response, it has calculated for all the four processes. But this grand chart also shows process 1 used between 0 to 2 seconds. Process 2 has used between 2 and 4. process 3 between 4 and 5 process 4 between 5 and 7 but again process 1 came used for 7 and 9 process 2 came back if you look at it quantum what we gave is 2 milliseconds so you can see it has used only for 2 milliseconds but the remaining is only 1 in the last that is process 1 So, it is used only for 15 to 16. So, 0 to 2, 2 to 4, 4 to 5, 5 to 7, 7 to 9, 9 to 11, 11 to 13, 13 to 15. Total is 16. Here it is, overall 16. But if you look at the P2, P2 completed when it is here. In this time period, this process is completed. Turnaround is also 11, but the rating is 7. That means out of this 11, it has waited for 7 milliseconds. Execution was only 4 milliseconds. So we can see that P2 here, 2 milliseconds. P2 here, 2 milliseconds. But it has to wait to get the CPU for 7 milliseconds in the round robin. But in the SJFs, if you look at it, this also 16 seconds. In priority also, overall is 16. But priority, if you look at it, P2 was 1, P4 was 2, P1 was 3, and P3 was 4. That is the priority we had given earlier. So in that order, only if you observe, it would have consumed the CPU. So waiting time, if you look at it, P2, waiting time is 0 immediate response. It required only 4 milliseconds to compute. P2 has used the CPU totally in the priority. Zero to four, only process two consumed the CPU tag. Then P4 was the next priority with two. If you look at it, P4, completion time, eight milliseconds, but out of which four is the waiting period and four is the response. So that means the first four is consumed by P2. the next to 4 is consumed by P4 now the third one comes into the queue is process 1 this process 1 completely took almost like 15 milliseconds but out of that 15 milliseconds it has to wait for 8 so response is also 8 so the response if you observe it has waited for 8 milliseconds the first response it got is after the 8 milliseconds and waiting is also totally only 8. So in the priority, the moment it gets the CPU time, it will execute. It will wait for the completion to assign the CPU for the next process. Here, I have not spent the course of the processes between, sorry, course of the CPU between the processes. Assuming that I have only two cores, I'm giving two cores to the whole process. Can I manage to split the CPU cores to the processes? Yes, we can. But coming back here, 15, it took only one millisecond to complete. But it has to, P3 has to wait for 15 seconds, milliseconds. It got a response for CPU to execute after it came into the queue. It took 15 milliseconds to get the CPU time. Waiting also is only 15. Its execution time is 1. Now P3 here, if you look at here, 1 milliseconds. That is what it took. It did not wait. because SJF is going to determine based on the burst that is available. So here P3 got the execution time first in terms of SJF because the execution completion time is only one millisecond. But in case of priority, P3 has got the CPU in the last. So it has only one millisecond. How we know? waited for 15 milliseconds and completed in the 16th millisecond. So only one millisecond process time. Actually, this matches our assumption. And this Gantt chart is the one which gives how. Sundar, one quick question. Can we make this as part of my application GUI? Yes, you can. like how I am capturing the details in the Python program. You can also write a Python program to capture your respective processes. And that is what exactly the testing team do. When they are writing code for their purposes, they are going to write code like what I have shown. They are not going to write a functional code. The developers are going to run the, sorry, create the functional code. And when testing team is going to create the code for themselves, especially for the performance testing, they'll create the code something like this. And in addition to this type of code, they will also have the values. The average waiting time, average turnaround, average response, CPU execution for process 3, CPU execution for process 1, the total duration. All those things, they will have a benchmark. And whatever is the outcome that is coming from the execution, they will match it with the benchmark. If it is lesser than or equivalent to the benchmark, then your code has succeeded. They will say, yes, we certify this. and the operations are going to monitor. It doesn't end there. The DevOps doesn't end there. Again, the operations are going to monitor this. When they are monitoring, they are going to monitor in the production. Okay. Okay. We have done it. We will monitor it. When the actual traffic comes into the picture, whether our code is utilizing the resources as expected or not. Now, this code, I return the same processes for four types. I can have a combination of this. I can also have a combination of this and Ops team will monitor. And when they identify that it is breaching, they are going to inform the DevOps team. Then the DevOps team is going to look at why it's happening in the testing environment. Because they have certified it, the testing team, they will again run it with that particular load. So from the performance, they will move towards load testing. And under the critical load, what is the performance? They'll start capturing and that feedback will go back to the developer. But at the end of the day, what we were trying to understand in this session is how the CPU scheduling method or process influencing the utilization of the CPU and how it controls the process, either based on priority or based on SJF or based on FCFS. In FCFS, our priority, it will allow for the process to complete, which is what we have observed. This is FCFS and this is priority. We observe that it is. But generally, we provide a round and robin and see if the response back to the customer is impacting or not. And this is how the troubleshooting is being done by either the production operations team or the DevOps team. So, notice that SJF gives the lowest average weighting. As far as the processes are concerned, here the average weighting is 7.5 in FCFS. In round robin, the average weighting is also 7.25, lesser than FCFS. and in the priority it is a 6.75 but SJF waiting period is 3.75 SJF turnaround is 7.75 priority turnaround is 10.75 the average and here FCF as first come first of basis is 11.5 and this is 11.25 in case of round robin. So SJF force in both. Response 3.75. Here it is 6.75. 2.75 is the response for the round robin. So the response that means every process gets a CPU at the earliest point of time. So what we are what we need to understand. Actually, this does not prove that SJF is always the best algorithm or that round-robin is always the most responsive. It does not mean that. But this result shows that for these four processes, round-robin is responsive, effective, and SJF proved to be the best algorithm for the weighting. The results depend on arrival times. Here, one thing what we have assumed is all the processes are arriving at the same zeroth millisecond. But in a practical scenario, the arrival time is not going to be the same for all the processes. It depends on the traffic. It depends on function. It depends on procedure. It depends on values. It depends on dependencies of the function to get ready for the resources. Too many things are going to be around. All those things we have assumed it to be same in this exercise. But why does P3 finish early under SJF? That's a question we need to ask ourselves. Actually, the results depend on the arrival times, CPU burst, scheduling rules, and time quantum, all the four. Now, if I have given time quantum as 5 instead of 2 milliseconds, then the CPU will be allotted for 5 milliseconds for that process. And if it finishes within 5, then it allocates to the next ready process, the queue. So do I need to adjust the quantum? Good question to ask. Why does P1 execute more than once under round robin? P1 has executed more than once. Started with 2 milliseconds. Then another 2 milliseconds. Then another 2 milliseconds. Then another once. Because the quantum is 2. If I have given quantum is equal to 5, then this entire Gantt chart, the entire averages would change. Let us quickly set that. Let me go ahead and let me edit. And instead of giving quantum as 2, let me make it as 4. Let me double it and then let me save it. Now let me clear. so that no confusions what I'm going to do is I'm just going to execute this program again now you can see the average response especially in the round robin instead of three times it has executed only two times for p1 and p2 p3 and p4 got executed in one attempt so the average response also has come down average turnaround also has come down and the average waiting period has also come down compared to the earlier one. So, now we can also observe the utilization by running a couple of Linux commands. So now, that is something that I'm going to give it as part of your exercise. And I want you to perform that and understand between the scheduling also. So now, what is that we have completed today? We created the process and we created the threads. We inspected the thread list. We observed the CPU activity. We also got the explanation on the scheduler's role and simulated the algorithm, that is scheduler demo. and we connected the results to engineering decision. What is the best to do in terms of method, in terms of quantum, in terms of bust? The combination will be arrived and then set in the system by the operations. Based on that, the developer is going to use the system parameters only. we are not going to write our code we are not going to define all these things in our code but what we are setting whether it is 2 milliseconds for the quantum whether it is the burst is 7 whether are we going to use round robin or FJT all depends on the performance of the load testing that the team does so you actually need to think about three things Which workloads should prioritize low response time? What could happen if the application creates too many worker threads? What measurements would you collect before recommending a scheduling-related change? There should be some clarity on the scheduling-related changes. Today, we have used a C program to observe real threads. Linux commands to inspect the CPU and Python to model a textbook scheduling algorithms. So the central observation is that scheduling is not merely about choosing which process runs next. That's not the case. But it is all about understanding the workload and balancing responsiveness, throughput, fairness and resource efficiency. Suppose if you see that out of six cores we have, only five cores are being utilized. Also, we need to address. If the resource is available but not being used by the application, also need to be addressed. It can happen. It can happen if any of the cores are reserved for any of the applications or the profit. Best example is your own laptop. your system function is prioritized and it will use an allocated CPU core and that is the reason you are seeing even if your desktop or laptop is not responding you are able to see the process information you are able to close any of the application that it is not responding or it is using high utilization For example, one of the browsers is using almost like 85% of the CPU. If you want to kill that process, you are able to kill it because the system processes are prioritized. It has dedicated resources. Applications do not have dedicated resources. It will take the resources whenever it is required. So if you are going to observe that one core is not being used by the application, then you should understand that maybe it is reserved for the system. When I say system, I refer to the operating system because end of the day, operating system is the one which is providing the resources required for the application. So it needs to have some resources to work because operating system is also an application. So, the operating system will have a dedicated resource. If you are speaking about CPU, then dedicated CPU, maybe one core, half core, quarter core, whatever it is, it will be reserved. And that is what is actually allowing you to observe the application behavior. And if it need be, you are able to stop the application or you are able to end the running application. So now, what we have understood, we have understood the workload and balancing, responsiveness, throughput, fairness, and resource efficiency. I'd like to pass here and check if there are any queries, questions. Don't worry about the commands which I have executed. That will be available in the PDF that I'm going to upload. And don't worry about the program. i will also ensure that it is available in the feed we are talking about something scala that is used for high computation distributed system and petabytes of data so can we know the how ada system are designed and computational efficient architecture profillah kumar i'll take this question i will definitely answer it tomorrow when we connect yeah thank you very much for asking let me note it down and then i will also syllabus for quiz one i have already uploaded but this, sorry, syllabus for phase one, right? Okay, I thought assignment two. It will include process and OS system calls process and CPU utilization. Whatever we are covering, whatever we have covered until today, what we are going to see tomorrow is not going to be part of the phase one. Okay, cool. Every time do we need to compare scheduling algorithms for each workload like this? No, what I have displayed is basically for our understanding purpose, the same workload in multiple method. But generally the testing team will put it across in this manner, multiple workloads, multiple methods and see which is the best and then they'll decide. Can we say SJF is the best algorithm? I said, not really. It depends on your test case. When are you uploading quiz one? It will be uploaded maybe by tomorrow evening after our class. Hope you guys understand that it is a test season and as well as... I'll try to upload by tomorrow night itself. So that is available for you from Monday. Will end-time exam have all syllabus or things after the mid-term exam? Okay, there will be a, you know, threshold. Whatever has been completed in the mid, will it appear in the final? Yes, it will. but the number of questions will differ there will be a lower threshold in terms of if there are 10 questions 3 from probably 3 or 4 from what we have covered in the midstream but rest would be from the threshold but to answer you yes it is whole anything else from anyone before we conclude our session today ok guys thank you very much have a great evening see you tomorrow take care bye bye