进程

1.介绍

    现在的操作系统里,进程只是进行管理资源(进程参与调度时,频繁地从CPU的寄存器和进程堆栈的保存运行状态和对应的信息都很耗时,所以现代进程仅仅进行资源管理),真正参与执行的是线程,每个进程都会无形中默认的创建一个线程,让其至少能执行任务。

    Linux中定义进程描述符的结构体task_struct,在include/linux/sched.h中定义。

2.进程树

$ ps -ef
UID         PID   PPID  C STIME TTY          TIME CMD
root          1      0  0 00:29 ?        00:00:16 /sbin/init maybe-ubiquity
root          2      0  0 00:29 ?        00:00:00 [kthreadd]
root          3      2  0 00:29 ?        00:00:00 [rcu_gp]
root          4      2  0 00:29 ?        00:00:00 [rcu_par_gp]
root          6      2  0 00:29 ?        00:00:00 [kworker/0:0H]
root          8      2  0 00:29 ?        00:00:00 [mm_percpu_wq]
root          9      2  0 00:29 ?        00:00:00 [ksoftirqd/0]
。。。

root        582      1  0 00:29 ?        00:00:03 /usr/bin/vmtoolsd
root        598      1  0 00:29 ?        00:00:00 /lib/systemd/systemd-journald
root        602      2  0 00:29 ?        00:00:00 [iscsi_eh]
root        610      1  0 00:29 ?        00:00:00 /sbin/lvmetad -f

。。。
bo         1813   1812  0 00:30 ?        00:00:00 /usr/lib/openssh/sftp-server
bo         1817   1806  0 00:30 pts/0    00:00:00 -bash
root       1859      2  0 00:44 ?        00:00:00 [kworker/0:0-cgr]
root       1890   1114  0 00:57 ?        00:00:00 /usr/sbin/smbd --foreground --no-process-group
root       1936      2  0 01:47 ?        00:00:00 [kworker/u256:1-]
root       1952      2  0 02:10 ?        00:00:00 [kworker/u256:2-]
root       1958      2  0 02:16 ?        00:00:00 [kworker/u256:0-]
bo         1962   1817  0 02:18 pts/0    00:00:00 ps -ef

1号进程是init进程,2号进程是内核进程。

内核态的进程都带“[]”,而用户态进程不带;

每个进程PID都有父进程PPID;

TTY一列有“?”表示后台服务,不带的是前台启动的;

3.创建进程

fork调用后,程序会有两个分支往下执行;fork的返回值有三种情况:

(1)在父进程里pfd会大于0,并且pid的值是子进程的进程ID;

(2)在子进程里pfd会等于0;

(3)创建失败pfd等于-1;

因此可以通过pid的值来判断程序进入了哪个进程了:

#include<stdio.h>
#include<unistd.h>
int main(){
 pid_t pfd = -1;
 pfd = fork();//执行fork后,就会有两个分支“一模一样”进程
 if(pfd < 0){//pfd < 0创建失败
  printf("create fork failed!\n");
  return -1;
 }
 else if(pfd > 0){//pfd > 0 父进程
	printf("pfd = %d,ID = %d, this is parent process\n", pfd, getpid());
 }else{//pfd = 0 子进程
 	printf("pfd = %d,ID = %d, this is child process\n", pfd, getpid());
 }
 getchar();
 return 0;
}

父进程ID = 3125,子进程ID = 3126 (一般子进程的ID的值为:在父进程的ID加1

unistd.h 是 C 和 C++ 程序设计语言中提供对 POSIX 操作系统 API 的访问功能的头文件 

4.进程间通信

4.1 匿名管道

pipe的使用前提条件是:通信的双方进程是有亲缘关系的,也就是说在父子进程中通信。

#include<string.h>
#include<stdio.h>
#include<unistd.h>
int main(){
 pid_t pfd = -1;
 int pipefd[2] = {0};//管道fd,pipefd[0]---读取数,pipefd[1]---写入数据
 int ret = pipe(pipefd);
 if(ret != 0){
	printf("create pipe falied!\n");
 }
 pfd = fork();//执行fork后,就会有两个分支“一模一样”进程
 if(pfd < 0){//pfd < 0创建失败
  printf("create fork failed!\n");
  return -1;
 }
 else if(pfd > 0){//pfd > 0 父进程
	printf("pfd = %d,ID = %d, this is parent process\n", pfd, getpid());
	char msg[256] = {0};
        read(pipefd[0], msg, 256);
        printf("ID = %d,parent recved : %s\n", getpid(),msg);
 }else{//pfd = 0 子进程
 	printf("pid = %d,ID = %d, this is child process\n", pfd, getpid());
	char* msg = "I am your child!";
        write(pipefd[1], msg, strlen(msg));
	printf("ID = %d,child send : %s\n", getpid(),msg);
 }
 getchar();
 return 0;
}

 

4.2命名管道

fifo是可以在任何进程之间通信。

新建两个文件:fifo_wtite.c和fifo_read.c

fifo_wtite.c:

#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/types.h>
#include <fcntl.h>
#include <string.h>
#include<stdlib.h>

#define FIFO_PATH  "/tmp/myfifo"


int main(int argc, char** argv)
{

    if (-1 == access(FIFO_PATH, F_OK))
    {
        printf("myfifo is not exist, and try to create myfifo!\n");
        if (0 != mkfifo(FIFO_PATH, 0777))
        {
            printf("create myfifo  failed!\n");
            return -1;
        }
    }

    int fd = open(FIFO_PATH, O_WRONLY);
    if (-1 == fd)
    {
        printf("open myfifo failed!\n");
        return -1;
    }
    
    char buffer[256] = {0};
    char* msg = " write this msg!\n";
    char s[20] = {0};
    sprintf(s,"%d", getpid());
    memcpy(buffer, s, strlen(s));
    memcpy(buffer + strlen(s), msg, strlen(msg));
    int len = strlen(buffer);
    if( write(fd, buffer, len) > 0)
    {
        printf("ID = %d, write the msg is:%s \n",getpid(), buffer);
    }
    else{
       printf("write failed!");
    }

   while(1);

    return 0;
}

fifo_read.c:

#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <sys/types.h>
#include <fcntl.h>
#include <string.h>

#define FIFO_PATH  "/tmp/myfifo"

int main(int argc, char** argv)
{
    if (-1 == access(FIFO_PATH, F_OK))
    {
        printf("myfifo file is not exist, and try to create myfifo!\n");
    }
    
    int fd = open(FIFO_PATH, O_RDONLY);
    char buffer[256] = {0};
    int len = read(fd, buffer, 256);
    if ( len <= 0 )
    {
        printf("read myfifo nothing!\n");
        return -1;
    }

    printf("read msg is : %s\n", buffer);

    return 0;
}

 程序里设置阻塞的方式打开,即write必须等read返回,程序才往下走。

这种通信模式是同步的。

open的第二个参数设置为:O_WRONLY | O_NONBLOCK,即非阻塞打开,也就是说write不需要read返回程序就往下走,而不是一直等待对方读完。注意:设置非阻塞打开时,读的一方需要先打开,要不然写的一方会打开失败;上面的程序那怎么测试时非阻塞的呢(怎么证明设置非阻塞后就是非阻塞的)?

在读方只open ,不进行read,(读的一方在程序最后加上while(1),要不然程序就退出了),然后写方启动,看看是否不阻塞在write函数那里。

如下:

#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <sys/types.h>
#include <fcntl.h>
#include <string.h>

#define FIFO_PATH  "/tmp/myfifo"

int main(int argc, char** argv)
{
    if (-1 == access(FIFO_PATH, F_OK))
    {
        printf("myfifo file is not exist, and try to create myfifo!\n");
    }
    
    int fd = open(FIFO_PATH, O_RDONLY);
    char buffer[256] = {0};
    /*int len = read(fd, buffer, 256);
    if ( len <= 0 )
    {
        printf("read myfifo nothing!\n");
        return -1;
    }
*/
    printf("read msg is : %s\n", buffer);
   while(1);
    return 0;
}

4.3

5.其他

5.1孤儿进程

    一个父进程退出,而它的一个或多个子进程还在运行。孤儿进程将被init进程所收养,并由init进程对它们完成状态收集工作。

5.2僵尸进程

    一个进程使用fork创建子进程,如果子进程退出,而父进程并没有调用wait或waitpid获取子进程的状态信息,那么子进程的进程描述符仍然保存在系统中。一个进程在调用exit命令结束自己的生命的时候,其实它并没有真正的被销毁,而是留下一个称为僵尸进程。命令ps,可以看到有标记为Z的进程就是僵尸进程。

5.3守护进程

    不与任何终端关联的进程,通常情况下守护进程在系统启动时就在运行,守护进程脱离于终端,是为了避免进程在执行过程中的信息在任何终端上显示,并且进程也不会被任何终端所产生的终端信息所打断(比如关闭终端等)。

变为守护进程的步骤:

1.调用fork(),创建新进程,它会是将来的守护进程.
2.在父进程中调用exit,保证子进程不是进程组长

3.调用setsid()创建新的会话区

4.将当前目录改成跟目录(如果把当前目录作为守护进程的目录,当前目录不能被卸载他作为守护进程的工作目录)

5.将标准输入,标注输出,标准错误重定向到/dev/null.

#include <fcntl.h>
#include <unistd.h>
#include "daemon.h"

int daemon(int nochdir, int noclose)
{
    int fd;

    switch (fork()) {
    case -1:
        return (-1);
    case 0:
        break;
    default:
        _exit(0);
    }

    if (setsid() == -1)
        return (-1);

    if (!nochdir)
        (void)chdir("/");

    if (!noclose && (fd = open("/dev/null", O_RDWR, 0)) != -1) {
        (void)dup2(fd, STDIN_FILENO);
        (void)dup2(fd, STDOUT_FILENO);
        (void)dup2(fd, STDERR_FILENO);
        if (fd > 2)
            (void)close (fd);
    }
    return (0);
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值