"Introduction to Operating System" Experiment One: CPU Virtualization (Process)

Simulation Homework: Simulation process state transition

1. Job package: process-run.py

2. Operation description:Insert picture description here

3. Operation requirements:

(1) Run process-run.py with the following flags: -l5: 100,5: 100.
Parameters -l PROCESS_LIST: comma-separated list of processes, in the form X1:Y1,X2:Y2,..., where X represents the number of process instructions and Y (0 ~ 100) represents the running instructions Still declare that the I / O request
Insert picture description hereInsert picture description here
the command to run process 0, run 5 instructions, and the CPU occupancy rate of each instruction is 100%. At this time, process 1 is in a ready state, process 0 is completed, and CPU runs process 1, performing the same operation , The total time is 10 units of time.

(2) Now run with these flags: ./process-run.py -l 4: 100,1: 0.
Insert picture description here
This command runs process 0, runs 4 instructions, and the CPU usage of each instruction is 100%. Process 1 issues an I / O request, the CPU blocks the process, I / O executes for 3 units of time, the CPU releases the blocked state of process 1, and process 1 performs the completion operation, which takes 10 units of time.

(3) Switch the order of the processes: -l 1: 0,4: 100.
Insert picture description here
This command runs process 0 and issues I / O requests. The CPU blocks the process and performs I / O operations. At the same time, the CPU runs process 1 and runs 4 instructions, and the CPU occupancy rate of each instruction is 100%. After 4 units of time, I / O operations and process 1 are completed, and process 0 executes the completed operation, which takes 6 units of time.

(4) The SWITCH_ON_END parameter will cause the CPU to wait for the completion of the I / O operation.
Insert picture description here
The SWITCH_ON_END parameter will cause the CPU to wait for the completion of the I / O operation.
This command runs process 0 to issue an I / O request. The CPU blocks the process and executes I / O operations for 4 unit times. Then process 0 executes the completion operation. At the same time, the CPU runs the process 1 instructions and runs 4 unit times. 9 unit time.

(5) The SWITCH_ON_IO parameter will make the CPU run the process at the same time during I / O operation, which is the default operation of the simulator, and the result is consistent with (3)

(6) (7) IO_RUN_IMMEDIATE parameter VS IO_RUN_LATER parameter
To enhance contrast, reduce I / O requests to 2 and CPU process to 1.
Insert picture description here
The difference between the two is whether the CPU accepts the next I / O request or executes the process instruction after the execution of the I / O operation. Since the default mode SWITCH_ON_IO is used, the CPU executes instructions and I / O operations simultaneously, so IO_RUN_IMMEDIATE is one unit time faster.

(8) The topic is the comprehensive use of the above commands, not much improvement, and no action.

Code Homework: Use of Process API

(1) Write a program to call fork (). Before calling fork (), the main program uses and sets the value of the variable x. What is the value of x in the subroutine? What happens when the value of x is modified in the parent program and the child program at the same time?

// p1.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>

int main() {
  int x = 0;
  printf("father x is %d\n", x);	
  
  int rc = fork();
  if (rc < 0) {
    fprintf(stderr, "fork failed\n");
    exit(-1);
  } else if (rc == 0) {
    printf("child x is %d\n", x);	// 子进程
    x = 10;
    printf("child x has changed to %d\n", x);
    
  } else {							// 父进程
    x = 100;
    printf("father x has changed to %d\n", x);
  }
  exit(1);

  return 0;
}

Insert picture description here
It can be seen that the initial value of x of the parent process and the child process are both 0, and change to 100 and 10 in their respective processes, and the variables do not affect each other.

(2) Write a process to open a file with open () and call fork () to create a new process. Can both parent and child processes access the file through open ()? What happens when they are written to the file at the same time?

function parameter
open O_RDONLY read-only mode, O_WRONLY write-only mode, O_RDWR read-write mode
read(int fd, void *buf, size_t count) Read the file name fd, read the memory space name buff, read the number of bytes count
write (int fd,const void * buf,size_t count) Write file name fd, read memory space name buff, read byte count
// p2.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>

int main() {
  int fd = open("./p2.rtf", O_RDWR);
  
  char buff[20];
  int rc = fork();
  if (rc < 0) {
    fprintf(stderr, "fork failed\n");
    exit(-1);   
  } else if (rc == 0) {
    read(fd,buff,20);
    printf("child process read:%s\n", buff);
    write(fd, "child process write ", 20);
  } else {
    read(fd,buff,20);
    printf("father process read:%s\n", buff);
    write(fd, "father process write\n", 20);
  }
  exit(1);
  
  return 0;
}

Insert picture description here
It can be seen that only the parent process successfully reads the contents of the file, but both the parent process and the child process successfully write to the file.

(3) Write another program that uses fork (). The child process should print "hello"; the parent process should print "goodbye". The program guarantees to print the contents of the child process first, and does not use wait () in the parent process.

// p3.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main() {
  
  int rc = fork();
  if (rc < 0) {
    fprintf(stderr, "fork failed\n");
    exit(-1);   
  } else if (rc == 0) {  // 第1个子进程
    printf("hello\n");
  } else {
    int id = fork();     // 第2个子进程
    if (id == 0) printf("goodbye\n");
  }
  exit(1);
  
  return 0;
}

Insert picture description here
The main program main () calls fork () to create child process 1 output hello, and then calls fork () again to create child process 2 output in the parent process goodbye. The parent process and the child process are executed at the same time. From the perspective of the process tree, the child process 2 is in a deeper position than the child process 1, and the execution time is longer, so that the output is guaranteed first hello.

(4) Write a program call fork()and exec()function cluster overload program /bin/ls, and analyze the exec()difference between function clusters.

#include <stdio.h>   // fprintf printf stderr
#include <stdlib.h>  // exit()
#include <unistd.h>  // fork() execvp()
#include <string.h>  // strdup()
#include <fcntl.h>   // file control options

int main(int argc, char *argv[]) {
  int rc = fork();
  if (rc < 0) {
    fprintf(stderr, "fork failed");
    exit(-1);
  } else if (rc == 0) {
    printf("child process called\n");

    char *myargs[2];
    myargs[0] = strdup("ls");      // program:"ls"
    myargs[1] = NULL;              // mark end of array
    execvp(myargs[0], myargs);
    printf("this shouldn't print out");
  } else {
    int wc = wait(NULL);
    printf("father process called\n");
  }

  return 0;
}

Insert picture description here
Successfully call the fork()create subprocess, and run the lsprogram to display the files in the current desktop working directory.

(5) (6) wait()What will the writing program call in the parent process return? What happens if it is called in a child process wait()? How to use waitpid()it to play wait()its role?
The parent process call wait()returns the PID of the child process, the child process call wait(), because there is no process that can wait for the end, all errors return -1. waitpid()The prototype of the function is waitpid(pid_t pid,int *status,int options), set pid to -1 and options to 0, then the waitpid()function is completely reduced to a wait()function.

(7) Write a program to create a subprocess, what effect STDOUT_FILENOwill it printf()have on turning off standard output in the subprocess ?

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <assert.h>
#include <sys/wait.h>

int
main(int argc, char *argv[])
{
    int rc = fork();
    if (rc < 0) {
      fprintf(stderr, "fork failed\n");
      exit(1);
    } else if (rc == 0) {
      close(STDOUT_FILENO); 
      printf("child process called\n");
    } else {
      printf("father process called\n");
    }
    return 0;
}

Insert picture description here
It can be seen that only the printf()statements of the parent process are output, and the printf()statements of the child process can STDOUT_FILENOnot be output because they are closed .

(8) Write a program to create two child processes, and use pipe()the standard output of one process as the standard input of the other process.
Come back after learning the relevant knowledge of the pipeline. . .

Published 21 original articles · praised 8 · visits 1495

Guess you like

Origin blog.csdn.net/K_Xin/article/details/104679401