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$ 
    
  • 相关阅读:
    关于C语言中类型的理解,有符号无符号的理解以及浮点数的理解
    关于集中注意力,情绪管理,记忆的总结整体
    关于链表逆置的问题
    git中reset和checkout的总结整理
    git中关于分支和stash的理解
    SVN和git的区别
    shell命令之find的用法
    (转载)获取服务器响应时间
    (转载)Spring定时任务的几种实现
    (转载)spring单例和多例详解。如何在单例中调用多例对象
  • 原文地址:https://www.cnblogs.com/feiwatson/p/15364126.html
Copyright © 2011-2022 走看看