Midterm Exam

CS354 Fall 2000

Name:



 

1. Answer True/False (T/F) (1 point each)

  F     The threads in a process share the same stack.
_F_ ELF is an executable format used on Windows.
_F___ truss is a UNIX command to show the number of users in the system.
_T__ The text section of a program is memory mapped readable and executable.
_F___ System calls and interrupts run in user mode.
_F___ There may be three running process in a dual processor computer.
_T___ Most of the CPU time is spent in user mode.
_T___ Shared libraries run in user mode.
_T__ A small time quantum will cause a program to take more time.
_F___ The arguments of a system call are checked in user space.
_F___In preemptive scheduling a process takes higher priority until completion.
_F___ A program that runs with preemptive scheduling runs faster than one in non-preemptive scheduling.
_T___ Most of the processes' CPU bursts are completed before the time quantum expires.
_T/F___ Programs that run with FCFS have a higher response time than SJF.
_F___ The file descriptors of a process are closed when the process calls fork().
_T___ The write() function is a system call but printf() is not.
_F___ A pipe that is used to communicate a parent and a child process could be created by the child.
_T___ Threads in the same process share the same memory.
_T___ A section of code that is guarded by mutex_lock()/mutex_unlock() can be executed by only one thread at a time.
_F___The process table contains a set of registers and program counters for each user and kernel level thread in a process.
 

2. (4 pts.) What is a real-time system, a hard real-time system, and a soft real-time system?

Real time system - time constraints.
Hard-real-time sys. - a process has to fullfil the time contraints.
Soft-real-time sys. - higher priority processes run until completion.

3. (4 pts.) Why system calls use software interrupts and not normal function calls.

System calls need to run in kernel mode.
 

4. (4 pts.) What are the steps for servicing an interrupt?
-save registers.
-jump to interrupt vector routine.
-process interrupt.
-restore registers.
-return from interrupt.

5. (4 pts.) In which state processes are most of the time and in what kind of programs this is not true?

-waiting state.
-batch processes
or compilers, ray tracing, numerical programs etc.

6. (4 pts.) What are the advantages and disadvantages of using threads vs. using processes?
Threads
        -context switch faster
        - shared memory.
Processes
        -no need of synchronization.
        - more reliable.

7. (4 pts.) What factors have to be considered when choosing the length of a quantum time?

Response time.
Context switch overhead.
Average burst time.

8. (8 pts.) Assume that a computer executes the following jobs:
 

Process Time needed for completion (ms)
P1 50
P2 5
P3 25

Compute the average waiting time if the following scheduling algorithms are used:

FCFS:

Avg wait time = (0+50+55)/3 = 105/3
SJF:
        Avg wait time = (0+5+30)/3 = 35/3

Round Robin Preemptive scheduling (Quantum=10ms):

        Avg wait time = (0+10+15)/3 = 25/3
 
 
9. (6 pts.) Mark the "C" expressions that are quivalent to a[i]. The type of a[i] is  <type of a[0]>
Expression Equivalent to a[i]. Yes or Not
a + i   NO.
a + i * sizeof( <type of a[0]>)  NO.
&a + i  NO
*(a + i)  YES
*(a[0] + i)  NO
*(&a[0] + i)  YES
*(&a[i])  YES
&a[i]  NO
*(<type of a[0]> *) ((char *) &a[0] + i )  NO
*(<type of a[0]> *) ((char *)&a[0] + i * sizeof(a[0]))  YES

 
 
10. (6 pts.) The following class implements a multi-threaded stack. The push() and pop() calls can be called simultaneously by different threads. Insert the necessary code and member variables so multiple threads can run the stack operations simultaneously without corrupting the stack.

class Stack {

int stackPointer; 
int stack[ MaxStack ]; 

mutex_t      mutex;

public:

void 
Stack::push( int a ) 

mutex_lock(&mutex);

    stack[ stackPointer ] = a; 
    stackPointer++; 

mutex_unlock(&mutex);

int 
Stack::pop() 

mutex_lock(&mutex);

    stackPointer--; 
    int tmp = stack[ stackPointer ]; 

mutex_unlock(&mutex);

    return tmp; 
}

Stack::Stack() {
stackpointer = 0;
mutex_init(&mutex);
}
 

};

 


 
 
11. (13 pts.) Complete the procedure redirectStdout( fileName ) to redirect the standard out to file fileName. The procedure will return -1 if any error occurrs or 0 if it succeeds. Look at main() to see how redirectStdout( ) is used. Also write the procedure restoreOutput() that will restore the output to the way it was before calling redirectStdout. Notice that the procedure is called redirectStdout and not redirectStdin.
int tmpfd;

int redirectStdout( char * fileName )
{
tmpfd = dup(1);

int fd = open(filename, O_RDWR | O_CREAT, 0x660);
assert(fd>0);
dup2(fd,1);

}

int restoreStdout() {

dup2(tmpfd,1);

}

main()
{
    if ( redirectStdout( "myfile" ) < 0 ) {
        perror( "redirectStdout" );
        exit( -1 );
    }

    printf( "This Hello world will be printed to myfile\n" );

    restoreStdout();

    printf( "This Hello World will be printed to the screen\n");

}
 


 
 
12. (13 pts.) 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.
int
runCommand( char * command, char * outputBuffer, int maxBufferSize)
{
    int fdpipe[2];
    char * args[2];
 
    pipe(fdpipe);
    int pid = fork();
    assert(pid>=0);
    if (pid ==0)
    {
        dup2(fdpipe[1], 1);
        args[0] = command;
        args[1] = NULL;
        close(fdpipe[0]);
        close(fdpipe[1]);
        execv(args[0], args);
        exit(-1);
    }
    else
    {
         int i =0;
         close(fdpipe[1]);
         while((read(fdpipe[0], outputBuffer[i++], 1) > 0) && (i<(maxBufferSize-1)));
         outputBuffer[i] = 0; 
         close(fdpipe[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 );
}

 


 
 
13. (10 pts.) Write the code for the function strcat().
char * strcat( char * dest, char * src )
{

    while(*dst)
         dest++;

    while(*src)
    {
        *dest = *src;
        dest++;
        src++;
    }
    *dest = 0;
}