{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Final 7 - Matthew Sutherlin CS252 Final Review Please...

Info icon This preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
Matthew Sutherlin CS252 Final Review Please answer this final review and return it during the final exam. If you write down the solution in HTML and turn it in before 1:00pm the day before the final exam, you will get up to 4 extra points in the final exam. The submitted solutions will be posted. 1. Complete the procedure runCommand ( command, outputBuffer, bufferSize) that executes a command in a different process and stores its output in outputBuffer. command is the name of the program with no arguments. See how main uses runCommand (). runCommand will return 0 on success or -1 otherwise. Hint: Use a pipe to communicate the parent and the child process running runCommand(). Have the parent read from the pipe and write into the outputBuffer. int runCommand( char * command, char * outputBuffer, int maxBufferSize) { int tempin = dup(0); int tempout = dup(1); int fdpipe[2]; pipe(fdpipe); dup2(fdpipe[0], 1); close(fdpipe[0]); int ret = fork(); if (ret == 0) { close(fdpipe[1]); char* args[2]; args[0]; args[1]; execvp(args[0], args); perror(“execvp”); _exit(1); } while (fgets(outputBuffer, sizeof(outputBuffer), fdpipe[0]) {}; dup2(tempin, 0); dup2(tempout, 1);
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
waitpid(ret, NULL); return 0; } int main() { // The output of "ls" will be stored in buffer char buffer[ 1024 ]; if ( runCommand( "ls", buffer, 1024 ) < 0 ) { perror("runCommand" ); exit( -1 ); } printf( "ls: %s\n", buffer ); exit( 0 ); } 2. Add the necessary code to the insert() and removeFirst() functions to make them synchronized. removeFirst() will have to wait if the list is empty. insert() will have to wait if there are already 20 elements in the list. Use semaphores . Add also the variables you need. struct List { int val; int next; }; struct List * head = NULL; // More variables pthread_mutex_t lock; sema_t sem; main() { // DO any initializations here pthread_mutex_init(&lock, NULL); sema_init(sema_t* sem, USYNC_THREAD, NULL); } void insert( int val ) { pthread_mutex_lock(&lock); List tmp = new List; tmp->val = val; tmp->next = head;
Image of page 2
head = tmp; pthread_mutex_unlock(&lock); sema_post(&sem); } Struct List * removeFirst() { sema_wait(&sem); pthread_mutex_init(&lock, NULL); List tmp = head; head = tmp->next; pthread_mutex_unlock(&lock); return tmp; } 3. From lab3, assuming you have a procedure void dispatchHTTP( int slaveSocket) that processes the request and closes slaveSocket, write the loop server code for a) iterative server, b) concurrent server using fork, c) concurrent server creating a thread after each request, and d) pool of threads, in the procedures indicated. Each procedure receives as argument the master socket already initialized and ready to be used inside accept. void iterativeServer( int masterSocket) { // Accept incoming connections struct sockaddr_in clientIPAddress; int alen = sizeof( clientIPAddress ); int slaveSocket; while (1) { int slaveSocket = accept(masterSocket, &sockInfo, &alen); if (slaveSocket >= 0) { dispatchHTTP(slaveSocket); } } } void forkServer( int masterSocket) { // Accept incoming connections struct sockaddr_in clientIPAddress; int alen = sizeof( clientIPAddress ); int slaveSocket;
Image of page 3

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
while (1) { int slaveSocket = accept(masterSocket, &sockInfo, &alen); if (slaveSocket >= 0) { int ret = fork(); ` if (ret == 0) { dispatchHTTP(slaveSocket); exit(0); } close(slaveSocket); } } } void poolOfThreads( int masterSocket) { for (int i=0; i<4; i++) {
Image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

{[ 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