Concept explainers
Explanation of Solution
Implementation of a concurrent version of the TINY web server on I/O threads:
Modified “main.c” code:
#include "csapp.h"
//Function declaration
void doit(int fd);
void read_requesthdrs(rio_t *rp);
int parse_uri(char *uri, char *filename, char *cgiargs);
void serve_static(int fd, char *filename, int filesize);
void get_filetype(char *filename, char *filetype);
void serve_dynamic(int fd, char *filename, char *cgiargs);
void clienterror(int fd, char *cause, char *errnum,char *shortmsg, char *longmsg);
void *thread(void *vargp);
//Main function
int main(int argc, char **argv)
{
//Declare variable
int listenfd, connfd;
int *connfdp;
pthread_t tid;
char hostname[MAXLINE], port[MAXLINE];
socklen_t clientlen;
struct sockaddr_storage clientaddr;
/* Check the command line argument. if the argument does not satify, then */
if (argc != 2)
{
//Display the below error message
fprintf(stderr, "usage: %s <port>\n", argv[0]);
fprintf(stderr, "use default port 5000\n");
listenfd = Open_listenfd("5000");
}
//Otherwise,
else
{
//Call the Open_listenfd method
listenfd = Open_listenfd(argv[1]);
}
//Check condition
while (1)
{
//Get the length the client address
clientlen = sizeof(clientaddr);
//For connection accept
connfd = Accept(listenfd, (SA *)&clientaddr, &clientlen);
//Call the method
Getnameinfo((SA *) &clientaddr, clientlen, hostname, MAXLINE,
port, MAXLINE, 0);
//Display the accepted connection
printf("Accepted connection from (%s, %s)\n", hostname, port);
connfdp = (int*)Malloc(sizeof(int));
*connfdp = connfd;
//Call Pthread_create method
Pthread_create(&tid, NULL, thread, connfdp);
}
}
//Function definition for thread method
void *thread(void *vargp)
{
//Compute the connection file descriptor number
int connfd = *(int*)vargp;
//Call pthread detach method
Pthread_detach(Pthread_self());
//Free the given file
Free(vargp);
//Call doit method
doit(connfd);
//Close the connection
Close(connfd);
return NULL;
}
/* Function definition for doit method that is for handle HTTP request or response transfer */
void doit(int fd)
{
//Declare variable
int is_static;
struct stat sbuf;
char buf[MAXLINE], method[MAXLINE], uri[MAXLINE], version[MAXLINE];
char filename[MAXLINE], cgiargs[MAXLINE];
//Create object for rio function
rio_t rio;
/* Read request line and headers */
Rio_readinitb(&rio, fd);
//Check read request
if (!Rio_readlineb(&rio, buf, MAXLINE))
return;
//Display the buffer
printf("%s", buf);
//Display the parse request
sscanf(buf, "%s %s %s", method, uri, version);
//Check condition
if (strcasecmp(method, "GET"))
{
//Call client error method
clienterror(fd, method, "501", "Not Implemented","Tiny does not implement this method");
return;
}
//Call read_requestdrs method
read_requesthdrs(&rio);
/* Parse URI from GET request */
is_static = parse_uri(uri, filename, cgiargs);
//Check the static connection
if (stat(filename, &sbuf) < 0)
{
//Call client error method
clienterror(fd, filename, "404", "Not found",
"Tiny couldn't find this file");
return;
}
/* Check serve static content */
if (is_static)
{
if (!(S_ISREG(sbuf.st_mode)) || !(S_IRUSR & sbuf.st_mode))
{
//Call client error method
clienterror(fd, filename, "403", "Forbidden","Tiny couldn't read the file");
return;
}
//Call serve_static method
serve_static(fd, filename, sbuf.st_size);
}
//Otherwise check the serve dynamic content
else
{
if (!(S_ISREG(sbuf.st_mode)) || !(S_IXUSR & sbuf.st_mode))
{
//Call client error method
clienterror(fd, filename, "403", "Forbidden","Tiny couldn't run the CGI program");
return;
}
//Call serve dynamic method
serve_dynamic(fd, filename, cgiargs);
}
}
/* Function definition for read_requestdrs that is for read HTTP request headers */
void read_requesthdrs(rio_t *rp)
{
//Declare variable
char buf[MAXLINE];
Rio_readlineb(rp, buf, MAXLINE);
printf("%s", buf);
//Check condition
while(strcmp(buf, "\r\n"))
{
//Call rio readlineb function
Rio_readlineb(rp, buf, MAXLINE);
printf("%s", buf);
}
return;
}
/* Function definition for parse uri into file nane and cgi argument */
int parse_uri(char *uri, char *filename, char *cgiargs)
{
//Declare char variable
char *ptr;
//Check content type...
Want to see the full answer?
Check out a sample textbook solutionChapter 12 Solutions
Computer Systems: A Programmer's Perspective (3rd Edition)
- Write a Java program using Thread with single Server and Multiple Clients NOTE: Client must be running until Bye message is sent. Server should never stop I need answer question pls one server two clientarrow_forwardWrite a MultiThreaded ServerSocket Program in C# that can handle multiple clients at the same time. a C# Server Socket use TcpListener Class and listen to PORT 9393. When the C# Server Socket gets a request from Client side, the Server passes the instance of the client request to a separate class handleClient. For each Client request, there is a new thread instant is created in C# Server for separate communication with Client.arrow_forwardModify the above thread program so that you implement the interface Runnable. Make themain thread waiting till all other threads finish execution.arrow_forward
- Write a multithreaded Hello World program using OpenMP. Have each thread say “Hello world” along with its thread ID and the number of threads the program is using.Run the program with as many threads as your computer will allow. Then try setting the thread count to a different number. please post:- The source code.- A screenshot of the program running.- A screenshot of the program running with a different thread count.arrow_forwardIt was previously stated that only one method may be in use by a given thread in the JVM at any one moment. Why do you think this is true?arrow_forwardWrite a Java program using Thread with single Server and Multiple Clients NOTE: Client must be running until Bye message is sent. Server should never stoparrow_forward
- Prior to this conversation, we said that each JVM thread could only use a single method at a time. What about this circumstance causes you to think this way?arrow_forwardhow can I write a client program that send 10 one after the other by a thread and by user. and the server recieves the 10 messages and write a different 10 messages.arrow_forwardWrite a Java program using threads to compute the first 15 natural numbers, and to compute the first 50 Fibonacci numbers. Set the priority of thread that computes Fibonacci number to 9 and the other to 5. After calculating 30 Fibonacci numbers, make that thread to sleep and take up natural number computation. After computing the 15 natural numbers continue the Fibonacci number computing.arrow_forward
- perform several operations at the same time. * Each Thread that you run works separately from the rest of the commands in the program, and therefore in the event of any error occurring in the Thread, it will not affect the rest of the commands in the program, as it does not affect any other Thread running in the program. In light of your study of multiple channels, • Write a program in the Java language that demonstrates this concept - you can choose the nature of the program that you want to achievearrow_forwardWrite a GUI Java program to implement a client/server chat application using Java sockets and threads.arrow_forwardWrite a Java program to implement two threads such that one thread prints all the numbers that are divisible by 3 or 5 but not both from 1 to 100 and other thread prints all the numbers that are divisible by both 3 and 5 from 1 to 100. (use Thread class ). Note : Each thread has a delay of 200 millisecond after printing one numberarrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education