16281035- Operating System Experiment 2

A second experiment operating system: Process control

16,281,035
my github website, in which the active file, please see

Purpose

  • Deepen their understanding of the concept of the process, a clear distinction between process and procedures.
  • Linux system to master the process of creation, and other management and delete operations.
  • Familiar with the commands and tools under Linux, such as man, find, grep, whereis, ps, pgrep, kill, ptree, top, vim, gcc, gdb, the pipeline | and so on.

Basics

  • Creation process

Linux, loaded into memory and executed operation and create a new program image process operations are split. The program image loaded into memory, and start running it, a process known to run a new program, known as the corresponding system calls exec system call. And create a new process is fork system call system call.

  • exec system call

#include <unistd.h>

int execl (const char *path, const char *arg,…);

the execl () within the meaning of the image path into memory path, arg its first parameter. Variable-length parameters. Argument list must end with NULL.

Usually execl () does not return. A successful call to jump to a new entry point as the end. When an error occurs, the execl () returns -1, and errno value.

Example Edit /home/kidd/hooks.txt:

int ret;

ret = execl ( "/ bin / we", "us", "/ home / kidd / hooks.txt", NULL);

if (ret == -1)

perror (”execl”);

  • system call fork ****

#include <sys/types.h>

#include <unistd.h>

pid_t fork (void);

A successful call to fork () creates a new process, roughly the same as it calls fork () process. When an error occurs, the fork () returns -1, and errno value.

Example:

pid_t pid;

pid = fork ();

if (pid > 0)

printf (”I am the parent of pid=%d!\n”, pid);

else if (!pid)

printf (”I am the baby!\n”);

else if (pid == -1)

perror (”fork”);

  • Terminate the process

exit () system call:

#include <stdlib.h>

void exit (int status);

  • Process Hangs

pause () system call:

int pause( void );

Pause function will suspend the process until a signal is received. After receiving the signal, the process will exit from the pause function, continue to operate.

  • wait ( wait for the child interrupt or end )

#include<sys/types.h>

#include<sys/wait.h>

pid_t wait (int * status);

wait () will temporarily stop the implementation of the current process, or until there is a signal to the child process ends.

If the child process has ended when calling wait (), then wait () will immediately return the child process end status value.

Ending state value is returned by the child process parameter status, while the child process will be returned along with the identification code.

If the end state do not care value, the status parameter can be set to NULL.

VIM Quick commonly used commands

Here Insert Picture Description

Experimental subject

1. Open a vi process. By the ps command, and select the appropriate parameters, only the name for the process of vi. Looking vi parent process until the init process so far. Recording process ID and parent process ID of all processes. The resulting process tree and get the process tree by the pstree command were compared.

  1. First, I create a file called vim VI himself, then opened with vim
    1552704979565

  2. Open interface as follows:
    in which I enabled editing commands to edit the following:
    1552705009838

  3. Storage
    1552705098963

  4. View vim process ID, 5065

    ![1552705140843](https://img-blog.csdnimg.cn/20190316114007384.png
    Here Insert Picture Description

  5. Looking vim parent of the process until the init process so far. Recording process ID and parent process ID of all processes
    Here Insert Picture Description
    Here Insert Picture DescriptionHere Insert Picture Description
    pid init process is the 1.

    The init process is the parent of all other processes.

    Listed parent process is the process number 1, that is, list all processes except init the outside.

    So look for the parent vim process as follows:

    ​ 5065 -> 4626 -> 4619 -> 908 -> 1

  6. The resulting process tree and get the process tree by the pstree command were compared.

Here Insert Picture Description

Here Insert Picture Description
Two compared to the same result: 5065--> 4626--> 4619--> 908--> 1

2, programming, the first to use the fork system call, create a child process. Continue air circulation operation in the parent process; calls exec to open the vi editor in the child process. Then another terminal by ps -Al command, ps aux or top commands, view the status of running vi process and its parent process, understand the meaning expressed by each parameter. Select the appropriate command parameters, sort all processes in accordance with the CPU utilization.

  1. Program code fork.c

    #include <unistd.h>
    #include <sys/types.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc, char *argv[]){
    pid_t pid = fork();
    
    //在子进程
    if(pid == 0)
    {
    int num = execl("/usr/bin/vim","vim","NULL");
    if(num == -1)
    perror("execl");
    }
    
    else if(pid>0)
    {
    //在父进程
    while(1){}
    }
    
    else
    {
    printf("fork调用失败");
    }
    return (0);
    }
    
  2. The result: open vim

    Here Insert Picture Description
    Here Insert Picture Description
    Here Insert Picture Description

  3. View vi process and the parent process of running the state and the various parameters mean:

    Here Insert Picture Description
    Here Insert Picture Description

  4. Select the appropriate command parameters, sort all processes in accordance with the CPU utilization

Here Insert Picture Description
Here Insert Picture Description

3, using the fork system call, create the following process tree, and the output of each process ID and its parent process ID. Change the order of execution and operational status of the observation process.

Here Insert Picture Description

  1. First, the fork () function call a simple test
    Here Insert Picture Description

Operating results
Here Insert Picture Description
output two hello

So: function call through the system to create a process nearly identical to the original process, the newly created process is called the child process. A process calls fork () after the function, the system assigns resources to give new process, such as space to store data and code. Then all the value of the original are copied to the new process of the new process, different values ​​only a few values ​​with the original process. The equivalent of a clone of himself. One thing to note: that after calling the fork function, it must be two processes at the same snippet of code is executed after the fork function, whereas the previous code, and performed by the parent process is completed.

fork () return value meanings are as follows:

= 0: in the child

0: In the parent process

<0: Creation Failed

  1. Source fork3.c

    #include <unistd.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <sys/wait.h>
    #include <time.h>
    #include <stdarg.h>
    #include<sys/types.h> //这个头文件不能少,否则pid_t没有定义 
    
    int main(int argc, char *argv[])
    {
    pid_t pidA,pidB,pidC,pidD;
    printf("这是父进程,P1,PID是%d\n",getpid());
    
    pidA = fork(); //创建新进程
    
    if(pidA<0)
    {
    printf("新进程创建失败\n");
    return 0;
    }
    
    else if(pidA == 0)
    {
    printf("这是子进程,P3,PID是%d\n",getpid());
    return 0;
    }
    
    else
    {
    pidB = fork();
    if(pidB<0)
    {
    printf("新进程创建失败\n");
    return 0;
    }
    
    else if(pidB == 0)
    {
    printf("我是子进程,P2,PID是%d\n",getpid());
    
    pidC = fork();
    if(pidC == 0)
    {
    printf("这是子进程,P4,PID是%d\n",getpid());
    return 0;//防止产生孙进程
    }
    
    pidD = fork();
    if(pidD == 0)
    {
    printf("这是子进程,P5,PID是%d\n",getpid());
    return 0;//防止产生孙进程
    }
    }
    }
    return 0;
    }
    
    
  2. operation result
    Here Insert Picture Description

  3. Code improvements: I found the code as above, whether parent or child processes are still in execution, will not stop.

    So I added the following code in the original basis:

     int *status;
                 waitpid(pidC,status, 0);
                 waitpid(pidD,status, 0);
                 return 0;
            }
        }
        int *status;
        waitpid(pidB,status, 0);
        waitpid(pidA,status, 0);
        return 0;
    
    

    The results obtained automatically stop running
    Here Insert Picture Description

4, the above-described process to modify the process tree, such that all processes are cyclic own ID and outputs the ID of the parent process. Then terminate the process p2 (respectively kill -9, own normal exit exit (), segmentation fault exit), observe p1, p3, p4, p5 operating status process and other related parameters have changed.

  1. kill -9

    Here Insert Picture Description

    In kill the process P2, the process number after 5228, P4 and P5 of PPID is no longer 5228, and became a 895.

    I query the process tree, found systemd (895), which is equivalent to the most primitive of a root process. So I have a lot of questions:

    (1) loss of the parent's why these "orphan" child process PPID is not systemd (1)?

    What is the association between (2) systemd (1) and systemd (895) that?

  2. Own normal exit

    Source

    #include <unistd.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <sys/wait.h>
    #include <time.h>
    #include <stdarg.h>
    #include<sys/types.h> //这个头文件不能少,否则pid_t没有定义 
    
    int main(int argc, char *argv[])
    {
    pid_t pidA,pidB,pidC,pidD;
    int i;
    printf("这是父进程,P1,PID是%d\n",getpid());
    
    pidA = fork(); //创建新进程
    
    if(pidA<0)
    {
    printf("新进程创建失败\n");
    exit(0);
    }
    
    else if(pidA == 0)
    {
    i=100;
    while(i-->0)
    {
    printf("这是子进程,P3,PID是%d 它的父进程,P1,PID是%d\n",getpid(),getppid());
    }
    return 0;
    }
    
    else
    {
    pidB = fork();
    if(pidB<0)
    {
    printf("新进程创建失败\n");
    return 0;
    }
    
    else if(pidB == 0)
    {
    i=100;
    pidC = fork();
    if(pidC == 0)
    {
    i=100;
    while(i-->0){
    printf("这是子进程,P4,PID是%d 它的父进程,P2,PID是%d\n",getpid(),getppid());}
    return 0;//防止产生孙进程
    }
    
    pidD = fork();
    if(pidD == 0)
    {
    i=100;
    while(i-->0){
    printf("这是子进程,P5,PID是%d 它的父进程,P2,PID是%d\n",getpid(),getppid());
    }
    return 0;//防止产生孙进程
    }
    i=100;
    while(i-->0)
    {
    printf("这是子进程,P2,PID是%d 它的父进程,P1,PID是%d\n",getpid(),getppid());
    if(i==50) {
    	exit(0);
    }
    }
    
    int *status;
    waitpid(pidC,status, 0);
    waitpid(pidD,status, 0);
    return 0;
    }
    }
    int *status;
    waitpid(pidB,status, 0);
    waitpid(pidA,status, 0);
    return 0;
    
    }
    

    operation result:

And the first asking the same results, but it runs too fast, is not automatically part of the front end is not enough time to capture.
Here Insert Picture Description
3. Exit mistakes

Source


#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <time.h>
#include <stdarg.h>
#include<sys/types.h> //这个头文件不能少,否则pid_t没有定义 

int main(int argc, char *argv[])
{
pid_t pidA,pidB,pidC,pidD;
int i;
printf("这是父进程,P1,PID是%d\n",getpid());

pidA = fork(); //创建新进程

if(pidA<0)
{
printf("新进程创建失败\n");
exit(0);
}

else if(pidA == 0)
{
i=100;
while(i-->0)
{
printf("这是子进程,P3,PID是%d 它的父进程,P1,PID是%d\n",getpid(),getppid());
}
return 0;
}

else
{
pidB = fork();
if(pidB<0)
{
printf("新进程创建失败\n");
return 0;
}

else if(pidB == 0)
{
i=100;
pidC = fork();
if(pidC == 0)
{
i=100;
while(i-->0){
printf("这是子进程,P4,PID是%d 它的父进程,P2,PID是%d\n",getpid(),getppid());}
return 0;//防止产生孙进程
}

pidD = fork();
if(pidD == 0)
{
i=100;
while(i-->0){
printf("这是子进程,P5,PID是%d 它的父进程,P2,PID是%d\n",getpid(),getppid());
}
return 0;//防止产生孙进程
}
i=100;
while(i-->0)
{
printf("这是子进程,P2,PID是%d 它的父进程,P1,PID是%d\n",getpid(),getppid());
if(i==50) {
int *ptr = NULL;
*ptr = 0;
}
}

int *status;
waitpid(pidC,status, 0);
waitpid(pidD,status, 0);
return 0;
}
}
int *status;
waitpid(pidB,status, 0);
waitpid(pidA,status, 0);
return 0;

}

The result:
Here Insert Picture Description
Here Insert Picture Description
mistakes will not affect the exit from the above results, P2? It still is the parent process P4 and P5.

Guess you like

Origin blog.csdn.net/qq_42452405/article/details/88594918