LINUX进程学习(一)

(一)什么是进程

通俗地来讲,进程就是程序的一次活动,让程序跑起来,系统会产生一个进程。而程序是一个静态的文件。我们可以通过

// An highlighted block
CLC@Embed_Learn:~$ top		//可以查看当前进程

(二)创建进程fork()函数

// 函数讲解
FORK(2)                                                                               
NAME
       fork - create a child process

SYNOPSIS
       #include <unistd.h>

       pid_t fork(void);
RETURN VALUE
       On success, the PID of the child process is returned in the parent, and 0 is returned in the  child.   On  failure,  -1  is
       returned in the parent, no child process is created, and errno is set appropriately.

这个函数的作用是创建一个子进程,无需参数,创建成功时返回大于零的数是父进程,等于零是进入了子进程。

// An highlighted block
#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>

int main()
{

        pid_t pd;
        pd = fork();

        if(pd>0)
        {
                printf("this is father jincheng!%d\n",getpid());

        }else if(pd==0)
        {
                printf("this child jihceng!%d\n",getpid());
        }
        return 0;
}
// 运行结果,两个进程不一样
LS@Embed_Learn:~$ 
LS@Embed_Learn:~$ gcc jinchengtest1.c  -o test
LS@Embed_Learn:~$ ./test
this is father jincheng!4847
this child jihceng!4848


//在执行第一次程序的会走一个父进程,父进程的返回值大于零会输出那一句话。
//然后再走一遍程序的时候,因为已经产生了一个子进程,所以会走下面那一句。

如果以上还不够形象理解,我们继续举例说明:

// 代码展示
var foo = 'bar';
#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
#include<unistd.h>


int main()
{
        pid_t pd1;
        pid_t pd2;
        pid_t returnpd;

        pd1 = getpid();

        printf("before fork the pid = %d\n",pd1);

        returnpd = fork();

        pd2 = getpid();

        if(pd1==pd2){
        	printf("this is father jingcheng! pid=%d  returnpd=%d\n",getpid(),returnpd);}

        else{
        	printf("this is child jincheng! pid=%d  returnpd=%d\n",getpid(),returnpd);}

        return 0;

}

// 运行结果
LS@Embed_Learn:~$ ./test
before fork the pid = 5479
this is father jingcheng! pid=5479  returnpd=5480
this is child jincheng! pid=5480  returnpd=0

//通过结果我们可以看到父进程的fork函数返回的是子进程的pid,子进程返回0.

(三)进程的模拟运用

进程拿来干嘛呢?我们再模拟一个服务器的功能,我们运用父进程进行父进程的指令监听,接收到外界的用户需求时,会创建子进程进行响应。下面我们来实现这个功能。

// 代码展示
#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>

int main()
{
        pid_t pid1;
        pid_t pid2;
        pid_t returnpid;
        while(1){
                int data;
                printf("please input your choose:\n");
                scanf("%d",&data);
                if(data==1){
                        returnpid = fork();
                        if(returnpid>0){

                        }
                        else if(returnpid==0){
                                printf("do net connected! the pid=%d\n",getpid());
                                sleep(3);
                        }
                }
                else {
                        printf("not fonud!\n");
                }
}

        return 0;
};

(四)vfork函数运用

vfork与fork函数之间的区别
1.前者会拷贝一份父进程,后者是与父进程共享
2.前者是保证先执行子进程再执行父进程。

// An highlighted block
#include<stdlib.h>
#include<sys/types.h>
#include<stdio.h>
#include<unistd.h>


int main()
{
        pid_t returnpid = vfork();

        int cnt =0;
        if(returnpid>0)
        {
                while(1){
                printf("the cnt = %d\n",cnt);
                printf("fu jingcheng!  the pid=%d\n",getpid());
                sleep(3);
        }
}
        else if(returnpid==0){
                while(1){
                printf("this if zijingcheng! the  pid=%d\n",getpid());
                cnt++;
                sleep(2);
                if(cnt==3){exit(0);}
        }
}
        return 0;
}
//可以得到反馈

(五)父进程等待子进程退出

我们这里需要用到

// An highlighted block
wait();//函数

通过收集子进程的状态码来判断是否子进程已经退出。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值