[Linux] Von Neumann Architecture && Operating System && Process Concept

Table of contents

1. Von Neumann Architecture

2. Operating system

 1. Concept

 2. The purpose of designing the OS

3. Process

 1. Basic concepts

 2. Describe the process-PCB

 3. Organization process

 4. View process and terminate

 5. Obtain the process identifier through the system call

 6. Create a process through a system call - fork

 7. Process status

 8. Special process

   8.1 Zombie process

   8.2 Orphan Processes

 


1. Von Neumann Architecture

Our common computers, such as notebooks. Most of our uncommon computers, such as servers, obey the von Neumann system.

The following is a diagram of the von Neumann architecture:

  The computers we know are composed of input devices , memory , arithmetic units , controllers , and output devices .

  • Input unit: including keyboard, mouse, scanner, tablet, network card, disk, etc.;
  • Central Processing Unit (CPU): Contains calculators and controllers, etc.;
  • Output unit: display, network card, printer, etc.

  A few points must be emphasized about von Neumann:

  • The storage here refers to the memory;
  • Regardless of the cache situation, the CPU here can and can only read and write memory, and cannot access peripherals (input or output devices);
  • Peripherals (input or output devices) can only be written to or read from memory if they want to input or output data;
  • In a word, all devices can only deal directly with memory .

  Our data needs to be loaded from the disk into the memory first, then read and calculated by the CPU, load the calculated results into the memory again, and finally write the data to the disk from the memory, and give the data to us through the output device.

  Why can't the CPU directly access the peripherals?

  Because input and output devices are called peripherals, peripherals are generally very slow, such as disks, compared to memory, its speed is very slow, but the calculation speed of the CPU is indeed very fast. It's like the reading speed from the disk is very slow, but the calculation speed of the CPU is very fast, but the overall speed is still dominated by the reading speed of the disk, so the overall efficiency is dominated by the outside.

  The understanding of von Neumann should not stop at the concept, but go deep into the understanding of software data flow. Please explain, from the moment you log in to QQ and start chatting with a friend, the process of data flow. From the time you open the window, start sending him a message, to the data flow process after he receives the message. What if the file is sent on qq?

   First read the information from the keyboard and load it into the memory , then send the data from the memory to the output device (network card) through a series of operations, and then send the data to the friend's input device (network card) through a series of network operations, The friend's computer reads the data from the input device to the memory , and then sends the information to the friend's computer through the output device (display).

2. Operating system

 1. Concept

  Any computer system contains a basic collection of programs called an operating system (OS). Generally speaking, the operating system includes:

  • Kernel (process management, memory management, file management, driver management);
  • Other programs (such as function libraries, shell programs, etc.).

  An operating system is a piece of software that manages hardware and software resources. Why does the operating system manage software and hardware?

  Because the operating system needs to manage software and hardware resources well, and needs to provide users with a good (safe, stable, efficient, feature-rich, etc.) execution environment.

 The essence of operating system management : first describe, then organize .

  • Description: describe various data through the struct structure;
  • Organization : Organize and manage data through efficient data structures such as linked lists.

  In a computer, the operating system is equivalent to our manager , the hardware driver is equivalent to our executor , and the software is our managed .

  First of all, the operating system does not trust anyone . Just as we are bank users, we often go to the bank to save money, but do the banks trust us? In order to avoid malicious destruction by some of the users and cause harm to the operating system, the operating system does not expose all its functions but uses system calls to access the operating system. Because the cost of using system calls may be high, some people then carried out secondary software development on this basis, resulting in a  graphical interface  , shell and tool set .

   System calls and library functions 

  • From the perspective of development, the operating system will appear as a whole to the outside world, but it will expose part of its interface for upper-level development. This part of the interface provided by the operating system is called a system call;
  • In the use of system calls, the functions are relatively basic, and the requirements for users are relatively high. Therefore, interested developers can properly encapsulate some system calls to form a library. With a library, it is very beneficial for higher-level users or Developers carry out secondary development.

 2. The purpose of designing the OS

  • Interact with hardware and manage all hardware and software resources;
  • Provide a good execution environment for user programs (applications).

  The structure of the computer system is layered, and it is generally not possible to skip a certain layer

3. Process

 1. Basic concepts

  • Textbook concepts: an execution instance of a program, a program being executed, etc.;
  • Kernel point of view: Acts as the entity that allocates system resources (CPU time, memory).

 When we open the task manager, we will find that these running executable files are all processes.

 2. Describe the process-PCB

  • Process information is placed in a data structure called a process control block , which can be understood as a collection of process attributes ;
  • The textbook is called PCB (process control block), and the PCB under the Linux operating system is: task_struct

 A type of task_struct-PCB

  • The structure describing the process in Linux is called task_struct;
  • task_struct is a data structure of the Linux kernel that is loaded into RAM (memory) and contains process information.

  When the operating system first describes our process and then organizes it, it will first create a structure with the common attributes of our program, and then create a structure object for each of our processes. This is the process described first. Next, our operating system will use characteristic data structures (such as linked lists) to organize our structure objects, which is the post-organization process. Then our operating system's management of processes will be converted into the management of specific data structures.

   Therefore, process = kernel data structure related to the process + code and data of the current process.

 task_ struct content classification

  • Identifier: Describe the unique identifier of this process, which is used to distinguish other processes;
  • Status: task status, exit code, exit signal, etc.;
  • Priority: Priority relative to other processes;
  • Program counter: the address of the next instruction to be executed in the program;
  • Memory pointers: including pointers to program code and process-related data, as well as pointers to memory blocks shared with other processes;
  • Context data: the data in the registers of the processor when the process is executed [example of suspension of studies, to add pictures of CPU, registers];
  • I/O status information: including displayed I/O requests, I/O devices allocated to the process and a list of files used by the process;
  • Billing information: may include sum of processor time, sum of clocks used, time limit, billing account number, etc.;
  • other information.

 3. Organization process

It can be found in the kernel source code. All processes running in the system are stored in the kernel in the form of task_struct linked list.

 4. View process and terminate

To view the basic information of a process, we can use the command ps -axj to list the process information used by the current system;

First test a piece of code:

#include<stdio.h>  
#include<unistd.h>
int main()
{
   while(1)
   {
       printf("我是一个进程!\n");
       sleep(1);
   }
   return 0;
} 

Enter the command to view the process, as follows: 

Enter ps -axj | head -1 && ps -axj | grep "test" to get the header and the process with test.

  Here we also need to explain a concept that is the concept of PID and PPID, which refers to the process identification number in the operating system, that is, the process identifier . Every time a program is opened in the operating system, a process ID, or PID, is created. Of course, PPID is the process ID number of the parent process.

  kill process

There are two methods: the first is Ctrl + c to forcibly end the process, and the second is: in the form of command, kill -9 PID, specify the target process to kill.

  Here I recommend using the second method.

 5. Obtain the process identifier through the system call

  • process-id (PID);
  • Parent process id (PPID).
#include<iostream>
#include<sys/types.h>
#include<unistd.h>
using namespace std;

int main()
{
	pid_t t = fork();
	if (t == 0)
	{
		while (1)
		{
			cout << "我是一个子进程" << " pid:" << getpid() << " ppid:" << getppid() << endl;
			sleep(1);
		}
	}
	else if (t > 0)
	{
		while (1)
		{
			cout << "我是一个父进程" << " pid:" << getpid() << " ppid:" << getppid() << endl;
			sleep(1);
		}
	}
	return 0;
}

 6. Create a process through a system call - fork

  • Run man fork to know fork;
  • fork has two return values;
  • Parent-child process code sharing, each open up space for data, a private copy (using copy-on-write);
  • After fork, if is usually used for branching.

  You can see that the fork() function is very special. After successfully creating a child process, there are actually two return values. It returns the pid of the child process to the parent process, returns 0 to the child process, and returns -1 if the creation fails. 

  How to understand the two return values

When fork() wants to return the value, in fact, the work of creating a child process inside the function has been completed. At this time, there are already two processes, and the two processes continue to execute the following statement. After executing fork(), naturally There will be a return value, so it seems to us that there are two return values. In fact, when we receive the return value, we have triggered the copy-on-write (realistic copy is when the operating system detects that the child process has a write operation. The operating system will allocate corresponding physical space to the child process), and the seemingly identical rets are actually stored in different spaces.

 7. Process status

  • R running status (running): It does not mean that the process must be running, it indicates that the process is either running or in the run queue;
  • S sleeping state (sleeping): means that the process is waiting for the event to complete (sleep here is sometimes called interruptible sleep (interruptible sleep));
  • D disk sleep state (Disk sleep) is sometimes called uninterruptible sleep state (uninterruptible sleep), the process in this state usually waits for the end of IO;
  • T stop state (stopped): A process can be stopped (T) by sending a SIGSTOP signal to the process. The suspended process can be resumed by sending the SIGCONT signal;
  • X dead state (dead): This state is just a return state, you will not see this state in the task list.

The ps -axj command is to view the information of the process

  (1) R running status (running): It does not mean that the process must be running, it indicates that the process is either running or in the running queue; 

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
int main()
{
	while (1)
	{
		;
	}
	return 0;
}

When running, check the status of the process is R+, indicating that it is running.

 What does the + sign after it mean?

Here + means that the process is running in the foreground. When we use ctrl+c, the process can be terminated. If we do not write +, it means that the process is running in the background. At this time, ctrl+c cannot terminate the program. Use Command kill process to terminate.

  (2) S sleeping state (sleeping): means that the process is waiting for the event to complete (sleep here is sometimes called interruptible sleep (interruptible sleep));

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>

int main()
{
	int n = 0;
	scanf("%d", &n);
	return 0;
}

When running, check that the status of the process is S+, indicating that it is in a sleep state.

 (3) T stop state (stopped): The (T) process can be stopped by sending a SIGSTOP signal to the process. The suspended process can be resumed by sending the SIGCONT signal;

kill -l: All signals can be displayed, and the SIGSTOP signal corresponds to 19, and the SIGCONT signal corresponds to 18.

When we enter kill -19 PID, the process will be stopped.

When we enter kill -18 PID, the process will be restored.

  (4) Death state: This state is just a return state, and it is instantaneous. You may not see this state in the task list, because a process will become a zombie process after it dies.

  (5) Zombie state: the state after the process dies. After a process dies, it will be in the zombie state. If its parent process does not recycle, it will always occupy resources and cause memory leaks.

 8. Special process

   8.1 Zombie process

  • Zombie state (Zombies) is a special state. A zombie process is created when a process exits and the parent process (using the wait() system call) does not read the return code from the child process exit.
  • A zombie process will remain in the process table in a terminated state, and will keep waiting for the parent process to read the exit status code.
  • As long as the child process exits, the parent process is still running, but the parent process does not read the state of the child process, and the child process enters the Z state

Let's demonstrate the zombie process:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>

int main()
{
	pid_t id = fork();
	if (id == 0)
	{
		//子进程
		while (1)
		{
			printf("我是子进程,我的pid :%d,我的ppid: %d\n", getpid(), getppid());
			sleep(1);

		}
	}
	else if (id > 0)
	{
		//父进程
		while (1)
		{
			printf("我是父进程,我的pid :%d,我的ppid: %d\n", getpid(), getppid());
			sleep(1);
		}
	}
	else
	{
		perror("fail");
		exit(-1);
	}
	return 0;
}

When running, use kill -9 PID to kill the child process in the middle, and the child process is a zombie process at this time.

   8.2 Orphan Processes

  • If the parent process exits early, then the child process exits later and enters Z, what should I do?
  • The parent process exits first, and the child process is called an "orphan process"
  • The orphan process is adopted by the No. 1 init process, and of course the init process must be recycled.

 Look at the following piece of code, that is, the parent process runs for 3 seconds and then exits. At this time, the child process is an orphan process.

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int main()
{
	pid_t id = fork();
	if (id < 0) 
    {
		perror("fail");
		return 1;
	}
	else if (id == 0) 
	{
		//子进程
		printf("I am child, pid : %d\n", getpid());
		sleep(10);
	}
	else 
	{
		//父进程
		printf("I am parent, pid: %d\n", getpid());
		sleep(3);
		exit(-1);
	}
	return 0;
}

Comparison of running results:

 


 

If there are deficiencies in this article, you are welcome to comment below, and I will correct it as soon as possible.

Old irons, remember to like and pay attention!!!   

Guess you like

Origin blog.csdn.net/m0_63198468/article/details/131295936