How do I create websites that compile code online?

Source: Internet
Author: User
Just like some code competition websites, you can directly upload code to view the running results on the webpage. The official go language website also has similar functions. How to implement it? Is there a ready-made solution? Just like some code competition websites, you can directly upload code to view the running results on the webpage. The official go language website also has similar functions. How to implement it? Is there a ready-made solution? Reply content:

If you need a ready-made solution, this is good:


Effect preview:

Compiler Explorer

If it is implemented in a traditional method-based programming language, it involves a lot of content, including:

  • Components for Web Server + Web server interaction with backend programs (this can be used as a ready-made component, I use nginx + FCGI)
  • Backend Program

The back-end core is a compilation tool that calls an API similar to execl. When using this API, You have to carefully process the pipeline and export the output content to the webpage.

However, this type of program, like OJ, is a public service on the network and allows users to upload code for compilation and execution. The problem is mainly caused by security processing, the operating system is highly dependent on security issues.

Taking Linux as an example, security includes but not only the following:

  1. Dependent environment construction, Sandbox
  2. Running identity, including GID and UID
  3. Resource restrictions, including socket, file operations, core dump size, number of files, number of sub-processes, etc.
  4. Syscall restrictions
  5. Execution time limit, including running time and CPU time
  6. File System pressure

In Windows, there are another bunch of problems to solve, which are much more complicated than in Linux (maybe I don't use Windows too much ).

I know one person and I have implemented something similar to Haskell. I have a webpage running online similar to Go Playground. I asked him how to get everything done so quickly. He said, "The whole language is implemented using JS, so a browser can run without the backend." This is also an idea.

In the course design two days ago, the root student designed a simple online competition platform, and often played on the website. Let's simply put it into practice.

After submitting the code to the website, save it as code. In PHP or JSP, there is something that calls system commands, such as the exec function in PHP. In this case, you need a script for question determination to compile and run the obtained code, obtain the running information, and save it to the database. For example, you can write an implementation statement in php:
Exec ("./onj $ destFile $ file", $ output, $ verdict );
// From onj project-Processfile. php-line 105
This is to call the question determination function. In principle, it can be written in any language. Onj is written in python to compile, run, and judge the code. It needs to get the standard input of a competition question, redirect the standard output to a file, and then compare the standard answer to determine whether the Code submitted by the user can produce the correct result.
This system is much better on the Internet. We recommend the following:
1. ONJ Php, the code is very neat, I like it very much, Chinese support is not perfect.
2. NEUOJ The question is written in python.
3. HUSTOJ It seems that there are many domestic applications and I have never used them.

// I wrote it too ugly to post it.
People who engage in ACM in such competition websites are relatively familiar with it. If you know it, you can ask. The problem of directly using the web process fork is that the web is lightweight, and the concurrency is quite large.
Compilation and running are heavyweight and cannot have too many concurrencies and must be controlled.
Therefore, there should be a task queue mechanism in sequence.
As for security, you can open another topic. You should call the compilation command directly to check the output result.
For security issues, you can use a user with a low permission level to do this.

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: 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.