Operating system experiment 1. Familiar with the experimental environment - Linux (including code and experimental experience)

Implementation tool: PC

Implementation environment: Linux

Internship content (function, goal):

Experimental requirements:
1. Familiar with the basic installation and configuration of linux operating system; understand the startup process and desktop environment of linux operating system; master the use of VirtualBox virtual machine
2. Understand the command format of Linux, master the operating commands in Linux; learn to use various Shell commands to operate Linux; learn how to get help information
3. Familiar with the file and directory structure of the Linux file system, master the basic characteristics of the Linux file system; master the file operation commands in the command line mode and the use of file operation functions in the program; master the loading and unloading methods of the Linux file system
4. Understand the basic usage of editing text files with the vi editor; skillfully use the vi editor for quick document editing
Experimental goals:
(1) Deepen the understanding of the concept of process, and clarify the difference between process and procedure.
(2) Further understand the essence of concurrent execution.
(3) Analyze the phenomenon of processes competing for resources, and learn how to solve the mutual exclusion of processes.

Implemented ideas, methods and technologies (including data structures and algorithms):

Design ideas:
1. Linux installation and configuration:
2. Linux basic environment and use: Use the man command to obtain Linux commands such as ls, uname, date, cal, mkdir, cp, etc., and understand the specific usage methods of these commands
3. Linux file processing displays the file names of all hidden files in the user's home directory, lists the executed commands and output results
4. Use of vi editor Open the file with vi editor, perform the following operations, and describe the operation process and methods in detail
Usage of fork():
(1) A parent process wants to copy itself, and the child processes of the parent process execute different code segments at the same time. This is most common in network service processes - the parent process waits for service requests from clients. When such a request arrives, the parent process calls fork to make the child process process the request, and the parent process continues to wait for the next service request. (2) A process needs to execute a different program. This is the most common case for shells. In this case, the child process calls exec immediately after returning from fork.

Main code:

#include<stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdlib.h>
 
int main(){
        pid_t pid;
        pid=fork();
        if(pid<0)       /* 如果出错 */
                printf("error occurred!\n");
        else if(pid==0) /* 如果是子进程 */
                {
                        printf("我是子进程1,进程号是%d\n",getpid());
                        pid_t pid1;
                        pid1 = fork();
                        if(pid1==0){
                                printf("我是子进程1的子进程,进程号是%d\n",getpid());
                        }
                }
        else{
                printf("我是父进程,进程号是%d\n",getpid());
                pid=fork();
                if(pid==0)
                        printf("我是子进程2,进程号是%d\n",getpid());
                else{
                        pid=fork();
                if(pid==0)
                        printf("我是子进程3,进程号是%d\n",getpid());
                }
        }
        return 0;
}

Notes:

1. The process using fork() is the parent process, and the new process created by fork() is the child process.

2. Fork() is created successfully, the return value is 0 for the child process, and the pid (a positive integer) of the child process for the parent process.

3. After calling fork(), both the parent process and the child process start executing from the next statement.

4. getpid() returns the process identifier of the calling process, then the k of the output child process is equal to the i, j of the parent process.

Result analysis:

Screenshot of the experiment:

Experiment experience:

(1) Deepen the understanding of the concept of process, and clarify the difference between process and procedure.

(2) Further understand the essence of concurrent execution.

(3) Analyze the phenomenon of processes competing for resources, and learn how to solve the mutual exclusion of processes.

(4) Review the content of the textbook on process control and process synchronization to deepen the understanding of the concept of process management.

(5) Carefully read the process management part of the experimental materials, and analyze the operation of multiple processes. 

If you find it useful, please like, comment, share and favorite, thank you very much, good people have a safe life! ! !

I wish you all a happy day, sprinkle flowers❀❀❀❀❀❀*★,°*:.☆( ̄▽ ̄)/$:*.°★*.

Guess you like

Origin blog.csdn.net/m0_50962679/article/details/123600292
Recommended