Fork and clone

Source: Internet
Author: User

Use fork to create a sub-process.

Code:
  1. # Include <unistd. h>
  2. # Include <stdio. h>
  3. Int main (INT argc, char ** argv)
  4. {
  5. Int A = 10;
  6. Pid_t PID;
  7. Printf ("before fork pid = % d/N", getpid ());
  8. PID = fork ();
  9. If (PID <0 ){
  10. Printf ("fork failed! /N ");
  11. Return-1;
  12. }
  13. If (pid = 0 ){
  14. A ++;
  15. Printf ("this is child process! Parent pid = % d, my pid = % d, pid = % d, A = % d/N ", getppid (), getpid (), PID, );
  16. }
  17. Else {
  18. Printf ("this is parent process! My pid = % d, child pid = % d, A = % d/N ", getpid (), PID, );
  19. }
  20. Return 0;
  21. }

Compile: [root @ localhost/] # gcc-O fork. c

Result:

[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
 

Next, clone the system call.

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. Char * prog_argv [4];
  11. Int Foo;
  12. Int FD;
  13. # Define child_stack (1024*64)
  14. Int thread_function (void * argument)
  15. {
  16. Printf ("Child: Child thread begin.../N ");
  17. Fool = 2008;
  18. Close (FD );
  19. Execvp (prog_argv [0], prog_argv );
  20. Printf ("Child: Child thread exit, this line won't print out./N ");
  21. Return 0;
  22. }
  23. Int main ()
  24. {
  25. Char C;
  26. Char * stack;
  27. Pid_t PID;
  28. Fool = 2007;
  29. FD = open ("/etc/passwd", o_rdonly );
  30. If (FD <0 ){
  31. Perror ("open ");
  32. Exit (-1 );
  33. }
  34. Printf ("parient: The variable Foo was: % d/N", foo );
  35. If (read (FD, & C, 1) <1 ){
  36. Perror ("parient: File Read error/N ");
  37. Exit (1 );
  38. }
  39. Else
  40. Printf ("parient: We cocould read from the file: % C/N", C );
  41. Prog_argv [0] = "/bin/ls ";
  42. Prog_argv [1] = "-1 ";
  43. Prog_argv [2] = "/";
  44. Prog_argv [3] = "null ";
  45. Stack = (char *) malloc (child_stack );
  46. If (stack = NULL ){
  47. Perror ("malloc: cocould not allocate stack ");
  48. Exit (2 );
  49. }
  50. Printf ("parent: creating child thread/N ");
  51. PID = clone (thread_function, (void *) (stack + child_stack ),
  52. Sigchld | clone_fs | clone_sighand | clone_vm, null );
  53. If (pid =-1 ){
  54. Perror ("clone ");
  55. Exit (3 );
  56. }
  57. Printf ("parent: waiting for the finish of child thread: % d/N", pid );
  58. PID = waitpid (PID, 0, 0 );
  59. If (pid =-1 ){
  60. Perror ("wait ");
  61. Exit (4 );
  62. }
  63. Free (stack );
  64. Printf ("parent: Child thread returned and stack free./N ");
  65. Printf ("parent: The variable Foo now is % d/N", foo );
  66. If (read (FD, & C, 1) <1 ){
  67. Perror ("parent: File Read error/N ");
  68. Exit (5 );
  69. }
  70. }
  71. Else
  72. Printf ("parent: We cocould read from file: % C/N", C );
  73. Return 0;
  74. }

Compile and execute:

[Root @ localhost/] # gcc-O Clone. C-lpthread
[Root @ localhost/] #./clone
Parient: The variable Foo was: 2007
Parient: We cocould 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 cocould read from file: O

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.