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$ 
    
  • 相关阅读:
    vsprintf函数和va_list用法详解
    copy_from_user 详解
    OK6410之蜂鸣器buzzer字符驱
    树莓派调试PCF8591遇到的小问题
    APM关键姿态控制源码讲解
    APM姿态控制流程
    没有安装gawk
    git 第一次上传本地代码到远程仓库,解决 ! [rejected] master -> master (non-fast-forward)错误
    C++ 全面刨析使用指针方法 _new _delete
    typedef&define的用法与区别
  • 原文地址:https://www.cnblogs.com/feiwatson/p/15364126.html
Copyright © 2011-2022 走看看