First, process and signal process creation

Source: Internet
Author: User

Process creation

#include <sys/types.h><unistd.h>pid_t fork (void), fork Call 2 times, so 2 return values, The parent process returns a child process ID of0 , 0 is returned in the child process, and an error returns-1

Process instance

#include <unistd.h>#include<string.h>#include<fcntl.h>#include<stdlib.h>#include<stdio.h>intMainvoid) {printf ("Now PID:%d\n", Getpid ());    pid_t pid; PID=Fork (); inti; if(pid<0) {printf ("Create Progress error\n"); return 1; }    Else if(PID = =0)    {         for(i=0;i<Ten; i++) {printf ("I am son progress,pid:%d ppid:%d\n", Getpid (), Getppid ()); I++; Sleep (1); }            }    Else    {         for(i=0;i<Ten; i++) {printf ("I am Father Progress,pid:%d ppid:%d\n", Getpid (), Getppid ()); I++; Sleep (1); }} printf ("PID:%d\n", Getpid ()); Sleep (1); return 0;}

Run results

Now PID:3324I am Father Progress,pid:3324Ppid3166I am son progress,pid:3325Ppid3324I am Father Progress,pid:3324Ppid3166I am son progress,pid:3325Ppid3324I am Father Progress,pid:3324Ppid3166I am son progress,pid:3325Ppid3324I am Father Progress,pid:3324Ppid3166I am son progress,pid:3325Ppid3324I am Father Progress,pid:3324Ppid3166I am son progress,pid:3325Ppid3324PID:3324PID:3325
You can see that the parent-child process is running alternately
Parent-child processes which run first based on system scheduling

Child process Inheritance

Child process Inheritance Properties    user information permissions, directory information, signal information, environment, shared bucket, resource limit, heap, stack and data segment, shared code satin process-specific attribute    process ID, lock information, run time, pending signal operation file kernel structure change    The child process inherits only the file descriptor table of the parent process, does not inherit the single-shared file tables entry and the I-node    parent process creates a child process, and the reference counter of the file table entry is incremented by 1 to 2, and when the parent process is close, the counter is reduced by 1, and the child process can still use the file table entry. The file table entry is freed only if the counter is 0 o'clock

Only body segments share physical memory

Static variables, global variables stored in body segments, local variable storage Yu Ching

First, process and signal process creation

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.