Operating system experiment 2 process creation control experiment (including code and experiment experience)

Implementation tool: PC

Implementation environment: Linux

Internship content (function, goal):

Purpose of the experiment: 
Create a process and experience the concurrency characteristics between processes. 


Experimental content: 
Write a program to create two child processes p1 and p2 using the system call fork(). And the parent process outputs the string "father", the first child process outputs the string "borther1", and the second child process outputs the string "borther2". 


Experimental requirements: 
(1) Master the usage and functions of the system call fork(); 
(2) Use the system call sleep() to adjust the concurrent execution process of the parent process and each child process; 
(3) Carefully check and compare each execution As a result, analyze the reasons.

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

Use the system call fork() to create two subprocesses p1 and p2, and output their process numbers through getpid(), and output their respective identities in the subprocesses. Completed the creation of the process, and used the system call sleep() to adjust the concurrent execution process of the parent process and each child process to experience the concurrency characteristics between processes. This experiment is relatively simple, mainly using system call functions such as fork(), getpid(), and sleep(), and does not involve algorithm issues.


Function function:
1. Fork()
 (1) Function: create a child process;
 (2) return value
0: return from the process;
>0: return from the parent process, the return value is the process identification number of the child process;
-1: Create failed to return.

Main code:

#include<stdio.h> 
#include<stdlib.h> 

int main() { 
    int p1,p2; 
    while((p1=fork())==-1); 
    if(p1==0) 
    { 
        printf("brother1\n"); 
        exit(0); 
        sleep(5); 
    } 
    else{ 
        while((p2=fork())==-1); 
        if(p2==0) 
        { 
            printf("brother2\n"); 
            exit(0); 
            sleep(0); 
        } 
        wait(0); 
        printf("father\n"); 
    } 
}

Notes:

1. Create child process 1 
2. If p1 is 0, execute the content in child process 1 
3. Otherwise, create child process 2

Result analysis:

Screenshot of the experiment:

Create child process 1;
if p1 is 0, execute the content in child process 1;
otherwise, create child process 2


Mistake 1: 
Forgot to add the header file: #include<stdlib.h>
                     #include<wait.h> ;
 

Mistake 2: 
No wait (0), the parent process did not wait for the creation of the child process, and went to the CPU first;

Thinking questions:
(1) Why is the output order of each character string arbitrary? Answer: The processes are executed concurrently, and the child process and the parent process preempt the processor, so the order of the output characters is different. (2) Rewrite the program, use the loop statement to output the character string one by one, and then check the execution result? Answer: When the sleep () statement does not exist, the output will be random. During the output process of brother1, brother2 may be output first and then the characters behind brother1. Otherwise, the output will be sequential. Of course, the sequential output is based on a good system. Under the premise of control, otherwise it may be out of order
           

           
 

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. 

Guess you like

Origin blog.csdn.net/m0_50962679/article/details/124707266