0% found this document useful (0 votes)
60 views

Debasish.11701128@lpu - In: Student Name: Debasish Dash Student ID: 11701128 Email Address: Github Link: Code

The document describes a code sample that uses semaphores to coordinate three threads printing numbers sequentially without race conditions. Thread 1 prints 10 to 20, Thread 2 prints 1 to 10, and Thread 3 prints 20 to 30. Semaphores s1 and s2 are initialized to 0. Thread 1 waits on s1, then signals s2. Thread 2 signals s1 without waiting. Thread 3 waits on s2. This ensures the threads print numbers in order without interference. The code is tested and produces consistent output each time with semaphores, but inconsistent output without semaphores due to race conditions.

Uploaded by

Adry
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views

Debasish.11701128@lpu - In: Student Name: Debasish Dash Student ID: 11701128 Email Address: Github Link: Code

The document describes a code sample that uses semaphores to coordinate three threads printing numbers sequentially without race conditions. Thread 1 prints 10 to 20, Thread 2 prints 1 to 10, and Thread 3 prints 20 to 30. Semaphores s1 and s2 are initialized to 0. Thread 1 waits on s1, then signals s2. Thread 2 signals s1 without waiting. Thread 3 waits on s2. This ensures the threads print numbers in order without interference. The code is tested and produces consistent output each time with semaphores, but inconsistent output without semaphores due to race conditions.

Uploaded by

Adry
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Student Name: Debasish Dash Student ID: 11701128

Email Address: [email protected]


Github Link: https://github.com/debasishdash01/os-assignment

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)

There are 3 threads each having a function to print 10 numbers.


Thread 1 prints 10 to 20.
Thread 2 prints 1 to 10.
Thread 3 prints 20 to 30.
Using semaphore we can avoid this race condition. A semaphore is an integer variable that
apart from initialization is only accessed through two standard atomic operations i.e. wait()
or P() and signal() or V().
Student Name: Debasish Dash Student ID: 11701128
Email Address: [email protected]
Github Link: https://github.com/debasishdash01/os-assignment

Wait() decreases the value by 1


Signal() Increases the value by 1

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);

For i<-10 to 20 //O(1)

Print I;

V(s2);}

Thread2(){

For i<-0 to 10 //O(1)

Print i

V(s1);}

Thread3(){

P(s2)

For i<-20 to 30 //O(1)

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

I am second thread I am first thread


1 I am second thread
2 I am third thread
. 10
. 20
9 23
I am first thread 24
10 5
11 1
.

. ./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

8.Github Link: https://github.com/debasishdash01/os-assignment


Minimum revisions have been made.

You might also like