CS252 Final 3

CS252 Final 3 - CS252 Final Review Please aswer this final...

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

View Full Document Right Arrow Icon
CS252 Final Review Please aswer 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 tmpin=dup(0); int tmpout=dup(1); int fdin; int fdout; int ret; int i =0; int c = 0; int fdpipe[2]; pipe(fdpipe); fdout=fdpipe[0]; //fdpipe[1] will be the output from the child process dup2(fdout,1); close(fdout); ret=fork(); if(ret==0) { //execute the command execlp(command, command, NULL); return -1; } dup2(tmpout,1); close(tmpout); fdin=fdpipe[1]; dup2(fdin,0); close(fdin); //read from stdin (pipe) and write contents to outputBuffer while(i<maxBufferSize && (c=getchar()) != EOF) { outputBuffer[i] = c; i++; } dup2(tmpin,0);
Background image of page 1

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

View Full DocumentRight Arrow Icon
close(tmpin); waitpid(ret, NULL, 0); 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 int MaxSize; int size; mutex_t _mutex; sema_t _emptySem; sema_t _fullSem; main() { size=0;
Background image of page 2
MaxSize = 20; pthread_mutex_init(&_mutex, NULL, NULL); sema_init(&_emptySem, 0, USYNC_THREAD, NULL); sema_init(&_fullSem, MaxSize, USYNC_THREAD, NULL); } void insert( int val ) { sema_wait(&_fullSem); mutex_lock(_mutex); List tmp = new List; tmp->val = val; tmp->next = head; head = tmp; mutex_unlock(_mutex); sema_post(_emptySem); } Struct List * removeFirst() { sema_wait(&_emptySem); mutex_lock(_mutex); List tmp = head; head = tmp->next; mutex_unlock(_mutex); sema_post(_fullSem); 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) {
Background image of page 3

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

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

Page1 / 16

CS252 Final 3 - CS252 Final Review Please aswer this final...

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

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