Skip to content

Commit f3cb16f

Browse files
committed
feat: exec() 函数族:execl() / execlp()
1 parent 008b992 commit f3cb16f

File tree

4 files changed

+32
-0
lines changed

4 files changed

+32
-0
lines changed

unix_linux_09_exec/hello

8.28 KB
Binary file not shown.

unix_linux_09_exec/hello.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#include <stdio.h>
2+
#include <unistd.h>
3+
4+
int main(int argc, char *argv[])
5+
{
6+
while (true) {
7+
sleep(1);
8+
printf("hello world\n");
9+
}
10+
11+
return 0;
12+
}

unix_linux_09_exec/myexec

8.32 KB
Binary file not shown.

unix_linux_09_exec/myexec.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include <stdio.h>
2+
#include <unistd.h>
3+
4+
int main(int argc, char *argv[])
5+
{
6+
for (int i = 0; i < 3; i++)
7+
printf("-----i = %d\n", i);
8+
9+
pid_t pid = fork(); //创建子进程
10+
11+
if (pid == 0) {
12+
execl("/bin/ls", "占位参数", "-al", NULL); //ls 程序使用子进程的地址空间
13+
// execl("./hello", "占位参数", NULL); //一般是调用自己的程序,而非系统的程序
14+
}
15+
16+
for (int i = 0; i < 3; i++)
17+
printf("+++++i = %d\n", i);
18+
19+
return 0;
20+
}

0 commit comments

Comments
 (0)