Linux进程API

Linux下进程API

fork

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>


/*
linux环境运行
子进程并不是完全拷贝了父进程。具体来说,虽然它拥有自己的
地址空间(即拥有自己的私有内存)、寄存器、程序计数器等,但是它从 fork()返回的值是
不同的。父进程获得的返回值是新创建子进程的 PID,而子进程获得的返回值是 0。这个差
别非常重要,因为这样就很容易编写代码处理两种我同的情况(像上面那样)。
*/

int main(int argc, 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 parent of %d (pid:%d)\n", rc, (int)getpid());
}
return 0;
}

运行结果

wait

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
 // 在父进程的逻辑中增加这两行就行
#include <sys/wait.h>

int wc = wait(NULL);
-------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>

/*
linux环境运行
子进程并不是完全拷贝了父进程。具体来说,虽然它拥有自己的
地址空间(即拥有自己的私有内存)、寄存器、程序计数器等,但是它从 fork()返回的值是
不同的。父进程获得的返回值是新创建子进程的 PID,而子进程获得的返回值是 0。这个差
别非常重要,因为这样就很容易编写代码处理两种我同的情况(像上面那样)。
*/

int main(int argc, 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 parent of %d (pid:%d)\n", rc, (int)getpid());
}
return 0;
}

exec

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/wait.h>

/*
linux环境运行
子进程并不是完全拷贝了父进程。具体来说,虽然它拥有自己的
地址空间(即拥有自己的私有内存)、寄存器、程序计数器等,但是它从 fork()返回的值是
不同的。父进程获得的返回值是新创建子进程的 PID,而子进程获得的返回值是 0。这个差
别非常重要,因为这样就很容易编写代码处理两种我同的情况(像上面那样)。
*/

int main(int argc, 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());
char *myargs[3];
myargs[0] = strdup("wc");
myargs[1] = strdup("p3.c");
myargs[2] = NULL;
execvp(myargs[0],myargs); // 表示 wc p3.c 告诉我我该文件有多少行、多少单词,以及多少字节
printf("this shouldn't print out");
}
else // 父进程
{
int wc = wait(NULL);
printf("hello, I am parent of %d (pid:%d)\n", rc, (int)getpid());
}
return 0;

重定向的一个例子

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <sys/wait.h>
#include <sys/stat.h>

int main(int argc, 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) // 子进程
{
close(STDOUT_FILENO);
printf("hello, I am child(pid:%d)\n", (int)getpid());
open("p4.output", O_CREAT | O_WRONLY | O_TRUNC, S_IRWXU);

char *myargs[3];
myargs[0] = strdup("wc");
myargs[1] = strdup("p3.c");
myargs[2] = NULL;
execvp(myargs[0], myargs); // 表示 wc p3.c 告诉我我该文件有多少行、多少单词,以及多少字节
printf("this shouldn't print out");
}
else // 父进程
{
int wc = wait(NULL);
printf("hello, I am parent of %d (pid:%d)\n", rc, (int)getpid());
}
return 0;
}