应用:exec族函数配合fork函数

没有使用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 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值