zoukankan      html  css  js  c++  java
  • unix

    有三个主要用于进程控制的函数fork,exec, waitpid.
    exec函数有7中变体。

    通过这些进程控制函数,我们即可完成对进程创建、执行和终止等基本操作。进程的控制可以划分为三部曲,

    • 第一部:fork 创建新进程。

    • 第二部:exec 执行新程序。

    • 第三部:exit 和 wait 处理终止和等待终止。

    例程1,

    #include <stdio.h>          
    #include <stdlib.h>        
    #include <stddef.h>        
    #include <string.h>         
    #include <unistd.h>      
    #include <signal.h>
    
    
    int filter_dmesg(char *keyword)
    {
    	char line[1024];
    	FILE *fp;
    	int pipefds[2];
    	pid_t pid;
    	int ret;
    
    	ret = pipe(pipefds);
    	if (ret) {
    		perror("pipe");
    		return ret;
    	}
    	pid = fork();
    	if (pid == 0) {
    		close(pipefds[0]);
    		dup2(pipefds[1], STDOUT_FILENO);
    		execlp("dmesg", "dmesg", NULL);
    		perror("executing dmesg");
    		exit(1);
    	}
    	close(pipefds[1]);
    	fp = fdopen(pipefds[0], "r");
    	if (!fp) {
    		perror("fdopen(pipe)");
    		kill(pid, SIGTERM);
    
    		return -1;
    	}
    
    	while (fgets(line, 1024, fp)) {
    		if(strstr(line, keyword))
    		    printf("dmesg: %s", line);
    	}
    	fclose(fp);
    	waitpid(pid, NULL, 0);
    
    	return 0;
    }
    
    
    
    int
    main(int argc, char *argv[])
    {
    	if(argc!=2)
    	    printf("usage: cmd param1
    ");
    
    	return filter_dmesg(argv[1]);
    }
    
    
    ubuntu@ubuntu:~/unix-Code$ ./a.out raspberry
    dmesg: [    0.052162] raspberrypi-firmware soc:firmware: Attached to firmware from 2021-01-08 14:31, variant start
    dmesg: [    0.056172] raspberrypi-firmware soc:firmware: Firmware hash is 194a85abd768c7334bbadc3f1911c10a7d18ed14
    

    dup2()
    The dup2() system call performs the same task as dup(), but
    instead of using the lowest-numbered unused file descriptor, it
    uses the file descriptor number specified in newfd. In other
    words, the file descriptor newfd is adjusted so that it now
    refers to the same open file description as oldfd.

    例程2

    #include <stdio.h>          
    #include <stdlib.h>        
    #include <stddef.h>        
    #include <string.h>         
    #include <unistd.h>      
    
    #define MAXLINE 1024
    int
    main(void)
    {
        char buf[MAXLINE];
        pid_t pid;
        int status;
        printf("%%");
    
        while(fgets(buf, MAXLINE, stdin)!=NULL)
        {
            if(buf[strlen(buf)-1]=='
    ')
              buf[strlen(buf)-1]= 0;   /*replace new line with NULL*/
            
            if((pid=fork())<0)
            {
                perror("fork");
                return -1;
            }
            else if(pid==0)
            {
                execlp(buf, buf, NULL);
                perror("execlp");
                exit(127);           
            }
    
            if((pid=waitpid(pid, &status, 0))<0)
            {
                perror("waitpid");
            }
            printf("%%");
        }
    
        exit(0);
    
    }
    
    
    
    ubuntu@ubuntu:~/unix-Code$ ./a.out 
    %pwd
    /home/ubuntu/unix-Code
    %uname
    Linux
    %uname -r
    execlp: No such file or directory
    %ubuntu@ubuntu:~/unix-Code$ 
    
  • 相关阅读:
    远程登陆服务——SSH
    A web-based 3D modeling framework for a runner-gate design( 一种基于web的三维建模框架)
    Vue+Element+Echarts+Springboot+微信小程序开发(肿瘤医学项目)
    基于拖放布局的 Twitter Bootstrap 网站生成器
    Ubuntu下git使用华为云/gitee/github
    在 Ubuntu 18.04 上安装 Postman
    在Ubuntu 18.04上安装Git与入门教程
    转载:渲染层和逻辑层
    Ubuntu系统中安装Neo4j
    NosqlBooster连接数据库失败connect ECONNREFUSED 127.0.0.1:27017——mongodb连接失败
  • 原文地址:https://www.cnblogs.com/feiwatson/p/15364126.html
Copyright © 2011-2022 走看看