Linux中进程管理(一)-学习笔记(三)

1、虚拟空间
当运行内存不够时,磁盘分配一些内存个CPU寄存器,成为虚拟内存。

虚拟空间的作用?
(1)方便编译器和操作系统安排程序的地址分布
(2)方便进程之间的隔离
(3)方便内存管理
2、进程的产生

父进程和子进程用法实例
//程序1  产生父进程和子进程
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
//产生父进程和子进程
int main(void)
{
    pid_t pid;//定义pid变量
    int n = 10;//定义普通变量
    pid = fork();//调用1次,返回2次,在父进程中返回子进程ID号,在子进程中返回0
    if (pid>0)//父进程
    {
        printf("I am the parent\n");
        while (1)
            ;
    }
    else if (pid == 0)//子进程
    {
        printf("I am the child\n");
        while (1)
            ;
    }
    else
    {
        perror("fork\n");
        exit(-1);
    }
    return 0;
}
//ps aux查看进程
//程序2   无限执行父进程和子进程
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
//一直执行父进程和子进程
int main(void)
{
    pid_t pid;//定义pid变量
    int n = 10;//定义普通变量
    pid = fork();//调用1次,返回2次,在父进程中返回子进程ID号,在子进程中返回0
    if (pid>0)//父进程
    {
        while (1)
        {
            printf("I am the parent\n");
        }
        sleep(1);//延时1s
    }
    else if (pid == 0)//子进程
    {
        while (1)
        {
            printf("I am the child\n");
        }
        sleep(3);//延时3s
    }
    else
    {
        perror("fork\n");
        exit(-1);
    }
    return 0;
}
//程序3   父进程执行3次,子进程执行1次,无限循环
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
//父进程执行3次,子进程执行1次,无限循环
//变量值fork读同一份,当变量值被修改,则发生拷贝,开辟空间,父进程和子进程变量值各不同
int main(void)
{
    pid_t pid;//定义pid变量
    int n = 10;//定义普通变量
    pid = fork();//调用1次,返回2次,在父进程中返回子进程ID号,在子进程中返回0
    if (pid>0)//父进程
    {
        while (1)
        {
            printf("I am the parent %d \n", n++);
            printf("my pid= %d \n", getpid());//输出当前进程ID号
            printf("my parent pid= %d \n", getppid());//输出当前进程的父进程ID号
            sleep(1);//延时1s
        }
        
    }
    else if (pid == 0)//子进程
    {
        while (1)
        {
            printf("I am the child %d \n", n++);
            printf("my pid= %d \n", getpid());
            printf("my parent pid= %d \n", getppid());
            sleep(3);//延时3s
        }
        
    }
    else
    {
        perror("fork\n");
        exit(-1);
    }
    return 0;
}
//程序4   僵尸进程
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
//僵尸进程-->父进程存在,子进程中止,子进程PCB尚活跃,但占用资源且不做事

int main(void)
{
    pid_t pid;//定义pid变量
    int n = 10;//定义普通变量
    pid = fork();//调用1次,返回2次,在父进程中返回子进程ID号,在子进程中返回0
    if (pid>0)//父进程
    {
        while (1)
        {
            printf("I am the parent %d \n", n++);
            printf("my pid= %d \n", getpid());
            printf("my parent pid= %d \n", getppid());
            sleep(1);//延时1s
        }
        
    }
    else if (pid == 0)//子进程
    {
        //while (1)
        //{
        printf("I am the child %d \n", n++);
        printf("my pid= %d \n", getpid());
        printf("my parent pid= %d \n", getppid());
        sleep(3);//延时3s
        //}
    }
    else
    {
        perror("fork\n");
        exit(-1);
    }
    return 0;
}
//程序5   孤儿进程
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
//孤儿进程-->子进程存在,父进程中止,子进程给init PID=1

int main(void)
{
    pid_t pid;//定义pid变量
    int n = 10;//定义普通变量
    pid = fork();//调用1次,返回2次,在父进程中返回子进程ID号,在子进程中返回0
    if (pid>0)//父进程
    {
        //while (1)
        //{
        printf("I am the parent %d \n", n++);
        printf("my pid= %d \n", getpid());
        printf("my parent pid= %d \n", getppid());
        sleep(1);//延时1s
        //}
        
    }
    else if (pid == 0)//子进程
    {
        while (1)
        {
            printf("I am the child %d \n", n++);
            printf("my pid= %d \n", getpid());
            printf("my parent pid= %d \n", getppid());
            sleep(3);//延时3s
        }
    }
    else
    {
        perror("fork\n");
        exit(-1);
    }
    return 0;
}
//程序6 父进程写数据到文件 子进程读文件
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>

//fork 父子进程间具有血缘关系
int main(void)
{
    int fd;//定义文件描述符
    pid_t pid;//定义pid变量
    pid = fork();//调用1次,返回2次,在父进程中返回子进程ID号,在子进程中返回0
    //创建或打开一个文件
    fd = open("tmp.txt", O_CREAT | O_RDWR, 0644);
    if (pid>0)//父进程
    {
        char buf[1024];//接收屏幕输入的字符缓冲数组
        while (1)
        {
            memset(buf, 0, 1024);//字符缓冲数组清空数据
            //fgets(buf,sizeof(buf),stdin);//法一
            //法二--稍有延迟
            read(STDIN_FILENO, buf, sizeof(buf));//从屏幕读取数据到字符缓冲数组
            printf("input:%s", buf);//显示输入的字符
            if (write(fd, buf, sizeof(buf))<0)//或strlen
                perror("write");
            if (strstr(buf, "quit") != NULL)//读取数据中止
            {
                printf("exit\n");
                break;
            }
            printf("I am the parent\n");
        }
    }
    else if (pid == 0)//子进程
    {
        printf("I am child \n");
        char buf[1024];
        while (1)
        {
            int len;
            memset(buf, 0, sizeof(buf));//字符缓冲数组清空数据
            len = read(fd, buf, sizeof(buf));//从文件中读取数据
            if (len >0)
            {
                //printf("message:%s",buf);法一中文可能出现乱码
                //将数据显示在终端
                write(STDOUT_FILENO, buf, len); //法二
            }
            sleep(1);//延时1s
        }
    }
    else
    {
        perror("fork\n");
        exit(-1);
    }
    return 0;
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值