Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

Programming Assignment 2

CS450 Spring, 2023

trace the read()system call

a). start read(fd,10,n)  system call  Track the system call process of  read ()

The first step is to use the read call, write a short program () to call the xv6 system call. where fd is an invalid file descriptor.This will then invoke a function defined in user/usys.S

 

The operation result returns -1, indicating that there is an error

 

The assembly code segment in user/usys.S will be executed. This assembly code is dynamically generated by executing compilation from user/user.h and user/usys.pl.

 

li a7, SYS_read means to assign the call number of read to the a7 register, and SYS_read is an immediate value. It is defined in kernel/syscall.h  kernel/syscall.h

 

The execution of the ecall assembly instruction (see user/usys.S) will cause the process to fall into the kernel state (supervised mode); when falling into the kernel state, the assembly code in the      uservec section in kernel/trampoline.S will be executed.

 

Usertrap() will be executed Executed

The usertrap() function (see kernel/trap.c) handles all traps caused by user space. There are three possibilities - system calls, interrupts caused by devices, and exceptions. Finally it calls syscall();

 

syscall() first obtains the system call number 5 (which has been stored in p->trapframe->a7 in step 2), and then finds the processing function sys_read() corresponding to the system call;

// Use num to lookup the system call function for num, call it,and store its return value in p->trapframe->a0

Syscall

 

 

find the system call

 

If fd is defined, that will return -1, and record error code in the a0 register, return to the application

 

myproc() is a function that returns a pointer to the process   structure of the current process, and ofile is an array of         pointers to structure file objects, which represent open files. Thus, if f is zero, it means that there is no open file                  corresponding to the specified file descriptor.


Call Sys_read

 

Argfd() will be call , it checks if the file is legitimate the sys_read  function is defined in the

kernel/sysfile.c file.

 

If there is no problem with the check, will call the fileread() function, in the file.c file

 

2- 1) The program that calls alsoNice() will have its time slice increased to n times

Add a system call

Implementation ideas

To implement the alsosonice(int n) function, its function is to increase the time slice of the process.

We need to add a new attribute in the data structure of the process, which represents the multiple of the time section  we increase. This parameter will be initialized when calling the alsonice() function, and we will modify the process data. In Xv6, each time a time slice is completed, yield() will release the cup and trigger a schedule.

Now after the modification, we do not execute yield immediately after executing the process time slice, but subtract the time slice owned by the current process. Until the time slice of the current thread is executed, then decide whether to    schedule.

We register alsosonice syscall in suer location

User/ usys.pl

 

User/user.h

 

Kernel/Syscall :add code

 

Kernel /Sysproc.c

Take out the parameters and pass them to the alsonice function

 

Kernel /proc.c

The timeslice we assign to the current process, the counter is used to determine whether the current timeslice has been used up.

 

Kernel /proc.c  procdump()

Add print attribute information to procdump for tracking process information

 

Kernel/trap.c

We have expanded the time slice of the thread. If the time slice of the current thread is greater than 0, continue to run the current thread and subtract 1 from the time slice....

 

test case design

I use fork() to create three sub -processes, each sub-process sets a different time slice, and the operating system scheduler will allocate CPU time for each process in turn, allowing them to execute in parallel.      And the amount of tasks for each child process is the same using the same number of for loops.

In the parent process, we use a loop and a function to wait for all three child processes to complete     wait(). Each call to wait() blocks the parent process until one of the child processes terminates, allowing the parent process to output completion messages for each child process in the order in which they    completed

 

We run three processes and choose different slicetimes. We will find that the larger the slicetime, the earlier the    execution will be completed, because the larger slicetime will get more opportunities to run the cup, and all will be completed earlier.

 

 

If we comment out alsosonice() in each process, after running, we find that the running time of each child process is the same

 

 

control+p we can see that the time slice is decreasing

2-2) Realize the execution of programs with different time slices, LTSF and STSF

Added a attributes of process data structure

We increase one of property

 

Initialization data

 

 

the scheduler() modifies

For detailed code, please refer to the scheduler() function in proc_STSF.c and proc_LTSF.c files in the file

Large slicetime-first code

 

 

1.Declare three more

variables

2.Find the process with the   largest slicetime in the array, find this process in the loop  and execute it firs

3. The slicetime of the current process can only be run if it is  greater than or equal to the     maximum slicetime (priority)   in the currently runnable state process. If it is negative, it will keep looping to find the target process.

Test case

cpGck n2GL\c2.c L!lG LoL qG本g!l2

 

2本gL本 3 2np-bLocG22G2、

2G本 q!LLGLGU本 2l!cG本!山G (bL!oL!本入) LoL Ggcp bLocG22、

2lGGb LoL Mo 2GcoUq2 LoL Ggcp bLocG22 o GU2nLG pg本 pG bLocG22G2 GU本GL pG bLocG22 gLLg入 g本 pG 2g山G

!山G、2o g2 o GU2nLG 2nccG22Lnl 2cpGqnl!U8

L!Ugll入 bL!U本 pG LnUU!U8 !山G.


STSF  (shortest time slice first)  running results

LTSF (largest time slice first) running results

 

 

This outputs the priority of different processes

 

Data Analysis Description :

The amount of running code is 2,000,000,000 assignment operations, all threads are the same, you can refer to the c5.c file

TimeSlice(priority)

RR default

LTSF

STSF

2

139

33

41

10

133

51

59

50

125

71

77

Comparing the data of LTSF and STSF, we can conclude that

For this test case, all LTSF processes are faster than STSF, because the large TimeSlice task gets    the cpu first, and runs for more time at a time, without frequent competition for the cpu, and the competition for the cup will also run time, and the consumption can be seen from those The running time ofThere are advantages to running small tasks first, we can observe the running results first, and the small tasks are executed first, because small tasks only need a short running time.Overall runtime LTSF is faster than STSF.

Both LTSF and STSF run longer than default because default has too much competition.

Observing the default RR running data, we can find that a process with a large timeslice runs faster, because if a large timeslice gets cpu to run, it will continue to execute until the timeslice is 0, which helps it complete the task faster, while timeslice 1 needs to re-compete for the cup after each run.

By default, xv6 RR has no running time for priority scheduling, but simple rotation training