int pid = fork ();
if (PID > 0) {
printf ("parent:child=%d\n", PID);
PID = Wait ();
printf ("Child%d is done\n", PID);
} else if (PID = = 0) {
printf ("child:exiting\n");
Exit ();
} else {
printf ("fork error\n");
}
Fork creates a new process that returns two values and shares the same code equivalent to two threads concurrently executing
PID in parent process PID value for child process
The PID has a value of 0 in the child process
Output Result:
parent:child=1234
Child:exiting
The result is that the parent process executes to PID = Wait (); Time waits for the child process to exit but the child process does not exit
The =0 of the PID in the child process executes the statement of the else if (pid==0)
Parent:child 1234 is do this result is the parent process executing to PID = Wait ()
The child process has exited and is not running, so only one process of the parent process is running sequentially.
Exit function call exits the process and frees the resources that the process consumes
System Call Description
Fork () Create process
Exit () Terminate Current process
Wait () Wait for a child process to exit
Kill (PID) Terminate process pid
Getpid () Return current process ' s ID
Sleep (n) sleep for n seconds
EXEC (filename, *argv) Load a file and execute it
SBRK (n) Grow process ' s memory by n bytes
Open (filename, flags) open a file; Flags indicate Read/write
Read (FD, BUF, N) read n byes from a open file into buf
Write (fd, BUF, n) write n bytes to an open file
Close (FD) Release Open File fd
DUP (FD) Duplicate FD
Pipe (p) Create a pipe and return fd ' s in P
ChDir (dirname) Change the current directory
mkdir (dirname) Create a new directory
Mknod (name, major, minor) Create a device file
Fstat (FD) Return info about an open file
Link (f1, F2) Create another name (F2) for the file F1
Unlink (filename) Remove a file
Char *argv[3];
Argv[0] = "echo";
ARGV[1] = "Hello";
ARGV[2] = 0;
EXEC ("/bin/echo", argv);
printf ("exec error\n");
This code executes/bin/echo this program with a list of parameters. echo Hello this parameter first parameter echo can ignore most of the first parameter of the program
Can be ignored as the program name
The process for the shell to implement echo hello with parameters is as follows
8500 int main (void)
8502 {
8503 static char buf[100];
8504 int fd;
8505//read the command line input in the main loop with Geccmd
8506//assumes three file descriptors open.
8507 while (fd = open ("console", O_RDWR)) >= 0) {
8508 if (FD >= 3) {
8509 Close (FD);
8510 break;
8511}
8512}
8513
8514//Read and run input commands.
Read the command line input in the main loop with Geccmd
8515 while (Getcmd (buf, sizeof (BUF)) >= 0) {
8516 if (buf[0] = = ' C ' && buf[1] = = ' d ' && buf[2] = = ') {
8517//Clumsy but would have the to does for now.
8518//Chdir have no effect on the parent if run in the child.
8519 Buf[strlen (BUF)-1] = 0; Chop \ n
8520 if (chdir (buf+3) < 0)
8521 printf (2, "Cannot CD%s\n", buf+3);
8522 continue;
8523}
Create a child process to share the shell if the following code at this time the child process executes runcmd the parent process is in the wait state
If the user enters echo hello then runcmd will take "echo Hello" as the parameter
8524 if (fork1 () = = 0)
8525 Runcmd (Parsecmd (BUF));
8526 wait ();
8527}
8528 exit ();
8529}
The following is the code for Runcmd
8404//Execute cmd. Never returns.
8405 void
8406 runcmd (struct cmd *cmd)
8407 {
8408 int p[2];
8409 struct Backcmd *bcmd;
8410 struct ExecCmd *ecmd;
8411 struct Listcmd *lcmd;
8412 struct Pipecmd *pcmd;
8413 struct Redircmd *rcmd;
8414
8415 if (cmd = = 0)
8416 exit ();
8417
8418 switch (cmd->type) {
8419 Default:
8420 Panic ("Runcmd");
8421//performs exec (ecmd->argv[0], ECMD->ARGV) if executed here, and the function uses the Echo program instead of the RUNCMD program in the main program in the Echo
Calling the Exit function causes the child process to end the parent process wait () state will also end because the wait function waits until the child process hangs and returns the PID of the child process
8422 Case EXEC:
8423 ecmd = (struct execcmd*) cmd;
8424 if (ecmd->argv[0] = = 0)
8425 exit ();
8426 exec (ecmd->argv[0], ecmd->argv);
8427 printf (2, "exec%s failed\n", ecmd?>argv[0]);
8428 break;
8429
8430 Case REDIR:
8431 rcmd = (struct redircmd*) cmd;
8432 Close (RCMD->FD);
8433 if (open (Rcmd->file, Rcmd->mode) < 0) {
8434 printf (2, "open%s failed\n", rcmd?>file);
8435 exit ();
8436}
8437 Runcmd (Rcmd->cmd);
8438 break;
8439
8440 Case LIST:
8441 lcmd = (struct listcmd*) cmd;
8442 if (fork1 () = = 0)
8443 Runcmd (Lcmd?>left);
8444 wait ();
8445 Runcmd (lcmd?>right);
8446 break;
8450 Case PIPE:
8451 pcmd = (struct pipecmd*) cmd;
8452 if (pipe (p) < 0)
8453 Panic ("pipe");
8454 if (fork1 () = = 0) {
8455 Close (1);
8456 DUP (p[1]);
8457 Close (p[0]);
8458 Close (p[1]);
8459 Runcmd (Pcmd?>left);
8460}
8461 if (fork1 () = = 0) {
8462 Close (0);
8463 DUP (p[0]);
8464 Close (p[0]);
8465 Close (p[1]);
8466 Runcmd (pcmd?>right);
8467}
8468 Close (p[0]);
8469 Close (p[1]);
8470 wait ();
8471 wait ();
8472 break;
8473
8474 Case BACK:
8475 bcmd = (struct backcmd*) cmd;
8476 if (fork1 () = = 0)
8477 Runcmd (Bcmd?>cmd);
8478 break;
8479}
8480 exit ();
8481}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Process and memory simple anatomy of the flow of the echo hello to the console input