Chapter 8 process management sub-process Adoption of apue

Source: Internet
Author: User
Tags glob

Finally, I began to learn about the apue Process Management chapter. In fact, I have read a book, read it, and read it carefully. However, we do not have much time to read words one by one, so we need to identify them. You can only view what you are interested in. For example, if you are interested in process management, you can directly view the process management chapter, Instead of viewing files or Io.

The following is an example of fork. Program list 8-1

#include "apue.h"int glob = 6;char buf[] = "a write to stdout\n";int main(){intvar;       volatilepid_tpid,pid2;var = 88;if(write(STDOUT_FILENO, buf, sizeof(buf)-1 ) != sizeof(buf)-1 )err_sys("write error");printf("before fork\n");if(((pid = fork()) < 0) )err_sys("fork errir");else if(pid == 0){//printf("the pid = fork() returns %d\n",pid);glob++;var++;//sleep(10);//the child block itself,when its parent dies after sleep for 2s,the child will be an orphan  //then will be adobted by the process init }else sleep(2);printf("pid = %d, glob = %d,var =%d\n",getpid(),glob,var);//printf("the pid of parent is getppid = %d\n",getppid());//return the pid of parent of the process//printf("the sizeof buf is %d\n",sizeof(buf));//printf("the strlen of buf is %d\n",strlen(buf));exit(0);}

There are several interesting points.

Make certain modifications to the Program (in fact, do not comment out some statements ).

#include "apue.h"int glob = 6;char buf[] = "a write to stdout\n";int main(){intvar;       volatilepid_tpid,pid2;var = 88;if(write(STDOUT_FILENO, buf, sizeof(buf)-1 ) != sizeof(buf)-1 )err_sys("write error");printf("before fork\n");if(((pid = fork()) < 0) )err_sys("fork errir");else if(pid == 0){printf("the pid = fork() returns %d\n",pid);glob++;var++;//sleep(10);//the child block itself,when its parent dies after sleep for 2s,the child will be an orphan  //then will be adobted by the process init }else sleep(2);printf("pid = %d, glob = %d,var =%d\n",getpid(),glob,var);printf("the pid of parent is getppid = %d\n",getppid());//return the pid of parent of the process//printf("the sizeof buf is %d\n",sizeof(buf));//printf("the strlen of buf is %d\n",strlen(buf));exit(0);}

The difference between sizeof and strlen lies in that the length of the string Buf calculated by sizeof contains the final Null Byte (actually "\ 0"), while strlen does not calculate the null byte. Another difference is that sizeof has computed the buffer length during compilation, so the length is fixed, and strlen needs to call the explain function (I don't know what the difference is ).

a write to stdoutbefore forkthe pid = fork() returns 0pid = 12857, glob = 7,var =89the pid of parent is getppid = 12856pid = 12856, glob = 6,var =88the pid of parent is getppid = 12790

The running result is as follows:

About the parent process. Add printf ("the PID of parent is getppid = % d \ n", getppid (); before exit to obtain the parent process, we can see that the PID of the parent process of one process is the PID of another process, indicating that this process is a sub-process, and the other is the parent process. The parent process of the parent process is the currently opened terminal process, which is opened by the bash program.

PID TTY      STAT   TIME  MAJFL   TRS   DRS   RSS %MEM COMMAND  928 tty4     Ss+    0:00      2     0  4612   756  0.0 /sbin/getty -8 38400 tty4  932 tty5     Ss+    0:00      2     0  4612   756  0.0 /sbin/getty -8 38400 tty5  944 tty2     Ss+    0:00      1     0  4612   756  0.0 /sbin/getty -8 38400 tty2  945 tty3     Ss+    0:00      1     0  4612   756  0.0 /sbin/getty -8 38400 tty3  947 tty6     Ss+    0:00      1     0  4612   756  0.0 /sbin/getty -8 38400 tty6 1002 tty7     Ss+   11:07    117     0 114036 49700  2.4 /usr/bin/X :0 -auth /var/run/lightdm/root/:0 -nolisten tcp vt7 -novtswitch -background none 1181 tty1     Ss+    0:00      0     0  4612   864  0.0 /sbin/getty -8 38400 tty112790 pts/3    Ss     0:00      0   876  6767  4044  0.1 bash12871 pts/3    R+     0:00      0    82  4605   708  0.0 ps -av

In the program, we use the parent process to sleep for 2 seconds to achieve the effect of simultaneous output by the parent and child processes. If I let the child process sleep for a while, one of my surprises is found.

In the previous output result, the PID = fork () returns 0, because after the parent process fork, the child process returns 0, which indicates the parent process, because a process may have n sub-processes but only one parent process, 0 is returned, so it will enter the IF (pid = 0) region. If the sub-process is also sleep and sleep (10) (Added after var ++ ).

Effect:

a write to stdoutbefore forkthe pid = fork() returns 0pid = 13503, glob = 6,var =88the pid of parent is getppid = 12790pid = 13504, glob = 7,var =89the pid of parent is getppid = 1

The child process is the result of glob and VAR auto-increment. Therefore, the last two rows are the running result of the child process. However, this time, the getppid returned by the child process is 1 instead of the parent process PID 12503, what is the problem.

Sub-process sleep for a long time. That is to say, when the sub-process wakes up and continues to execute, the parent process has been executed and does not exist. At this time, the sub-process becomes an orphan process, then it will be adopted by the INIT process. The init PID is 1, which ensures that all processes in Linux have parent processes. This was an unexpected discovery. This coincidence helped me understand the INIT process in section 8.5. Otherwise, it would be a pleasant surprise. The inspiration I got from this is that the process can be stopped through process sleep, which is very helpful for debugging. There will be many different discoveries when I learn to change the sample program.

In Section 8.4, The vfork usage is introduced, but my results are not the same.

#include "apue.h"int glob = 6;char buf[] = "a write to stdout\n";intmain (){  int var;  volatile pid_t pid, pid2;var = 88;  printf ("before vfork\n");  if (((pid = vfork ()) < 0))    err_sys ("vfork errir");  else if (pid == 0)    {      glob++;      var++;      //printf ("pid = %d, glob = %d,var =%d\n", getpid (), glob, var);      _exit (0);    }  printf ("pid = %d, glob = %d,var =%d\n", getpid (), glob, var);  exit (0);}

My result is

before vforkpid = 13615, glob = 7,var =88

Unlike the answer in the book, the VaR in the book is 89, and my answer is 88. The strange thing is the glob auto-increment, which indicates that the if statement is executed. Remove the annotation of the printf in IF and print out Var = 89, which indicates that it is indeed executed. I am not a cainiao either. I think it is a problem of global variables and local variables in program execution. I can see that 89 is printed when I make var a volatile variable before int var. So I think it is an optimization problem. Check my makefile. Indeed, I am at the O2 optimization level. Changed to O1 and found that the result was still 88. If changed to Carefree, It would be 89. The GCC optimization issue is worth exploring. There is such a small problem in PC programming. It also exists in the GCC programming of AVR. It is a double-edged sword to optimize this thing. I will write an optimized blog later.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.