CS252 Final 5 - 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

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: 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. #include <stdio.h> #include <stdlib.h> #include <unistd.h> // slide 250-251 // executes a command in a different process int runCommand( char * command, char * outputBuffer, int maxBufferSize) { // save stdin/stdout int tempin = dup(0); int tempout = dup(1); // Hint: Use a pipe to communicate the parent and the child process running runCommand(). // create pipe int fdpipe[2]; pipe(fdpipe); // redirect stdout for "command" dup2(fdpipe[0],1); close(fdpipe[0]); // fork for "command" int ret = fork(); if (ret<0) return-1; if (ret == 0){ // close file descriptors ass soon as they are not needed close(fdpipe[1]); // command is the name of the program with no arguments. See how main uses runCommand(). char * args[2]; args[0] = command; args[1]=0; execvp(args[0],args); // error in execvp perror( "execvp" ); _exit(1); } unsigned char newChar; // Have the parent read from the pipe and write into the outputBuffer. int n,usedBufferLength=0; // doesn't work, don't use this... while ( usedBufferLength < maxBufferSize && ( n = read( fdpipe[0], &newChar, sizeof (newChar) ) ) > 0 ) { // stores its output in outputBuffer outputBuffer[ usedBufferLength ] = newChar; usedBufferLength++; } // add null character at the end of the string outputBuffer[ usedBufferLength ] = 0; // restore stdin/stdout dup2(tempin,0); dup2(tempout,1); // parent waits for "command" process waitpid(ret,0,0); // runCommand will return 0 on success or -1 otherwise. 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. // slide 338-339 does this in c++, we'll do it in c #include struct List { int val; List * next; }; struct List * head = NULL ; // More variables pthread_mutext_t _mutex; sema_t _emptySem; int main() { // do any initializations here head = NULL ; pthread_mutex_init( &_mutex, NULL ); // initialize mutex sema_init( &_emptySem, 0, USYNC_THREAD,...
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 / 28

CS252 Final 5 - 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