现在的位置: 首页 > 综合 > 正文

linux创建进程

2017年12月26日 ⁄ 综合 ⁄ 共 2950字 ⁄ 字号 评论关闭

linux下进程的创建有两种途径,一种是用一个进程来创建另一个进程,则这个创建的进程则作为父进程,被创建的进程将作为子进程。另一种方式就是由系统来创建,比如我们打开一个应用程序,这时的进程则由系统来创建。在进程被创建之后就要为其分配一定的资源。在系统运行时就创建了几个进程,这几个进程被称为系统进程,以后的创建的进程都是直接或间接的由它们来分配资源。一个进程创建之后系统就为其分配进程ID,当这个进程是由另一个进程创建时,则这个进程将继承其父进程的资源。注意子进程只继承父进程的资源,至于其它的像一些权限,和优先级,警告之类的则不继承。子进程被创建之后是和父进程的地位平等
的,即它们都争夺系统资源。

创建进程的函数为fork(),没有参数,有两个返回值。三种值返回。返回0表示返回是子进程调用fork()函数的返回值,如果返回的是子进程的pid 则表示是父进程在调用fork()函数进的返回,-1表示创建失败。我们可以认为在父进程调用fork()函数后,创建了子进程,子进程再次调用
fork()。所以fork()实际上是被调用了两次。但系统是如何区别是父进程还是子进程调用fork()函数,以便返回不同类型的值的呢。这是系统内 部的事,暂时我也不明白。

process.c

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
int main()
{
    pid_t   pid;
    pid     = fork();
    switch(pid)
    {   
        case 0:
            printf("child process is runing, pid is : %d\n", getpid());
            break;
        case -1: 
            printf("create process faild!\n");
            break;
        default:
            printf("parent process is runing, pid is : %d\n", getppid());
    }   
    return 0;
}


#cc process.c -o cc process -g

#./process

由于子进程与父进程的优先级相同,所以它们应该是交替执行的。可以用下面的代码说明:

process.c

#include
<stdio.h>
#include <unistd.h>
#include <sys/types.h>
int main()
{
    pid_t   pid;
    int     i;  
    char    *msg;
    pid     = fork();
    switch(pid)
    {   
        case 0:
            msg = "child process is runing";
            i=3;
            break;
        case -1: 
            printf("create process faild!\n");
            break;
        default:
            msg = "parent process is runing";
            i=5;
            break;
    }   
    while(i>0)
    {   
        printf("prent pid : %d, chind pid : %d \n", getppid(), getpid());
        puts(msg);
        sleep(1);
        printf("i : %d\n",i);
        i--;
    }   
    return 0;
}

#cc
process.c -o process -g

#./process 
prent pid : 30478, chind pid : 32253 
parent process is runing
prent pid : 32253, chind pid : 32254 
child process is runing
i : 5
prent pid : 30478, chind pid : 32253 
parent process is runing
i : 3
prent pid : 32253, chind pid : 32254 
child process is runing
i : 4
prent pid : 30478, chind pid : 32253 
parent process is runing
i : 2
prent pid : 32253, chind pid : 32254 
child process is runing
i : 3
prent pid : 30478, chind pid : 32253 
parent process is runing
i : 1
i : 2
prent pid : 30478, chind pid : 32253 
parent process is runing
i : 1

孤儿进程是指父进程执行完毕后,父进程被kill这时子进程的getppid()返回的父进程的pid为1,这时子进程就成为孤儿了。孤儿进程由init里程收养。成为init的子进程。init的pid为1。

process.c

#include <stdio.h>

#include
<unistd.h>
#include <sys/types.h>

int main()
{
    pid_t   pid;
    pid     =fork();
    switch(pid)
    {
        case 0:
            while(1)
            {
                printf("a background process, PID : %d, parentID : %d\n", getpid(), getppid());
                sleep(1);
            }
            break;
        case 1:
            printf("create child process faild!\n");
            break;
        default:
            printf("I am parent proccess, my pid is %d \n", getpid());
            break;
    }
    return 0;
}

#cc
process.c -o process -g

#./process

a
background process, PID : 836, parentID : 835
I am parent proccess, my pid is 835 

#a
background process, PID : 836, parentID : 1

a
background process, PID : 836, parentID : 1
a background process, PID : 836, parentID : 1
a background process, PID : 836, parentID : 1
a background process, PID : 836, parentID : 1
a background process, PID : 836, parentID : 1
a background process, PID : 836, parentID : 1
a background process, PID : 836, parentID : 1


这个程序运行后会一直执行,
这时可以打开另一个shell,用kill命令来kill它。

子进程与父进程相同的点主要有:相同的用户ID,相同的组ID。当前的工作目录,根目录,打开的文件,创建文件时使用的屏蔽字,信号屏蔽字,上下文环境,共享的存储段,资源限制....。



抱歉!评论已关闭.