final 9

final 9 - CS252 Final Review 1. Complete the procedure...

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

View Full Document Right Arrow Icon
CS252 Final Review 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 tmpout = dup(1); int fdpipe[2]; pipe(fdpipe); dup2(fdpipe[0],1); close(fdpipe[0]); int ret = fork(); if(ret == 0) { char* args[2]; args[0]=command; args[1]=NULL; execvp(args[0],args); perror("execvp"); _exit(1); } int c=read(fdpipe[1],outputBuffer,maxBufferSize); close(fdpipe[1]); dup2(tmpout,1); close(tmpout); if(c<0) { return -1; } else { return 0; } } int main() { // The output of "ls" will be stored in buffer char buffer[ 1024 ];
Background image of page 1

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

View Full DocumentRight Arrow Icon
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; int MAXSIZE = 20; mutex_t mutex; sema_t emptysem; sema_t fullsem; main() { 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;
Background image of page 2
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; return tmp; mutex_unlock(&mutex); sema_post(&fullsem); } 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) { while(1) { // Accept incoming connections struct sockaddr_in clientIPAddress; int alen = sizeof( clientIPAddress ); pthread_mutex_lock(&accept_mutex); int slaveSocket = accept( masterSocket, (socklen_t*)&alen); pthread_mutex_unlock(&accept_mutex); if ( slaveSocket < 0 ) { perror( "accept" ); exit( -1 ); } // Process request.
Background image of page 3

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

View Full DocumentRight Arrow Icon
dispatchHTTP( slaveSocket ); } } void forkServer( int masterSocket) { // catch SIGCHLD signals struct sigaction signalAction; signalAction.sa_handler = 0;
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 17

final 9 - CS252 Final Review 1. Complete the procedure...

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