zoukankan      html  css  js  c++  java
  • linux C进程入门

    • 一个进程控制块,大体分下列三个内容:
    1. 进程描述信息
    2. 进程运行状态/调度信息
    3. 进程资源信息(进程所在存储器的地址信息,文件系统以及打开的文件信息)
    4. cpu现场信息;中断后保存寄存器信息和堆栈信息,以便下次回到断点后可以继续执行
    进程通过fork()函数产生,其返回一个整型的进程号,在一段代码体中,通常以pid的值(>0主进程,==0子进程,-1进程创建失败)来区分主进程和子进程的行为
    #include<stdio.h>
    #include<stdlib.h>
    #include<unistd.h>
    void main()
    {
      pid_t pid;
      pid=fork();
      if(pid==0)
      {  
        //用于定义子进程的逻辑
    printf(
    "pid==%d this is a child process of %d ",getpid(),getppid()); } if(pid==-1) { printf("process created failed! "); } if(pid>0)//用于定义父进程的逻辑 { printf("%d: a master process working!---%d ",getpid(),getppid()); } }

    输出结果:

     根据这一原理可以循环创建进程----比如循环六次,但规定只让父进程来创建进程,以避免进程的创建出现指数级增长

    fork()函数特性,父进程返回的pid值>0,子进程返回的值==0

    #include<stdio.h>
    #include<stdlib.h>
    #include<unistd.h>
    void main()
    {
        pid_t pid;
        int i;
        for(i=0;i<6;i++)
        {
            pid=fork();
            if(pid==-1)
            {
                perror("mitched lily marlin");
                exit(-1);
            }
            if(pid==0)
            {
                break;
            }
        }
    
        if(i<6)
        {
            sleep(1);
            printf("processid: %d,  child process id %d, master process id %d
    ",i+1,getpid(),getppid());
        }
        else
        {
            sleep(1);
            printf("master process id %d
    ",getpid());
        }
    }

    内核调度算法:cpu决定给哪个进程共享

    父子进程间数据共享遵循,读时共享,写时复制的原则

    #include<stdio.h>
    #include<stdlib.h>
    #include<unistd.h>
    int var=88;
    void main()
    {
        pid_t pid;
        int i;
        pid=fork();
        if(pid>0)
        {
            sleep(2);
            var=66;
            printf("master process %d
    ",var);
        }
        if(pid==0)
        {
            var =100;
            printf("child process %d
    ",var);
        }
    }

    输出结果:

    •  gdb调试: set follow-mode-fork child跟踪子进程  set follow-mode-fork parent跟踪父进程,一定要在fork之前设定
    • 另一个进程上机实验,可以看到父进程和子进程各自保留了变量var的地址和值
    #include <stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #include<unistd.h>
    #include<sys/types.h>
    int main()
    {
        pid_t pid;
        pid_t pid2;
        int var =88;
        char *str=(char*)malloc(sizeof(char)*10);
        memset(str,0x00,10);
        pid=fork();
        if(pid<0)
        {
            fprintf(stderr,"fork failed
    ");
        }
        else if(pid==0)
        {
            sleep(2);
            printf("I'm the child %d
    ",pid);
            pid2 =getpid();
            printf("I'm the child %d
    ",pid2);
            strcpy(str,"lovecpc");
            var++;
        }
        else
        {
            pid2=getpid();
            printf("I'm the parent is %d, the child is %d
    ",pid2,pid);
            strcpy(str,"wantcpc");
            wait(NULL);
            printf("complete
    ");
        }
        printf("str is %s, straddr = %p, var=%d, varaddr=%p 
    ",str,str,var,&var);
    }
  • 相关阅读:
    Java连接操作redis
    redis 6.0.x简介和安装
    设计模式之代理模式(proxy)
    设计模式之组合模式(composize)
    Linux Shell脚本调试方法
    linuxcfg.sh
    反向代理和正向代理区别
    WAF与IPS的区别总结
    【LemonCK】jQuery的基本使用
    【LemonCK】CSS盒子塌陷问题
  • 原文地址:https://www.cnblogs.com/saintdingspage/p/12164568.html
Copyright © 2011-2022 走看看