22. Linux process and signal---process creation (cont.)

Source: Internet
Author: User

22.2 Parent-Child Process action file

File operations are made up of two modes:

IO System invoke Operation file

Standard C IO Operation file

Look at the code:

1#include <unistd.h>2#include <string.h>3#include <fcntl.h>4#include <stdio.h>5#include <stdlib.h>6 7 intG_val = -;//global variables, stored in data segments8 9 intMainvoid)Ten { One     intA_val = -;//local variables, which are stored in the stack when called A     Static intS_val = -;//static variables, stored in data segments -printf"pid:%d", Getpid ()); -  theFILE *FP = fopen ("S.txt","W"); -     intFD = open ("S_fd.txt", O_wronly | O_creat | O_trunc, S_irwxu |s_irwxg); -  -     Char*s ="Hello World"; +ssize_t size = strlen (s) *sizeof(Char); -  +     /*called by the parent process before fork*/ Afprintf (FP,"s:%s, PID:%d\n", S, Getpid ());//Standard IO write (with cache), full cache for file operations atWrite (fd, s, size);//IO system calls provided by the kernel (without caching) -  - pid_t pid; -PID = fork ();//Create Child process -     //after fork, two processes are run (parent and child processes) -     if(PID <0) { inPerror ("Fork Error"); -}Else if(PID >0) { to         //parent process (the PID of the child process is returned in the parent process) +         //code executed by the parent process -G_val = +; theA_val = +; *S_val = +; $ Panax Notoginsengprintf"I AM Parent process pid is%d, ppid are%d, fork return is%d\n", - getpid (), Getppid (), PID); theprintf"g_val:%p, A_val:%p, S_val:%p\n", &g_val, &a_val, &s_val); +}Else { A         //child process (Fork returned in sub-process is 0) the         //code that the child process executes +G_val = -; -A_val = -; $S_val = -; $printf"I am Child process pid is%d, ppid are%d, fork return is%d\n", - getpid (), Getppid (), PID); -printf"g_val:%p, A_val:%p, S_val:%p\n", &g_val, &a_val, &s_val); the     } - Wuyi     //The code here is the code that the parent-child process executes, which is written to the parent-child process's respective cache thefprintf (FP,"pid:%d, G_val:%d, A_val:%d, s_val:%d\n", Getpid (), G_val, A_val, s_val); -Sleep1); Wu  -     return 0; About}

After the compilation is run, two files are generated.

  

Parent Process File S.txt

  

Child process File S_fd.txt

  

The system call is not cached, and the write is written directly into the file, and standard IO is written to the cache.

The cache created is in the heap, our code is before the fork, then the cache is in the heap of the virtual space of the parent process, and after the fork, the child process copies a copy of the heap space of the parent process.

The same fork is also followed by a write cache of fprintf, which is written to their respective caches, at the end of the parent-child process will clear the cache, will be written to the FP

  

22.3 changes in kernel structure when manipulating files
    • The child process inherits only the file descriptor table of the parent process, but does not inherit the shared file tables and I-node
    • After the parent process creates a child process, the reference counter in the File table entry is incremented by 1 to 2, and when the parent process has a close operation, the counter is reduced by 1, the child process can still use the File table entry (that is, the child process or can manipulate the file), and the File table entry is released only if the counter is 0 o'clock.

  

Run Fork:

  

Example: Parent process adjusts file offset, child process writes

Process_append.c

1#include <unistd.h>2#include <fcntl.h>3#include <stdio.h>4#include <stdlib.h>5#include <string.h>6 7 intMainintargcChar*argv[])8 {9     if(ARGC <2)Ten     { Onefprintf (stderr,"Usage:%s file\n", argv[0]); AExit1); -     } -  the     intFD = open (argv[1], o_wronly); -     if(FD <0) -     { -Perror ("Open Error"); +Exit1); -     } +  Apid_t PID =fork (); at     if(PID <0) -     { -Perror ("Fork Error"); -Exit1); -     } -     Else if(PID >0) in{//Parent process adjusts file offset to file trailer -         if(FD, Lseek0L, Seek_end) <0) { toPerror ("Lseek Error"); +Exit1); -         } the     } *     Else ${//the child process appends content from the end of the filePanax Notoginseng         Char*str ="Hello Child"; -ssize_t size = strlen (str) *sizeof(Char); the  +Sleep3);//ensure the parent process adjusts the offset successfully A  the         //from the user's point of view, the child process copies a copy of the parent process's file descriptor, pointing to the same file +         //from the kernel angle area, the file descriptor table copies a copy of the file descriptor that points to the same file, all pointing to the same file -         //the FD here is copied from the parent process . $         //However, and the FD in the parent process is a pointer to the same file $         if(Write (fd, str, size)! =size) { -Perror ("Write Error"); -Exit1); the         } -     }Wuyi  theprintf"pid;%d finish\n", Getpid ()); -Sleep1); Wu  -     //The parent-child process is going to close the file descriptor About Close (FD); $  -     return 0; -}

Compile run:

  

  

22. Linux process and signal---process creation (cont.)

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.