The process of operating system experiment



This is a kid’s shoe with a silly little cake. Although it’s an experiment in class, if you want to finish it well and study it, it’s also an opportunity to improve your understanding. This experiment is mainly a small programming experiment under Linux to understand some knowledge about the process.

Question generation

First of all, I got the experimental instruction given to us by the teacher. At first glance, the procedure is very simple:
The process of operating system experiment

Later, it will be found that the running result is
The process of operating system experiment

It’s strange that in general we would think that it only performs one of the if bifurcations. The key is to understand the function of fork () (after all, we haven’t been exposed to B ({ ̄) d before).

A survey of fork (の)

After reading some big touch blogs that call the fork() function:

The system allocates resources to new processes, such as space for storing data and code. Then all the values of the original process are copied to the new process. Only a few values are different from the original process. It’s like cloning yourself.

Then combine the return value of fork() to judge:

stayParent processIn, fork returnsProcess ID of the new creation sub processInChild processIn, fork returns0; iferror, fork returns anegative

I was still a bit hazy, and later I wrote a silly program to test:
The process of operating system experiment

An output is inserted at almost every step.
The while() function is used to extend the running time to distinguish whether they are parallel programs or not.
Then let’s look at the results:
The process of operating system experiment

We will find that the subprogram does copy a parent process, and the output is the same as that of the parent process (except for the identification used to distinguish, 3657 is the process ID of the new child process created by the parent process, and 0 is the child process’s running time.) , and it’s true that the subprogram and the parent program are parallel (see the two starts).

However, after querying the data, we will find that,In fact, when the child process copies the parent process, it will continue to run from where the parent process runs, that is, it also copies a copy of the record pointer of the parent process。 So if you add an output at the beginning of this program, it will not be displayed in the subprocess.

In addition, we have questions?
Why does [root @ localhost CPP] appear??
Baidu later learned that the shell will listen to the end of its running process (that is, the parent process). When the last 3657 is output, the parent process will end. At this time, the system will automatically output [root @ localhost CPP]!
What about subprocesses?
Because the shell doesn’t care about the newly created process of the parent process itself, when the parent process ends, the child process is automatically classified as a system process, so after the parent process ends, the child process can continue to run.

Let’s try again and add a sentence at the end of the program


The operation results are as follows:
The process of operating system experiment

The purpose of wait (0) is to wait for all subprocesses to finish running, so at this time, it does not appear as before.

Other の extensions

Let’s look at other experiments at this time:

The process of operating system experiment

This is very similar to what we started with. The program will create a subroutine. The subroutine and the parent program are parallel, but in general, because of the fast speed, it is unlikely that the two cross. So we can understand the result.

The process of operating system experiment
The process of operating system experiment

This program has an extra sentence of exit (0), which can exit the subprogram, so we can guess that the subprocess will output XB, and the parent process will output xay.

Concluding remarks

Xiaoniangao may not be completely right about QAQ. If there is any error, please give me your correction( ̄▽ ̄), if you have the same doubts, don’t know if you understand what I said, hee hee, thank you for watching.

Recommended Today

Redis (1)

Redis About redis Remote dictionary server (redis) is a key value storage system. Redis is an open source log type, key value database written in ANSI C language, complying with BSD protocol, supporting network, memory based and persistent, and providing API in multiple languages. It is often referred to as a data structure server, because […]