Linux under C programming: "Heroic" of the process

Source: Internet
Author: User
Tags exit printf linux

If we think of the operating system and the various software on the computer as a series of organic life, not the instruction set, then this is a process of the world, in the process of the world also has "moral" and "Law and order", spying on the process world, look at its heroic, wind and rain ~~~~~

Linux supports multiple processes at the same time, which is what we often say in the modern operating system of multi-channel programming, the so-called Linux system is also the scheduling of each process to occupy the CPU times respectively. Because each time slice's time is very small and the macroscopic time compares, gives the person the feeling is many processes are running.

Note: The summary is in the micro is serial, in the macro is parallel.

In order to improve the efficiency of the program, the program is often divided into several parts, which means concurrent programming. The processes in the concurrent program are independent of each other and are communicated through the corresponding mechanism when necessary. If you want to share resources between processes, in order to avoid conflicts, they often use shared resources in turn using the appropriate communication mechanism. When a process communicates, a process waits for another process to continue to run, which also requires interprocess communication to understand how the other is running. Sometimes a mutex occurs between processes, which is used to lock the mechanism. In concurrent programming, the creation and completion of processes are determined by the user. This also shows the concept of the parent process and the child process.

Process creation:

#include <unistd.h>      
           
pid_t fork (void);      
           
pid_t vfork (void);

In this brief, fork creates a child process that is a copy of the parent process, but uses different data segments and stacks with the parent process. Vfork and fork are basically the same but vfork do not replicate the data segments of the parent process, they share data segments. This is because the vfork and EXEC functions use to invoke a program such as the LS command to open a new process. The vfork Hofu process waits for the child process to finish running or to call exit. Fork Hofu the order in which processes and child processes are run is indeterminate.

The following are the procedures that reflect their nature:

#include <sys/types.h> #include <stdio.h> #include <stdlib.h>       
           
     #include <unistd.h> main () {pid_tpid;      
           
     Char*pchar = "before fork";      
           
     Inttest_va = 1; if ((pid= fork ()) < 0) {printf ("forkerror!!      
           
     \ n ");      
           
     Exit (1);      
           
     } elseif (pid = = 0) {printf ("childprocess\n");      
           
     pchar= "Child pchr\n";      
           
                
           
     printf ("%s\n", Pchar);      
           
     Test_va= 2;      
           
     printf ("%d\n", Test_va);      
           
     _exit (2);      
        else {printf ("parentprocess\n");   
     Sleep (1);      
           
     printf ("%s\n", Pchar);      
           
     printf ("%d\n", Test_va); }      
           
}

Change the top fork to vfork.

Fork

Vfork:

View a full set of articles: Http://www.bianceng.cn/Programming/C/201212/34807.htm

Related Article

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.