Fork () Learn, understand
Example 1: (Independent context)
Click ( here) to collapse or open
- #include <stdio.h>
- #include <errno.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <sys/types.h>
- #include <fcntl.h>
- #define ERROR (flag) \
- if (flag) \
- { \
- printf ("%d:", __line__); \
- Fflush (stdout); \
- Perror ("error"); \
- Exit (errno); \
- }
- int num = 999;
- int main (int argc,char *argv[])
- {
- pid_t pid;
- char *s = NULL;
- printf ("Hello, my pid is%d\n\n", getpid ());
- PID = fork ();
- ERROR (PID = =-1);
- if (PID = = 0)
- {
- int i = 2;
- while (i--)
- {
- printf ("child process, PID =%d, Ppid =%d\n\n",
- Getpid (), Getppid ());
- s = "child process";
- Sleep (1);
- }
- }
- Else
- {
- int i = 2;
- while (i--)
- {
- GetChar ();
- printf ("parent process, PID =%d, child pid =%d\n\n",
- Getpid (), PID);
- s = "parent process";
- Sleep (1);
- }
- }
- printf ("I ' m%s, Byebye!!! \ n ", s);
- return 0;
- }
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
- #include <stdio.h>
- #include <errno.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <sys/types.h>
- #include <fcntl.h>
- #define ERROR (flag) \
- if (flag) \
- { \
- printf ("%d:", __line__); \
- Fflush (stdout); \
- Perror ("error"); \
- Exit (errno); \
- }
- int main (int argc,char *argv[])
- {
- #if 0
- int fd = open (argv[1],o_rdonly);
- ERROR (FD = =-1);
- #endif
- pid_t pid = fork ();
- ERROR (PID = =-1);
- #if 1
- int fd = open (argv[1],o_rdonly);
- ERROR (FD = =-1);
- #endif
- if (PID = = 0)
- {
- Char ch;
- int ret = read (fd,&ch,1);
- ERROR (Ret < 0);
- printf ("Child process got%c\n", ch);
- }
- Else
- {
- Char ch;
- int ret = read (fd,&ch,1);
- ERROR (Ret < 0);
- printf ("Parent process got%c\n", ch);
- }
- return 0;
- }
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
- #include <stdio.h>
- #include <errno.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <sys/types.h>
- #include <fcntl.h>
- #define ERROR (flag) \
- if (flag) \
- { \
- printf ("%d:", __line__); \
- Fflush (stdout); \
- Perror ("error"); \
- Exit (errno); \
- }
- int main (int argc,char *argv[])
- {
- pid_t pid;
- PID = fork ();
- ERROR (PID = =-1);
- if (PID = = 0)
- {
- int i = 1000;
- while (i--)
- {
- printf ("child process, PID =%d, Ppid =%d\n", Getpid (), Getppid ());
- Sleep (5);
- }
- }
- Else
- {
- printf ("Parent process, Byebye!!! \ n ");
- Sleep (3);
- _exit (0);
- }
- return 0;
- }
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
- #include <stdio.h>
- #include <errno.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <sys/types.h>
- #include <fcntl.h>
- #include <sys/wait.h>
- #define ERROR (flag) \
- if (flag) \
- { \
- printf ("%d:", __line__); \
- Fflush (stdout); \
- Perror ("error"); \
- Exit (errno); \
- }
- int main (int argc,char *argv[])
- {
- pid_t pid;
- PID = fork ();
- ERROR (PID = =-1);
- if (PID = = 0)
- {
- int i = 3;
- while (i--)
- {
- printf ("child process, PID =%d, Ppid =%d\n\n",
- Getpid (), Getppid ());
- Sleep (1);
- }
- printf ("Chile process exit, Byebye!!! \ n ");
- _exit (99);
- }
- #if 1
- int stat;
- pid_t Child_pid;
- Child_pid = Wait (&stat);
- printf ("Child process has exited, PID =%d\n\n", child_pid);
- if (wifexited (stat))
- printf ("Child exited with code%d\n\n", Wexitstatus (stat));
- Else
- printf ("Child exit abnormally\n\n");
- #endif
- printf ("parent process, PID =%d\n", Getpid ());
- GetChar ();
- return 0;
- }
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
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- #include <sys/stat.h>
- #include <unistd.h>
- #include <sys/types.h>
- #include <errno.h>
- int main (int argc, char *argv[])
- {
- printf ("Hello");
- _exit (0);
- return 0;
- }
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
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- #include <sys/stat.h>
- #include <unistd.h>
- #include <sys/types.h>
- #include <errno.h>
- int main (int argc, char *argv[])
- {
- printf ("Hello");
- _exit (0);
- return 0;
- }
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