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$ 
原文地址:https://www.cnblogs.com/feiwatson/p/15364126.html