OS hw1

1 Further study on process switching

Timer Interrupt is a non-cooperative approach to enforce process scheduling. To achieve this, the operating system relies on a hardware timer to generate periodic interrupts, which trigger process switching.
When the timer interrupt occurs, the currently running process is halted, and control is transferred to the OS (kernel). And the scheduler determines whether the current process should continue or if another process should run. If a new process is selected, the OS saves the context (registers, program counter, stack pointer) of the current process and loads the context of the next process.

2 Basic concepts of terminal and shell

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

I use tmux to seperate the terminal.
2. The process is managed by the OS, not the terminal itself. Therefore, we can kill any process run on the OS in different terminal.

3 Learning from the Linux man pages

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

The third line can reflect individual CPU state percentages.
As a default, percentages for these individual categories are displayed. Depending on your kernel version, the st field may not be shown.

  • us: is meaning of user CPU time, such as applications. When us is high, it indicates that user programs are heavily utilizing the CPU.
  • sy: is meaning of system CPU time, such as system calls and drivers. When sy is high, it means that system calls are consuming a significant portion of CPU time.
  • id: is meaning of idle. When id is low, it suggests that the CPU is under high load with little to no idle time.

4 I/O and system calls

#include <stdio.h> #include <unistd.h> #include <assert.h> #include <fcntl.h> #include <sys/stat.h> #include <sys/types.h> #include <errno.h> int main(int argc, char *argv[]) { int fd = open("/tmp/file", O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, S_IRUSR | S_IWUSR); if(fd < 0) printf("The error number is %d\n", errno); assert(fd >= 0); int rc = write(fd, "hello world\n", 13); assert(rc == 13); fsync(fd); close(fd); return 0; }

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

Screenshot showing that the error happended.
We can add the O_EXCL flag to trigger the error happened when the file already exists.
According to the manual:
Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

Which tell us if the pathname already exists, the open() fails with the error EEXIST. Therefore we successfully make the error happened.

Setting up xv6

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →