Linux高级编程(5)进程

被调修改主调

进程:
    进程是一个程序执行的过程,会去分配内存资源,CPU的调度

pcb  是一个结构体  process control  block   进程控制块

PID 进程标识符

当前工作路径 chdir

umask   0002

进程打开的文件列表 fopen...

ulimit -a,显示进程资源的上限

进程和程序的区别?

程序:静态

         存储在硬盘中代码,数据的集合

进程:动态

           程序执行的过程,包括进程的创建,调度,消亡

.c------>a.out--------->process(pid)

(1)程序是永存,进程是暂时的

(2)进程有程序变化,程序没有

(3)进程可以并发,程序无并发

(4)进程与进程会存在竞争计算机的资源

(5)一个程序可以运行多次,变成多个进程

一个进程可以运行一个或多个程序

进程分类:

1.交互式进程

2.批处理进程 shell脚本

3.守护进程(不需要和用户交互)

进程的作用?并发,并行区别。

while(1){   while(1){上下左右发视频}}

进程的状态:

3个状态,就绪->执行态->阻塞(等待,睡眠)基本操作系统

Linux中的状态,运行态,睡眠态,僵尸,暂停态

进程相关函数:

原语:

1.fork();

pid_t fork(); 叉子

一次调用,会返回两次。

子进程先运行和是父进程先进程,顺序不确定。

父子之间变量不共享。

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
   
int main(int argc, const char *argv[])
{
      pid_t pid = fork();
      if(pid>0)
      {
          while(1)
          {
              printf("father,发送视频\n");
              sleep(1);
          }
      }
      else if(0 == pid)
      {
          while(1)
          {
             printf("child,接收控制\n");
              sleep(1);
          }
      }
      else
      {
          perror("fork");
          return 1;
      }
  
      return 0;
 }      

结果为: 

father和child随机输出,不一定father先输出,因为只有一个CPU,cpu在father和child随机跳跃

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
int a = 10;
int main(int argc, char *argv[])
{
    pid_t pid = fork();
    if(pid>0)
    {
        sleep(3);
        printf("father  a is %d\n",a);
    }
    else if(0 == pid)
    {
        a+=20;
        printf("child  a is %d\n",a);
    }
    else 
    {
        perror("fork");
        return 1;
    }
    
    printf("a is %d\n",a);
    return 0;
}

结果为:

因为father里面有sleep,所以child先输出

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
int a = 10;
int main(int argc, char *argv[])
{
    pid_t pid = fork();
    if(pid>0)
    {
        sleep(3);
        printf("father  a is %d, pid:%d ppid:%d\n",a,getpid(),getppid());
    }
    else if(0 == pid)
    {
        a+=20;
        printf("child  a is %d pid:%d ,ppid:%d\n",a,getpid(),getppid());
    }
    else 
    {
        perror("fork");
        return 1;
    }
    
    printf("a is %d, pid:%d ppid:%d\n",a,getpid(),getppid());
    return 0;
}

结果为:

练习:

输出五个pid结果

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

int main(int argc, char *argv[])
{
    
    fork()&&fork()||fork();
    printf("pid :%d\n",getpid());
    return 0;
}

结果为:错行不用管

父子进程的关系

abort();//关闭

exit()函数代码:

#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
    printf("hello");
    exit(0);
    printf("aaaa\n");
    return 0;
}

结果:

_exit()函数代码:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
    printf("hello");
    _exit(0);
    printf("aaaa\n");
    return 0;
}

结果为:

区别:

正常就是进入缓冲区,满足上述四个条件就到屏幕上去

其中,exit()函数的上一行有或者没有\n等,都会显示出来,下面那一行不显示的

_exit()函数的上一行有\n等,就会显示,没有就啥也不是显示。同理,下面那行也是一直不显示的。

进程的退出:

孤儿进程代码(父结束):

 #include <stdio.h>
 #include <stdlib.h>
 #include <unistd.h>
 #include <string.h>
 #include <sys/types.h>
   
 int main(int argc, const char *argv[])
 {
     pid_t pid = fork();
     if(pid>0)
     {
        printf("father pid:%d ppid:%d\n",getpid(),getppid());
        exit(0);                                                           
  
  
     }
     else if(0 == pid)
     {
         sleep(3);
         printf("child pid:%d ppid:%d\n",getpid(),getppid());
         exit(0);
     }
     else
     {
         perror("fork");
         return 1;
     }
  
     return 0;
 }

结果为:

 

僵尸进程代码(子先结束,父后结束)(回收:父结束的时候如果有子还没结束,父就带着子一起结束):

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
   
int main(int argc, const char *argv[])
{
      pid_t pid = fork();
      if(pid>0)
      {
          printf("father pid:%d ppid:%d\n",getpid(),getppid());
          sleep(10);
  
  
      }
      else if(0 == pid)
      {
          printf("child pid:%d ppid:%d\n",getpid(),getppid());
          exit(0);                                                           
      }
      else
      {
          perror("fork");
          return 1;
      }
  
      return 0;
}

结果为:

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值