Menu

Class 7: Homework


You must print this sheet out and write/type answers on it!

  1. From your reading in APUE 7.3:
    a) what is the minimum number of exit handlers guaranteed in ISO C to be supported?
    
    
          
    b) how would you find out how many are supported on our machines?
    
    
    
    
    
    
          
  2. Why won't the code given below compile:
    #include <stdio.h>
    #include <stdlib.h>
    
    void foo(int x) { fprintf(stderr,"(%i) bye.\n",x); }
    
    int main()
    {
      atexit(foo);
      sleep(2);
      return 0;
    }
          
    BTW: compiling it to see what message is produced is a good thing, but you're going to have to explain why there's a problem here, so simply regurgitating the error message is not acceptable.
    
    
    
    
    
    
    
    
          
  3. Compile and run each of these three programs (be patient when running them, they may take a minute to produce results):
    p0.cp1.cp2.c
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
      setvbuf(stdout,0,_IONBF,0);
      while(1)
      {
        int i;
        for(i = 0; i < 5; ++i)
        {
          sleep(1);
          fprintf(stdout,"%c",'X');
        }
        fprintf(stdout,"\n");    
      }
      return 0;
    }
    
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
      char B[12];
      setvbuf(stdout,B,_IOLBF,12);
      while(1)
      {
        int i;
        for(i = 0; i < 5; ++i)
        {
          sleep(1);
          fprintf(stdout,"%c",'X');
        }
        fprintf(stdout,"\n");    
       
      }
      return 0;
    }
    
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
      char B[64];
      setvbuf(stdout,B,_IOFBF,12);
      while(1)
      {
        int i;
        for(i = 0; i < 5; ++i)
        {
          sleep(1);
          fprintf(stdout,"%c",'X');
        }
        fprintf(stdout,"\n");    
       
      }
      return 0;
    }
    

    Explain explain what the output behvior is for each of the three programs, and explain thoroughly why they behave differently!