用fork系统调用,clone系统调用

来一段C代码,用fork创建一个子进程

Code:
  1. #include <unistd.h>   
  2. #include <stdio.h>   
  3.   
  4. int main( int argc, char **argv)   
  5. {   
  6.     int a=10;   
  7.     pid_t pid;   
  8.   
  9.     printf("before fork pid=%d/n",getpid());   
  10.   
  11.     pid = fork();   
  12.   
  13.     if( pid<0){   
  14.         printf("fork failed!/n");   
  15.         return -1;   
  16.     }   
  17.   
  18.    if ( pid == 0 ){   
  19.         a++;   
  20.         printf("This is child process! parent pid=%d,my pid=%d,  pid=%d, a=%d/n",getppid(),getpid(),pid,a);   
  21.    }   
  22.    else{   
  23.       printf("This is parent process! my pid=%d, child pid=%d, a=%d/n", getpid(), pid,a);   
  24.    }   
  25.   
  26.    return 0;   
  27.        
  28. }  

编译:[root@localhost /]# gcc -o fork fork.c

结果:

[root@localhost /]# ./fork
before fork pid=23703
This is child process! parent pid=23703,my pid=23704,  pid=0, a=11
This is parent process! my pid=23703, child pid=23704, a=10
 

再来一段,clone系统调用

Code:
  1. #include <stdlib.h>   
  2. #include <sched.h>   
  3. #include <signal.h>   
  4. #include <unistd.h>   
  5. #include <malloc.h>   
  6. #include <fcntl.h>   
  7. #include <sys/types.h>   
  8. #include <sys/stat.h>   
  9. #include <sys/wait.h>   
  10.   
  11. char *prog_argv[4];   
  12. int foo;   
  13. int fd;   
  14.   
  15. #define CHILD_STACK (1024*64)   
  16.   
  17. int thread_function(void *argument)   
  18. {   
  19.         printf("CHILD:child thread begin.../n");   
  20.         foo=2008;   
  21.         close(fd);   
  22.         execvp(prog_argv[0],prog_argv);   
  23.   
  24.         printf("CHILD:child thread exit , this line won't print out./n");   
  25.   
  26.         return 0;   
  27. }   
  28.   
  29. int main()   
  30. {   
  31.         char c;   
  32.         char *stack;   
  33.         pid_t pid;   
  34.   
  35.         foo=2007;   
  36.   
  37.         fd=open("/etc/passwd",O_RDONLY);   
  38.         if(fd<0){   
  39.                 perror("open");   
  40.                 exit(-1);   
  41.         }   
  42.   
  43.         printf("PARIENT : the variable foo was :%d /n",foo);   
  44.         if(read(fd,&c,1)<1){   
  45.                 perror("PARIENT:File read error/n");   
  46.                 exit(1);   
  47.         }   
  48.         else  
  49.                 printf("PARIENT : We could read from the file:%c/n",c);   
  50.              
  51.         prog_argv[0]="/bin/ls";   
  52.         prog_argv[1]="-1";   
  53.         prog_argv[2]="/";   
  54.         prog_argv[3]="NULL";   
  55.   
  56.         stack = (char*)malloc(CHILD_STACK);   
  57.         if(stack == NULL){   
  58.                 perror("malloc : could not allocate stack");   
  59.                 exit(2);   
  60.         }   
  61.   
  62.         printf("PARENT:Creating child thread/n");   
  63.   
  64.         pid=clone(thread_function,(void*)(stack+CHILD_STACK),   
  65.                 SIGCHLD|CLONE_FS|CLONE_SIGHAND|CLONE_VM,NULL);   
  66.         if(pid == -1){   
  67.                 perror("clone");   
  68.                 exit(3);   
  69.         }   
  70.         printf("PARENT:Waiting for the finish of child thread :%d /n",pid);   
  71.         pid=waitpid(pid,0,0);   
  72.         if(pid == -1){   
  73.                 perror("wait");   
  74.                 exit(4);   
  75.         }   
  76.   
  77.         free(stack);   
  78.         printf("PARENT:Child thread returned and stack free./n");   
  79.   
  80.         printf("PARENT:The variable foo now is %d/n",foo);   
  81.         if(read(fd,&c,1)<1){   
  82.                 perror("PARENT:File read error/n");   
  83.                 exit(5);   
  84.         }   
  85.         }   
  86.         else  
  87.                 printf("PARENT:we could read from file:%c/n",c);   
  88.         return 0;   
  89. }   

编译,执行:

[root@localhost /]# gcc -o clone clone.c -lpthread
[root@localhost /]# ./clone
PARIENT : the variable foo was :2007
PARIENT : We could read from the file:r
PARENT:Creating child thread
PARENT:Waiting for the finish of child thread :24951
CHILD:child thread begin...
CHILD:child thread exit , this line won't print out.
PARENT:Child thread returned and stack free.
PARENT:The variable foo now is 2008
PARENT:we could read from file:o
 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值