Be careful with shared data structures connection

This preview shows page 5 - 8 out of 9 pages.

Be Careful with shared data structures Connection Queue Producer - main thread Consumers - connection handling threads
Image of page 5

Subscribe to view the full document.

Web Server Architecture #include … Queue connectionQ; main() { // Use signal handlers to shut down nicely SetUpSignalHandlers() SetUpSocket() for(j = 0; j < nthreads; j++) pthread_create(thread[j], HandleConn,args) pthread_create(logthread, LogWriter, args) for(;;) { sock = accept(…) connectionQ.enQ(sock) } } void *HandleConn(void *args) { while(we are still running) { // This call should block until I have a connection // You will need producer-consumer code sock = connectionQ.deQ(); recv(sock, message…) processURL() if (cgi) DoCGI() else { read file send(sock, response…) close(sock) } } }
Image of page 6
Shared Data Structures Queue of socket connections Strategy Write a template queue that inherits from the STL queue class Override the enqueue and dequeue methods sem_init(EmptySem, QSIZE); sem_init(PresentSem, 0); sem_init(mutex, 1); template <class Elem> void MyQueue<Elem>::enqueue(const Elem &Item) { sem_wait(EmptySem) sem_wait(mutex) super.push(Item) sem_post(mutex)
Image of page 7

Subscribe to view the full document.

Image of page 8
You've reached the end of this preview.
  • Winter '08
  • Clement,M
  • World Wide Web, Web server, Hypertext Transfer Protocol, Web Server Architecture

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern