next up previous contents
Next: Assignment II; due to Up: OPERATING SYSTEMS LABORATORY V Previous: OPERATING SYSTEMS LABORATORY V   Contents

Examples&Exercises:

  1. execl family of functions; execl execlp execle exect execv execvp all performs a similar function by starting another program. This new program overlays the existing program, so you can never return to the to original code unless the call to execl fails. http://siber.cankaya.edu.tr/OperatingSystems/cfiles/code21.c code21.c and http://siber.cankaya.edu.tr/OperatingSystems/cfiles/code22.c code22.c
    #include <unistd.h>
    #include <sys/types.h>
    #include <sys/wait.h>
    main()
    {
      int pid;
      if ((pid = fork()) == 0) {
      execl("/bin/ls", "ls", "/", 0);
      }
      else {
        wait(&pid);
      printf("Exec finished\n");
      }
    }
    






    #include <unistd.h>
    #include <stdio.h>
    int main()
    {
      printf("Running ps with execlp\n");
      execlp("ps", "ps", "-ax", 0);
      printf("Done.\n");
      exit(0);
    }
    
  2. Creating a (child) thread; pthread_t, pthread_create. http://siber.cankaya.edu.tr/OperatingSystems/cfiles/code23.c code23.c
  3. Creating a child thread (with error checking) and joining (existing a thread); pthread_join. http://siber.cankaya.edu.tr/OperatingSystems/cfiles/code24.c code24.c
  4. Competing; - http://siber.cankaya.edu.tr/OperatingSystems/cfiles/code25.c code25.c
  5. The parent and one child; same operations with different data. http://siber.cankaya.edu.tr/OperatingSystems/cfiles/code26.c code26.c
  6. The parent and four childrens; same operations with same data. http://siber.cankaya.edu.tr/OperatingSystems/cfiles/code27.c code27.c
  7. Complete the following program (....... are for the missing parts) that creates two threads and each print different characters in given amount.
    #include <pthread.h>
    #include <stdio.h>
    /* Parameters to print_function.  */
    struct char_print_parms
    {
      /* The character to print.  */
      char character;
      /* The number of times to print it.  */
      int count;
    };
    /* Prints a number of characters to stderr, as given by PARAMETERS,
       which is a pointer to a struct char_print_parms.  */
    void* char_print (void* parameters)
    {
      /* Cast the cookie pointer to the right type.  */
      struct char_print_parms* p = (struct char_print_parms*) parameters;
      int i;
      for (i = 0; i < p->count; ++i)
        fputc (p->character, stderr);
      return NULL;
    }
    int main ()
    {
      pthread_t thread1_id;
      .......
      struct char_print_parms thread1_args;
      .......
      /* Create a new thread to print 30000 x's.  */
      thread1_args.character = 'x';
      thread1_args.count = 30000;
      .......
      /* Create a new thread to print 20000 o's.  */
      thread2_args.character = 'o';
      .......
      .......
      /* Make sure the first thread has finished.  */
      pthread_join (thread1_id, NULL);
      /* Make sure the second thread has finished.  */
      .......
      /* Now we can safely return.  */
      return 0;
    }
    


next up previous contents
Next: Assignment II; due to Up: OPERATING SYSTEMS LABORATORY V Previous: OPERATING SYSTEMS LABORATORY V   Contents
Cem Ozdogan 2009-05-11