Debasish.11701128@lpu - In: Student Name: Debasish Dash Student ID: 11701128 Email Address: Github Link: Code
Debasish.11701128@lpu - In: Student Name: Debasish Dash Student ID: 11701128 Email Address: Github Link: Code
Code:
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<pthread.h>
#include<semaphore.h>
int i; //shared variable
sem_t s1,s2; //semaphore variables
void *Thread1(){
sem_wait(&s1);
printf("I am first thread\n");
for (int i=10;i<20;i++){
printf("%d\n",i); }
sem_post(&s2); }
void *Thread2(){
printf("I am second thread\n");
for (int i=0;i<10;i++){
printf("%d\n",i);}
sem_post(&s1);}
void *Thread3(){
sem_wait(&s2);
printf("I am third thread\n");
for(int i=20;i<31;i++){
printf("%d\n",i);}}
int main(){
sem_init(&s1,0,0);
sem_init(&s2,0,0);
//1st argument for the address of the semaphore variable
//2nd arg for the thread, non-zero value is used in case for process
//3rd arg for initializing the semaphore value
Student Name: Debasish Dash Student ID: 11701128
Email Address: [email protected]
Github Link: https://github.com/debasishdash01/os-assignment
pthread_t tid,tid2,tid3;
pthread_create(&tid,NULL,Thread1,(void *)&tid);
pthread_create(&tid2,NULL,Thread2,(void *)&tid2);
pthread_create(&tid3,NULL,Thread3,(void *)&tid3);
sem_destroy(&s1);
sem_destroy(&s2);
//for destroying the semaphores
pthread_exit(NULL);
return 0;}
1.Description:
Threads are lightweight processes which share same resource. As they work on the shared
resource, race condition may arise.
Basic standard code for a process:
P1
do{
Entry section
//critical section
Exit section
//remainder section
} while(T)
Wait() Signal()
Wait(S){ Signal(S){
While(S<=0); //This is the trap S=S+1;
S=S-1; }
}
Initial value of semaphores s1 and s2 are 0. In thread1 we have P (s1). So thread 1 can’t go
into Critical Section until s1 value changes to 1. Now thread2 has no P ().So it can directly go
to Critical Section. Now it signals s1 i.e. V (s1) and makes the value of s1 to 1. Thread3 has
P(s2) and s2 value is initially zero, it can’t go to C.S until s2 value is changed to 1. Now
thread1 can complete its execution because s1 value is 1. After completing it changes the
value of s2 to 1 by signalling i.e. V (s2). Now Thread3 can go into critical section and
complete its execution. Thus the goal is achieved.
2.Algorithm:
Set s1,s2->0
Thread1(){
P(s1);
Print I;
V(s2);}
Thread2(){
Print i
V(s1);}
Thread3(){
P(s2)
Print I;}
Student Name: Debasish Dash Student ID: 11701128
Email Address: [email protected]
Github Link: https://github.com/debasishdash01/os-assignment
T.C: Ω(1) is the best time complexity since we know that loop will run for a constant amount
of time
However Ö(∞) since there are no bounded wait condition applied. A process can undergo
into Critical Section any number of times denying the other processes.
S.C: 4 bytes are required for integer i.
3.Description(purpose of use):
Without using semaphores the threads undergo into race condition because it is accessing a
shared variable i. Due to context switch we are unable to print the 1 to 30 sequentially. When
we use semaphore we are able to print the number sequentially.
5.Code Snippet:
No extra code was required because linux has semaphore library. We used sem_wait() and
sem_post() from the library.
6.Boundary conditions:
Removing the semaphores produces different result, each time we run the code. But by using
semaphore we are able achieve the goal of printing the required result, even if we run it
multiple time we get the same output.
But there is no bounded wait condition. The program may execute indefinitely till semaphore
value changes to 1.
Student Name: Debasish Dash Student ID: 11701128
Email Address: [email protected]
Github Link: https://github.com/debasishdash01/os-assignment
7.Test Cases:
gcc code.c –lpthread gcc without_sem.c -lpthread
./a.out ./a.out
. ./a.out
19 I am second thread
I am third thread 1
20 2
21 I am third thread
. 10
. 20
30 23
It produces same result each time. It produces different result each time.
Student Name: Debasish Dash Student ID: 11701128
Email Address: [email protected]
Github Link: https://github.com/debasishdash01/os-assignment