1、fork()系统调用:创建新的进程。
#include <stdio.h>
#include<stdilib.h>
#include<unistd.h>
int main(int args,char*argv[]){
printf("hello world(pid:%d)\n",(int)getpid());
int rc = fork();
if(rc < 0){
fprintf(stderr,"fork failed\n");
exit(1);
} else if(rc == 0){
printf("hello, I am child(pid:%d)\n",(int)getpid());
}else{
printf("hello, I am mother of %d(pid:%d)\n",rc (int)getpid());
}
return 0;
}
2、wait()系统调用:用于父进程等待子进程执行完毕
#include<stdilib.h>
#include<unistd.h>
int main(int args,char*argv[]){
printf("hello world(pid:%d)\n",(int)getpid());
int rc = fork();
if(rc < 0){
fprintf(stderr,"fork failed\n");
exit(1);
} else if(rc == 0){
printf("hello, I am child(pid:%d)\n",(int)getpid());
}else{
int wc = wait(NULL);
printf("hello, I am mother of %d(wc:%d)(pid:%d)\n",rc (int)getpid());
}
return 0;
}
3、exec()系统调用:让子进程执行与父进程不同的程序
prompt> ./p3
hello world (pid:29383)
hello, I am child (pid:29384)
29 107 1030 p3.c
hello, I am parent of 29384 (wc:29384) (pid:29383)
prompt>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/wait.h>
int main(int argc, char *argv[]){
printf("hello world (pid:%d)\n", (int) getpid());
int rc = fork();
if (rc < 0) { // fork failed; exit
fprintf(stderr, "fork failed\n");
exit(1);
} else if (rc == 0) { // child (new process)
printf("hello, I am child (pid:%d)\n", (int) getpid());
char *myargs[3];
myargs[0] = strdup("wc"); // program: "wc" (word count)
myargs[1] = strdup("p3.c"); // argument: file to count
myargs[2] = NULL; // marks end of array
execvp(myargs[0], myargs); // runs word count
printf("this shouldn't print out");
} else { // parent goes down this path (main)
int wc = wait(NULL);
printf("hello, I am parent of %d (wc:%d) (pid:%d)\n",
rc, wc, (int) getpid());
}
return 0;
}
给定可执行程序的名称(如wc)及需要的参数后,exec()会从可执行程序中加载代码和静态数据,并用它覆写自己的代码段(以及静态数据),堆、栈及其他内存空间也会被重新初始化。然后操作系统就执行该程序,将参数通过argv传递给该进程。它并没有创建新进程,而是直接将当前运行的程序替换为不同的运行程序(wc)。
4、shell
shell是一个用户成簇,它首先显示一个提示符(prompt),然后等待用户输入。用户输入一个可执行程序的名称以及需要的参数后,shell可以在文件系统中找到这个可执行程序,调用fork()创建新的进程,并调用exec()的某个变体来执行这个可执行程序,调用wait()等待该命令完成,子进程结束后,shell从wait()返回并再次输出一个提示符,等待用户输入吓一跳命令。
prompt> wc p3.c > newfile.txt
wc的输出结果被重定向(redirect)到文件newfile.txt中(通过newfile.txt之前的大于号来指明重定向)。shell实现结果重定向的方式也很简单,当完成子进程的创建后,shell在调用exec()之前先关闭了标准输出(standard output),打开了文件newfile.txt。这样,即将运行的程序wc的输出结果就被发送到该文件,而不是打印在屏幕上。
prompt> ./p4
prompt> cat p4.output
32 109 846 p4.c
prompt>
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
#include<fcntl.h>
#include<sys/wait.h>
int main(int argc,char *argv[]){
int rc = fork();
if(rc<0){
fprintf(stderr,"fork failed\n");
exit(1);
}else if(rc == 0){
close(STDOUT_FILENO);
open("./p4.output",O_CEREAT|O_WEONLY|O_TRUNC,S_IRWXU);
char *myargs[3];
myargs[0]=strdup("wc");
myargs[1]=strdup("p4.c");
myargs[2]=NULL;
execvp(myargs[0],myargs);
}else{
int wc = wait(NULL);
}
return 0;
}
5、pipe()系统调用
一个进程的输出被链接到了一个内核管道(pipe)上(队列),另一个进程的输入也被连接到了同一个管道上。前一个进程的输出无缝地作为后一个进程的输入,许多命令可以用这种方式串联在一起,共同完成某项任务。
grep -o foo file|wc -l 将grep、wc命令用管道连接可以完成从一个文件中查找某个词,并统计其出现次数的功能