没有使用execl函数之前的用法
1 #include<stdio.h>
2 #include <unistd.h>
3 #include<stdio.h>
4 #include<fcntl.h>
5 #include<unistd.h>
6 #include<stdlib.h>
7 #include<string.h>
8
9 int main()
10 {
11 // pid_t getpid(void);
12 printf("before fork pid is %d\n",getpid());
13 // pid_t fork(void);
14 pid_t pid;
15 // pid = fork();
16 int num;
17 while(1){
18 printf("please input num\n");
19 scanf("%d",&num);
20 if(num == 1){
21 pid = fork();
22 if(pid == 0 ){
23
24 int fdsrc;
25 fdsrc = open("./text1.config",O_RDWR);
26 if(fdsrc == -1){
27 printf("open file failed\n");
28 }
29
30 int size;
31 size=lseek(fdsrc,0,SEEK_END);
32 lseek(fdsrc,0,SEEK_SET);
33
34 char *buf=NULL;
35 buf = (char*)malloc(sizeof(char)*size+1);
36 if(buf == NULL){
37 printf("malloc memary failed\n");
38 }
39
40 int n_read = read(fdsrc,buf,size);
41 if(n_read == -1){
42 printf("read failed\n");
43 }
44
45 char *point = strstr(buf,"SPEED=");
46 if(point == NULL){
47 printf("strstr failed\n");
48 }
49 point = point+strlen("SPEED=");
50 *point = '9';
51 // strcpy(point,"100");
52
53 lseek(fdsrc,0,SEEK_SET);
54 int n_write = write(fdsrc,buf,strlen(buf));
55 if(n_write == -1){
56 printf("write failed\n");
57 }
58
59 printf("read %d byte to buf\n,write %d byte to fddes file\n",n_read,n_write);
60
61 close(fdsrc);
62
63 }
64 }else if(pid > 0){
65
66 }
67
68 }
69 printf("wait,do nothing\n");
70
71 return 0;
72 }
使用了execl函数的用法
1 #include<stdio.h>
2 #include <unistd.h>
3
4 int main()
5 {
6 // pid_t getpid(void);
7 printf("before fork pid is %d\n",getpid());
8 // pid_t fork(void);
9 pid_t pid;
10 // pid = fork();
11 int num;
12 while(1){
13 printf("please input num\n");
14 scanf("%d",&num);
15 if(num == 1){
16 pid = fork();
17 if(pid == 0 ){
18 execl("./change","change","text1.config",NULL);
19
20 }else if(pid > 0){
21
22 }
23
24 }
25 printf("wait,do nothing\n");
26 }
27 return 0;
28 }