---
title: "CGI script execution mechanism"
subject: "Web Technology"
chapter: 3
key: "semester_7/web_technology:cgi-execution-mechanism"
source: https://www.bctengineer.com/q/semester_7/web_technology/cgi-execution-mechanism
site: BCT Engineering
---

# CGI script execution mechanism

Web Technology · Chapter 3: Server-side programming

- Asked in: 2076 Chaitra, 6 marks; 2068 Chaitra (old course), 6 marks (asked 2 times)
- Video: [4.1 Introduction to CGI and Servlet](https://www.youtube.com/watch?v=6Z0k9UfxvpE) — OU Education
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_7/web_technology/cgi-execution-mechanism
- Whole chapter: https://www.bctengineer.com/md/semester_7/web_technology/chapter-3

## Question

Explain the CGI server side script execution mechanism.

## Short answer

The web server recognizes a CGI URL, forks a new process for the script, passes request data through environment variables and stdin for POST, and the script writes headers and HTML to stdout, which the server sends back. One process per request makes CGI slow.

## Answer

**CGI (Common Gateway Interface)** is a standard that defines how a web server passes a request to an external program (CGI script, written in Perl, C, Python, shell, etc.), and how the program returns its output to the server so that dynamic pages can be created.

#### Execution mechanism
1. The browser sends an HTTP request for a CGI URL, e.g. `http://site.com/cgi-bin/search.pl?q=web`.
2. The web server recognises, from the URL or directory (`/cgi-bin/`), that this is a program, not a static file.
3. The server **creates a new process** and starts the script.
4. Request information is passed through **environment variables** (`REQUEST_METHOD`, `QUERY_STRING`, `CONTENT_LENGTH`, `REMOTE_ADDR`, `HTTP_USER_AGENT`). For POST, the form data is passed through **standard input (stdin)**.
5. The script processes the data, possibly querying a database or files.
6. The script writes to **standard output (stdout)** a header (e.g. `Content-type: text/html`), a blank line, then the HTML body.
7. The server reads this output, adds HTTP headers (status line) and sends the response to the browser.
8. The CGI process terminates.

```text
Browser --HTTP request--> Web server
                            | fork new process
                            | env vars + stdin
                            v
                       CGI script --> (DB / files)
                            | stdout (headers + HTML)
                            v
Browser <--HTTP response-- Web server
```

#### Example (Perl)
```perl
#!/usr/bin/perl
print "Content-type: text/html\n\n";
print "<html><body><h1>Hello from CGI</h1></body></html>";
```

#### Disadvantages
A new process per request is slow and uses memory, which does not scale well. Improved alternatives: FastCGI, mod_perl, servlets and PHP modules.

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. How does the server run a CGI script per request?
   A. It reuses one thread
   B. It loads the script once at startup
   C. It creates a new process
   D. It runs it in the browser
   Answer: C. A new process per request is the main disadvantage. (Key point: Process creation)

2. How does the script receive request information?
   A. Cookies only
   B. Command line from the browser
   C. Via DNS
   D. Environment variables, and stdin for POST data
   Answer: D. QUERY_STRING and REQUEST_METHOD are env vars; POST body comes via stdin. (Key point: Passing data)

3. What must the script output first?
   A. A header like Content-type, a blank line, then the body
   B. The HTML body, then headers
   C. Only the status line
   D. A DNS record
   Answer: A. Server reads stdout and adds the status line. (Key point: Output format)

4. Why is CGI slow and not scalable?
   A. It is written in Perl
   B. A new process per request uses time and memory
   C. It needs a browser plugin
   D. It uses UDP
   Answer: B. Alternatives like FastCGI and servlets avoid this. (Key point: Disadvantage)

## Related in Web Technology

- [Client-side versus server-side scripting](https://www.bctengineer.com/question.md?key=semester_7%2Fweb_technology%3Aclient-vs-server-side-scripting) — Web Technology, chapter 3, asked 3×
- [Server-side programming and Java Servlet](https://www.bctengineer.com/question.md?key=semester_7%2Fweb_technology%3Aserver-side-programming-servlet) — Web Technology, chapter 3
- [Server side languages and multi-tier architecture](https://www.bctengineer.com/question.md?key=semester_7%2Fweb_technology%3Aserver-side-languages-multitier) — Web Technology, chapter 3
- [Short note: Server side programming](https://www.bctengineer.com/question.md?key=semester_7%2Fweb_technology%3Ashort-note-server-side-programming) — Web Technology, chapter 3

## Related in other subjects

- [Position analysis of a four-bar mechanism](https://www.bctengineer.com/question.md?key=semester_6%2Ftheory_of_mechanism_and_machine_i%3Afour-bar-position-analysis-numerical) — Theory of Mechanism and Machine I, chapter 2
