C Process Learning Log

Source: Internet
Author: User

  

#include <unistd.h>#include<sys/types.h>#include<pwd.h>#include<stdio.h>intMainintargcChar**argv)    {pid_t my_pid,parent_pid;    pid_t te; My_pid=Getpid (); Parent_pid=Getppid (); printf ("Mypid is [%d]\t parentid is [%d]\n", My_pid,parent_pid); Te=Fork (); printf ("*********begin*************\n"); My_pid=Getpid (); Parent_pid=Getppid (); printf ("Mypid is [%d]\t parentid are [%d]\t te is%d\n", My_pid,parent_pid,te); printf ("*********end*************\n");#if0te=Fork (); My_pid=Getpid (); Parent_pid=Getppid (); printf ("Mypid is [%d]\t parentid are [%d]\t te is%d\n", My_pid,parent_pid,te); GetChar ();#endif

RESULT:

Mypid is [1705] parentid is [1521]
begin*************
Mypid is [1705] parentid are [1521] Te is 1706
end*************
[Email protected]:/usr/lsrcc/tempuserfun# *********begin*************
Mypid is [1706] ParentID are [1] te is 0
end*************

Result Analysis: Call fork once, fork return 2 times, one is parent process, one is child process.

#include <unistd.h>#include<sys/types.h>#include<pwd.h>#include<stdio.h>intMainintargcChar**argv)    {pid_t my_pid,parent_pid;    pid_t te; My_pid=Getpid (); Parent_pid=Getppid (); printf ("Mypid is [%d]\t parentid is [%d]\n", My_pid,parent_pid); Te=Fork (); printf ("*********begin*************\n"); My_pid=Getpid (); Parent_pid=Getppid (); printf ("Mypid is [%d]\t parentid are [%d]\t te is%d\n", My_pid,parent_pid,te); printf ("*********end*************\n");#if1printf ("*********2thbegin*************\n"); Te=Fork (); My_pid=Getpid (); Parent_pid=Getppid (); printf ("Mypid is [%d]\t parentid are [%d]\t te is%d\n", My_pid,parent_pid,te); printf ("********2th*end*************\n"); GetChar ();#endif}

RESULT:

begin*************
Mypid is [1801] parentid are [1521] Te is 1802
end*************//First Fork Parent process
2thbegin*************
Mypid is [1801] ParentID was [1521] Te is 1803//the 2nd fork of the parent process (based on the parent process of the first fork)
2th*end*************
Mypid is [1803] ParentID was [1801] Te is 0//the 2nd fork of the child process (based on the parent process of the first fork)
2th*end*************
begin*************
Mypid is [1802] parentid are [1801] te is 0
end*************//Sub-process for the 1th time Fork
2thbegin*************
Mypid is [1802] ParentID was [1801] Te is 1804//the 2nd fork of the parent process (based on the parent process of the 2nd fork)
2th*end*************
Mypid is [1804] ParentID was [1802] Te is 0//child process of the 2nd fork (based on the parent process of the 2nd fork)
2th*end*************

There are 6 processes at the end of the system run.

#include <unistd.h>#include<sys/types.h>#include<pwd.h>#include<stdio.h>intMainintargcChar**argv)    {pid_t my_pid,parent_pid;    pid_t te; Static intCount =0; My_pid=Getpid (); Parent_pid=Getppid (); inti =0; Const Char*p ="/usr/lsrcc/dmf/main";  for(i =0; I <2;++i) {te=Fork (); if(Te <0) {printf ("Fork err!\n"); return-1; }        if(Te >0) {printf ("I am%i \nmypid is [%d]\t parentid are [%d]\t te is%d\n", I,my_pid,parent_pid,te); printf ("This is parent\n"); Count++; }        Else if(0==te) {printf ("i is%i\nmypid are [%d]\t ParentID is] [%d]\t te is%d\n", I,my_pid,parent_pid,te); printf ("This is child\n"); Count++;        EXECLP (P,null); }} printf ("Count is%d\n", count);}

RESULT:

I is 0
Mypid is [2798] parentid are [2361] te is 2799
This is PARENT
I is 1
Mypid is [2798] parentid are [2361] te is 2800
This is PARENT
Count is 2
[Email protected]:/usr/lsrcc/tempuserfun# i is 1
I is 0
Mypid is [2798] parentid are [2361] te is 0
This is a child
This is mytool1 print Hello
Mypid is [2798] parentid are [2361] te is 0
This is a child
This is mytool1 print Hello
This is mytool2 print Hello
This is mytool2 print Hello

An example of a child process invocation is provided primarily

C Process Learning log

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.