Lab Session # 4 pthreads

#include <pthread.h> 

#include <stdio.h> 

void *hello(void *simpleInt) 

    int simple; 

    simple = (int)simpleInt; 

    printf("Hello From Thread ! I got fed with an integer whose value is %ld!\n", simple); 

            return NULL;

 

int main (int argc, char *argv[]) 

    pthread_t thread; 

    int rc; 

    int number = 3; 

            for (int i=0;i<3;i++)

            rc = pthread_create(&thread, NULL, hello, (void *)i); 

            pthread_exit(NULL); 

 

}

Exercise

 

 

Q1. Implement the code given above and show the output.

Q2. Modify the program so that we have only two threads created. The first should call print_hello1 and the second should call print_hello2. Both functions should have for loops for 100 times and should display line “Executing thread no. 1” or “Executing thread no. 2”.  Is the output interleaved or not. Why?

Q3. Use two threads to show two rotating bars at the two top corners of a screen. Basically each thread will be driving the “rotation” of the bar. (Hint: You can use “\” and “/” to achieve the effect but you will have to figure out how to achieve the effect of “rotation”.)

Q4. For the program developed in Q3, modify it so that on the pressing “1” the left bar starts rotating i.e. thread 1 starts executing and on pressing “2”, the left bar stops and the right bar starts executing. If “1” is pressed again then the left bar starts rotating again but the right bar stops rotating. (Hint: Think in terms of suspend and resume)

 

 

#include <pthread.h> 

#include <stdio.h> 

void *hello(void *simpleInt) 

    int simple; 

    simple = (int)simpleInt; 

    printf("Hello From Thread ! I got fed with an integer whose value is %ld!\n", simple); 

            return NULL;

 

int main (int argc, char *argv[]) 

    pthread_t thread; 

    int rc; 

    int number = 3; 

            for (int i=0;i<3;i++)

            rc = pthread_create(&thread, NULL, hello, (void *)i); 

            pthread_exit(NULL); 

 

}

Exercise

 

 

Q1. Implement the code given above and show the output.

Q2. Modify the program so that we have only two threads created. The first should call print_hello1 and the second should call print_hello2. Both functions should have for loops for 100 times and should display line “Executing thread no. 1” or “Executing thread no. 2”.  Is the output interleaved or not. Why?

Q3. Use two threads to show two rotating bars at the two top corners of a screen. Basically each thread will be driving the “rotation” of the bar. (Hint: You can use “\” and “/” to achieve the effect but you will have to figure out how to achieve the effect of “rotation”.)

Q4. For the program developed in Q3, modify it so that on the pressing “1” the left bar starts rotating i.e. thread 1 starts executing and on pressing “2”, the left bar stops and the right bar starts executing. If “1” is pressed again then the left bar starts rotating again but the right bar stops rotating. (Hint: Think in terms of suspend and resume)

 

 

 

Download Files