Week 10 Tutorial Questions

Objectives

  1. How is the assignment going?

    Does anyone have hints or advice for other students?

    Has anyone discovered interesting cases that have to be handled?

  2. Write a C program, now.c, which prints the following information:

    1. The current date.
    2. The current time.
    3. The current user.
    4. The current hostname.
    5. The current working directory.
    dcc now.c -o now
    ./now
    29-02-2022
    03:59:60
    cs1521
    zappa.orchestra.cse.unsw.EDU.AU
    /home/cs1521/lab08
    
  3. What is the difference between concurrency and parallelism?

  4. How can we make our programs concurrent in C?

  5. Write a C program that creates a thread that infinitely prints some message provided by main (eg. "Hello\n"), while the main (default) thread infinitely prints a different message (eg. "there!\n").

  6. The following C program attempts to say hello from another thread:

    #include <stdio.h>
    #include <pthread.h>
    
    void *thread_run(void *data) {
        printf("Hello from thread!\n");
    
        return NULL;
    }
    
    int main(void) {
        pthread_t thread;
        pthread_create(
            &thread,    // the pthread_t handle that will represent this thread
            NULL,       // thread-attributes -- we usually just leave this NULL
            thread_run, // the function that the thread should start executing
            NULL        // data we want to pass to the thread -- this will be
                        // given in the `void *data` argument above
        );
    
        return 0;
    }
    

    However, when running this program after compiling with clang, the thread doesn't say hello.

    clang -pthread program.c -o program
    ./program
    ./program
    ./program
    

    Why does our program exhibit such behaviour?

    How can we fix it?

  7. Concurrency can allow our programs to perform certain actions simultaneously that were previously tricky for us to do as COMP1521 students.

    For example, with our current C knowledge, we cannot execute any code while waiting for input (with, for example, scanf, fgets, etc.).

    Write a C program that creates a thread which infinitely prints the message "feed me input!\n" once per second (sleep(3)), while the main (default) thread continuously reads in lines of input, and prints those lines back out to stdout with the prefix: "you entered: ".

  8. The following C program attempts to increment a global variable in two different threads, 5000 times each.

    #include <stdio.h>
    #include <pthread.h>
    
    int global_total = 0;
    
    void *add_5000_to_counter(void *data) {
        for (int i = 0; i < 5000; i++) {
            // sleep for 1 nanosecond
            nanosleep (&(struct timespec){.tv_nsec = 1}, NULL);
            
            // increment the global total by 1
            global_total++;
        }
    
        return NULL;
    }
    
    int main(void) {
        pthread_t thread1;
        pthread_create(&thread1, NULL, add_5000_to_counter, NULL);
    
        pthread_t thread2;
        pthread_create(&thread2, NULL, add_5000_to_counter, NULL);
    
        pthread_join(thread1, NULL);
        pthread_join(thread2, NULL);
    
        // if program works correctly, should print 10000
        printf("Final total: %d\n", global_total);
    }
    

    Since the global starts at 0, one may reasonably assume the value would total to 10000.

    However, when running this program, it often gives differing values each individual execution:

    dcc -pthread program.c -o program
    ./program
    Final total: 9930
    ./program
    Final total: 9983
    ./program
    Final total: 9994
    ./program
    Final total: 9970
    ./program
    Final total: 10000
    ./program
    Final total: 9996
    ./program
    Final total: 9964
    ./program
    Final total: 9999
    

    Why does our program exhibit such behaviour?

  9. How can we use "mutual exclusion" to fix the previous program?

  10. How can we use atomic types to fix the previous program?

Revision questions

The following questions are primarily intended for revision, either this week or later in session.
Your tutor may still choose to cover some of these questions, time permitting.