Final 8 - 1. Complete the procedure runCommand ( command,...

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 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[1], tempout); int ret = fork(); if(ret == 0){ char args[2]; args[0] = command; args[1] = NULL; execvp(args[0], args); perror("execvp"); return -1; } dup2(outputBuffer, tempout); dup2(tempin,0); dup2(tempout,1); waitpid(ret, NULL); return 1; } 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 mutex; sema_t emptySem; main() { // DO any initializations here pthread_mutex_init(&mutex, NULL); sema_init(&emptySem, 0, ...); } void insert( int val ) { List tmp = new List; pthread_mutex_lock(&mutex); tmp->val = val; tmp->next = head; head = tmp; pthread_mutex_unlock(&mutex); sema_post(&emptySem); } Struct List * removeFirst() { sema_wait(&emptySem); pthread_mutex_lock(&mutex); List tmp = head; head = tmp->next; pthread_mutex_unlock(&mutex); 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) { int slaveSocket = accept(masterSocket, &sockInfo, &alen); if(slaveSocket >= 0){ dispathHttp(slaveSocket); } } void forkServer( int masterSocket) { int slaveSocket = accept(masterSocket, &sockInfo, &alen); if(slaveSocket >= 0){ ret = fork(); if(ret == 0){ dispatchHttp(slaveSocket); exit(0); } close(slaveSocket); } } void poolOfThreads( int masterSocket) { int i = 0; for(i = 0; i < 4; i++){ pthread_create(&thread[i], NULL, loopthread, masterSocket); } loopthread(masterSocket); } void createThreadForEachRequest( int masterSocket ) { int slaveSocket = accept(masterSocket, &socketInfo, &alen); if(slaveSocket >= 0){ pthread_create(&thread, NULL, dispathHttp, slaveSocket);...
View Full Document

This note was uploaded on 01/30/2012 for the course CS 252 taught by Professor Gustavorodriguez during the Fall '11 term at Purdue University-West Lafayette.

Page1 / 16

Final 8 - 1. Complete the procedure runCommand ( command,...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online