Fork system Call

Source: Internet
Author: User

Fork () Learn, understand

Example 1: (Independent context)

Click ( here) to collapse or open

  1. #include <stdio.h>
  2. #include <errno.h>
  3. #include <stdlib.h>
  4. #include <unistd.h>
  5. #include <sys/types.h>
  6. #include <fcntl.h>
  7. #define ERROR (flag) \
  8. if (flag) \
  9. {                    \
  10. printf ("%d:", __line__); \
  11. Fflush (stdout); \
  12. Perror ("error"); \
  13. Exit (errno); \
  14. }
  15. int num = 999;
  16. int main (int argc,char *argv[])
  17. {
  18. pid_t pid;
  19. char *s = NULL;
  20. printf ("Hello, my pid is%d\n\n", getpid ());
  21. PID = fork ();
  22. ERROR (PID = =-1);
  23. if (PID = = 0)
  24. {
  25. int i = 2;
  26. while (i--)
  27. {
  28. printf ("child process, PID =%d, Ppid =%d\n\n",
  29. Getpid (), Getppid ());
  30. s = "child process";
  31. Sleep (1);
  32. }
  33. }
  34. Else
  35. {
  36. int i = 2;
  37. while (i--)
  38. {
  39. GetChar ();
  40. printf ("parent process, PID =%d, child pid =%d\n\n",
  41. Getpid (), PID);
  42. s = "parent process";
  43. Sleep (1);
  44. }
  45. }
  46. printf ("I ' m%s, Byebye!!! \ n ", s);
  47. return 0;
  48. }

The compile link runs successfully, the parent process outputs the current and child process numbers, and the child processes output the current and parent process numbers. Results such as:


Example 2: (Independent file reading and writing)
Code:1

Click ( here) to collapse or open

  1. #include <stdio.h>
  2. #include <errno.h>
  3. #include <stdlib.h>
  4. #include <unistd.h>
  5. #include <sys/types.h>
  6. #include <fcntl.h>
  7. #define ERROR (flag) \
  8. if (flag) \
  9. {                    \
  10. printf ("%d:", __line__); \
  11. Fflush (stdout); \
  12. Perror ("error"); \
  13. Exit (errno); \
  14. }
  15. int main (int argc,char *argv[])
  16. {
  17. #if 0
  18. int fd = open (argv[1],o_rdonly);
  19. ERROR (FD = =-1);
  20. #endif
  21. pid_t pid = fork ();
  22. ERROR (PID = =-1);
  23. #if 1
  24. int fd = open (argv[1],o_rdonly);
  25. ERROR (FD = =-1);
  26. #endif
  27. if (PID = = 0)
  28. {
  29. Char ch;
  30. int ret = read (fd,&ch,1);
  31. ERROR (Ret < 0);
  32. printf ("Child process got%c\n", ch);
  33. }
  34. Else
  35. {
  36. Char ch;
  37. int ret = read (fd,&ch,1);
  38. ERROR (Ret < 0);
  39. printf ("Parent process got%c\n", ch);
  40. }
  41. return 0;
  42. }

After the compile link runs successfully, the parent-child process outputs the contents of the file read out from the file. Results such as:


Example 3: Orphan process

Click ( here) to collapse or open

  1. #include <stdio.h>
  2. #include <errno.h>
  3. #include <stdlib.h>
  4. #include <unistd.h>
  5. #include <sys/types.h>
  6. #include <fcntl.h>
  7. #define ERROR (flag) \
  8. if (flag) \
  9. {                    \
  10. printf ("%d:", __line__); \
  11. Fflush (stdout); \
  12. Perror ("error"); \
  13. Exit (errno); \
  14. }
  15. int main (int argc,char *argv[])
  16. {
  17. pid_t pid;
  18. PID = fork ();
  19. ERROR (PID = =-1);
  20. if (PID = = 0)
  21. {
  22. int i = 1000;
  23. while (i--)
  24. {
  25. printf ("child process, PID =%d, Ppid =%d\n", Getpid (), Getppid ());
  26. Sleep (5);
  27. }
  28. }
  29. Else
  30. {
  31. printf ("Parent process, Byebye!!! \ n ");
  32. Sleep (3);
  33. _exit (0);
  34. }
  35. return 0;
  36. }

The compile link runs successfully after the parent-child process outputs the content. Results such as:

The inter-process relationships viewed at another terminal are as follows:


Example 4: Parent process Gets the return value of a child process

Click ( here) to collapse or open

  1. #include <stdio.h>
  2. #include <errno.h>
  3. #include <stdlib.h>
  4. #include <unistd.h>
  5. #include <sys/types.h>
  6. #include <fcntl.h>
  7. #include <sys/wait.h>
  8. #define ERROR (flag) \
  9. if (flag) \
  10. {                    \
  11. printf ("%d:", __line__); \
  12. Fflush (stdout); \
  13. Perror ("error"); \
  14. Exit (errno); \
  15. }
  16. int main (int argc,char *argv[])
  17. {
  18. pid_t pid;
  19. PID = fork ();
  20. ERROR (PID = =-1);
  21. if (PID = = 0)
  22. {
  23. int i = 3;
  24. while (i--)
  25. {
  26. printf ("child process, PID =%d, Ppid =%d\n\n",
  27. Getpid (), Getppid ());
  28. Sleep (1);
  29. }
  30. printf ("Chile process exit, Byebye!!! \ n ");
  31. _exit (99);
  32. }
  33. #if 1
  34. int stat;
  35. pid_t Child_pid;
  36. Child_pid = Wait (&stat);
  37. printf ("Child process has exited, PID =%d\n\n", child_pid);
  38. if (wifexited (stat))
  39. printf ("Child exited with code%d\n\n", Wexitstatus (stat));
  40. Else
  41. printf ("Child exit abnormally\n\n");
  42. #endif
  43. printf ("parent process, PID =%d\n", Getpid ());
  44. GetChar ();
  45. return 0;
  46. }

The compile link runs successfully after the parent-child process outputs the content. Results such as:

Example 5: Process exit

Click ( here) to collapse or open

    1. #include <stdio.h>
    2. #include <stdlib.h>
    3. #include <time.h>
    4. #include <sys/stat.h>
    5. #include <unistd.h>
    6. #include <sys/types.h>
    7. #include <errno.h>
    8. int main (int argc, char *argv[])
    9. {
    10. printf ("Hello");
    11. _exit (0);
    12. return 0;
    13. }

The compile link runs successfully, with no output content. Results such as:


After commenting out the _exit () function, the following

Click ( here) to collapse or open

    1. #include <stdio.h>
    2. #include <stdlib.h>
    3. #include <time.h>
    4. #include <sys/stat.h>
    5. #include <unistd.h>
    6. #include <sys/types.h>
    7. #include <errno.h>
    8. int main (int argc, char *argv[])
    9. {
    10. printf ("Hello");
    11. _exit (0);
    12. return 0;
    13. }

Compile the link to run successfully, output content such as:


_exit () causes the process to exit before character output to the display device.

Fork system Call

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.