Search code examples
servletsjakarta-eeprocesscgi

What does it mean when a new process is started for every request sent to CGI?


I'm trying to understand servlets and advantages it offers over CGI. It was mentioned that a new process is started everytime in CGI and it is slow compared to servlet. Can someone explain what exactly is a process here and how servlet it beneficial over CGI ?


Solution

  • A CGI can be thought of a normal executable - it's a program that is run, does something, then ends. Like a dos or shell command. The issue is that there is a small amount of overhead in starting up such an executable, with the operating system allocating memory, loading the program into memory, running it, then deallocating everything. If you're running a website with many 100's of requests per second, this overhead can become significant, with potential many copies of this CGI ending up in memory should many concurrent HTTP requests hit the server.

    Servlets on the other hand, have its resources allocated just once, for one single instance held in memory. This single instance can process many HTTP requests concurrently, the single instance sharing it's allocated resources between all requests. This can be an issue - instance and static variables may be corrupted if two requests attempt to access. However, the advantages of efficiency and speed far outweighs this.