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$ 
    
  • 相关阅读:
    100个经典的动态规划方程
    poj 2105 IP Address
    codeforces 299 A. Ksusha and Array
    codeforces 305 C. Ivan and Powers of Two
    性能测试实战 | 修改 JMeter 源码,定制化聚合压测报告
    Pytest 测试框架训练营,测试大咖带你搞定自动化+接口测试实战 | 限时免费报名
    MTSC 测试开开发大会(深圳站),报名立减 100 元!
    测试人面试 BAT 大厂之前,需要做好哪些准备?
    iOS 自动化测试踩坑(二):Appium 架构原理、环境命令、定位方式
    【北京】美团打车招聘职位
  • 原文地址:https://www.cnblogs.com/feiwatson/p/15364126.html
Copyright © 2011-2022 走看看