Linux system call using PHP to implement an online compiler running C language program ____linux

Source: Internet
Author: User
Tags error handling strlen file permissions

Some time ago to the school to do an online practice C language Programs and C language test system, the server architecture is lamp. Because the others are not much, to achieve online compilation as long as the Exec () function directly invoke GCC to compile the line, $compile _str = "gcc". $filename. "-O" $prog _name. " 2> "." Compile_result.txt;iconv-f UTF-8-T GB2312 "." Compile_result.txt-o Compile_res.txt ";

Here's an explanation of the command: $filename is your C language source program address, o specifies the build executable file name, 2>compile_result.txt is the output of the compilation results to Compile_ Result.txt file (GCC's compilation result is output to standard error 2 rather than standard output 1) The semicolon is the end of this statement, and the next statement is the code conversion command, because I want to return the GB2312 encoded character, So convert the previous compilation result file into GB2312 code and put it in the compile_res.txt. Here implementation is actually very superfluous, because you can completely enconv-l zh_cn-x UTF-8 filename directly, but the egg pain is the server actually does not have this tool. No way to use the iconv. If the customer wants is UTF-8 code, that direct 2>&1 OK, do not use temporary documents slightly ~

The next step is to run our C program on the client and output the program output to the browser. (and be able to read input from the user side in the browser) that's our big play.
First, our implementation is based on temporary files and system calls. Yes, if you want to put the user's input into the program inside, just this is not possible, below I say my implementation method:

$input = $_post[' input ']; Get user input $fp =fopen ($path. ' Input.txt ', ' w+ ');//Open File if (! $fp)//error handling {echo Open file failed; echo $path. ' Input.txt ';} $temp = Fwrite ($fp, $input); The user input is written to the file fclose ($FP); if (chdir ($path) = = False) {//Enter directory execution command echo "Enter directory failed:";} if (!chmod ("Run_me", 0777)) {//run_me program we'll talk about echo "Change file permissions failed";} $cmd _str = "./run_me". $prog _name. "Input.txt";//Command Statement Ech o "operation result:"; EXEC ($cmd _str, $arr); Executes the command to put the output into the array arr for ($i = 0; $i < count ($arr); $i + +) {echo $arr [$i]. " /n "; }

The main code is this, where the Run_me program is written in C, the user program as a child process, and the user program to close the standard input, through the pipeline to input.txt content into the user program. (Here you need to write the operation of the final output of the process under Linux.)
Run_me's source code is as follows:

#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <sys /types.h> #include <sys/stat.h> #include <errno.h> void Get_file_buf (char (*BUF) [Bayi], char *filename)// Gets the cache of the file to the data buffer {file *fp; int i = 0; if (fp = fopen (filename, "r") = = NULL) {perror ("can ' t open the file"); Exit (Exit_f Ailure); while (Fgets (Buf[i], 81,FP)!= NULL) {i++;}} int main (int argc, char **argv)//main function {int file_pipe[2], i = 0;//define pipe pid_t fork_result;//fork result Char row_input[100][81 ]; Define user input cache size char progname[255]; Defines the program name Char *filename; if (argc > 3) {printf ("Usage:run_me $programname $filename"); exit (exit_failure);}//initiaze buffer for (i = 0; i < 100; i++) {memset (progname, '/0 ', Bayi); memset (Row_input[i], '/0 ', Bayi);}//Strncat (Progname, "./", strlen ("./"); The user program is added here./Specify the current user strncat (Progname, argv[1], strlen (argv[1));//link parameter Pass program name if (argc = 2) {System (Progname); return 0 ; filename = argv[2]; Enter filename Get_file_buF (row_input, filename); i = 0; if (pipe (file_pipe) = = 0)//pipe initialization {Fork_result = fork ();//fork Process Generation subprocess if (Fork_result = = (pid_t)-1) {//Error handling Perror ("fork FA Ilure "); Exit (Exit_failure); } if (Fork_result = = (pid_t) 0)//subprocess operation {Close (0); DUP (file_pipe[0)); close (file_pipe[0); close (file_pipe[1]); EXECLP (pr Ogname, progname, NULL, NULL); Exit (Exit_failure); else//Parent process Action {close (file_pipe[0]), while (* (Row_input[i])!= '/0 ')//write buffer stream to child process {write (file_pipe[1), Row_input[i], Strle N (row_input[i])); i++; Close (file_pipe[1]); Usleep (10000); Let the program execute only 10 milliseconds execlp ("Killall", "Killall", argv[1), NULL);//End subprocess}} return 0; }

If you are familiar with the operation of Linux under the door of a friend must feel that my operation is a little redundant, directly run./prog_name < input.txt it's over. Oh, it is like this, but you have to think, if the user is a dead loop program, then, your server will not live too long time, if only at the system level limit to limit the system, but also can not guarantee a large number of users at the same time using the system. So I manually implemented a redirection operation, and added only to allow the user program to perform 10 milliseconds of elements, so that the problem to solve the ~ Oh, if there is a better way to hope that you recommend. You are also welcome to point out my mistakes and problems. What do not understand the place can also say that we discuss the discussion ~ ~

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.