This is the 18th day of my participation in the August Challenge

Xv6 multi-process programming

Xv6-riscv-book 1.1 Processes and Memory

This article refers to xv6-RISCV-book to introduce how to use Xv6 system call to achieve multi-process programming. (Just complete the code from the book with a complete implementation from the real system.)

The system calls describe
int fork() Create a process (by copying the current process) that returns the child process PID
int exit(int status) To terminate the current process, status is reported to wait() with no return value
int wait(int *status) Wait for a child process to exit, write the exit status (exit de status) to status, return the exiting child process PID
int exec(char *file, char *argv[]) Loads a file and executes it with the specified parameters. Error return

fork & wait

The fork system call copies the current process, creates a process, and returns a child process PID.

Wait waits for a child of the current process to exit (call exit).

Xv6

(For information on how to write your own user programs in Xv6, please refer to my previous article: Writing your own user programs in Xv6.)

A complete code implementation of the use of fork example:

#include "kernel/types.h"
#include "kernel/stat.h"
#include "user/user.h"

int main(a) {
	int pid = fork();
	if(pid > 0) {
		printf("parent: child=%d\n", pid);
		pid = wait((int *) 0);
		printf("child %d is done\n", pid);
	} else if (pid == 0) {
		printf("child: exiting\n");
		exit(0);
	} else {
		printf("fork error\n");
	}

	exit(0);
}
Copy the code

Note that the printf thread provided in Xv6 is not safe, and the characters printed by the running program may be mixed randomly:

$ usefork   # This is a little better
parent: child=c5
hild: exiting
child 5 is done
$ usefork  # this is very messy
cphairledn:t :e xcihtiilndg=
7
child 7 is done
Copy the code

Real Unix

On a real * NIx system (macOS 11, GCC 10 as an example), this code would be written as:

// usefork.c for macOS GCC

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main(a) {
	int pid = fork();
	if(pid > 0) {
		printf("parent: child=%d\n", pid);
		pid = wait((int *) 0);
		printf("child %d is done\n", pid);
	} else if (pid == 0) {
		printf("child: exiting\n");
		// sleep(2);
		exit(0);
	} else {
		printf("fork error\n");
	}

	return 0;
}
Copy the code

The effect of running in the real system will be better, generally no character mixing situation:

$ gcc-10 usefork.c ; ./a.out 
parent: child=3598
child: exiting
child 3598 is done
Copy the code

exec

The exec system call loads an executable file and replaces it with its own program to execute with specified parameters.

Note that exec replaces the program if it succeeds. So the code under exec will not execute. Once the code behind exec is executed, exec has failed. So exec is usually followed by failure handling code.

Xv6

Useexec. c in Xv6:

Notice the difference between the include library and the real world.

#include "kernel/types.h"
#include "kernel/stat.h"
#include "user/user.h"

int main(a) {
	char *argv[3];

	argv[0] = "echo";
	argv[1] = "hello";
	argv[2] = 0;

	exec("echo", argv);
	// If the exec succeeds, the following will not be executed:
	printf("exec error\n");

	exit(0);
}
Copy the code

Compile and run:

$ useexec
hello
Copy the code

Real Unix

Under macOS useexec. C:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main(a) {
	char *argv[3];

	argv[0] = "echo";
	argv[1] = "hello";
	argv[2] = 0;

	execv("/bin/echo", argv);
	// execv("/bin/echooooo", argv); // an error one
	printf("exec error\n");
}

Copy the code

Compile and run:

$ gcc-10 useexec.c ; ./a.out 
hello
Copy the code

By CDFMLR 2021-02-18

echo “See you.”

The picture at the top is from xiaowei API, which is randomly selected and only used to test the mechanical and photoelectric performance of the screen. It has nothing to do with any content or opinion of the article, and does not mean that I agree with, support or oppose any content or opinion in part or in whole. If there is infringement, contact delete.