Basic Apis: Operating Systems Lab #5

Download as pdf or txt
Download as pdf or txt
You are on page 1of 1

Operating Systems

Lab #5

Objective of this lab is to practice POSIX thread API to gain better understanding of how threads are used and to learn some
problems that may occur due to inappropriate multi-threaded application design.

Basic APIs
int pthread_create(pthread_t *threadid, const pthread_attr_t *attr, void *(*start_routine)(void*), void *arg)
int pthread_join(pthread_t threadid, void **value_ptr)

How to compile
1. Include required header file
#Include<pthread.h>
2. Linking pthread library is also needed
gcc progname.c –o progname -lpthread

Task 1
1. Write a C program that creates a thread which prints something on screen 100,000 times. The value of delay between printing
and the data to be printed are provided as an argument to the thread function.
a. Execute the program several times and note down every time whether newly created thread has actually printed the
provided value 100,000 times or a different number of times.

2. Write a C program that creates a thread which prints something on screen 100,000 times. The value of delay between printing
and the data to be printed are provided as an argument to the thread function. The main program now waits for the created
thread.
a. Execute the program several times and note down every time whether newly created thread has actually printed the
provided value 100,000 times or a different number of times.

3. Write a C program that has a global integer variable initialized to 0. The program creates two threads – Thread A and B. Thread
A increments the global variable X number of times. Thread B decrements the same global variable X number of times.
These increment/decrements are done using simple iteration as given below:
for (i=0; i<X; i++)
{ X++ / X-- }
The value of X is provided to both threads as an argument. Main should not exit before both threads are completed.
a. Execute this program several times with different value of X ranging from 100,000 to highest possible value that you
can use. Note down the final value of global variable. Is it the same that you were expecting or different?

Submission & Evaluation


1. This is an individual assignment
2. All the working against every task should be shown to the instructor within lab.
3. Final code/document must also be uploaded on LMS.

You might also like