SlideShare a Scribd company logo
1 of 5
Download to read offline
Course Code: CSE2005 Course Name: Operating System Lab
Faculty: Dr. Bhanu Chander Balusa Slot: L21+L22
Program-1:
Aim:
Consider that two threads of a process should access the critical section. Assume that the
critical section has a count variable with the initial value of 0. First thread increments the count
variable by one for five times and second thread decrements the count variable by one for five
times. Implement this job in C programming using Peterson’s solution of process
synchronization.
Background:
NAME: Mridul Jadon
REGNO: 20BCE1734
CODE:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#define TRUE 1
#define FALSE 0
// Let us create a global variable count to change it in threads
int count = 0;
int favoured;
int t1_wants_to_enter =FALSE;
int t2_wants_to_enter=FALSE;
void *increment(void *vargp)
{
// Store the value argument passed to this thread
int *myid = (int *)vargp;
t1_wants_to_enter=TRUE;
favoured=2;
while(t2_wants_to_enter==TRUE && favoured==2);
// Change global variable
for(int i=0;i<5;i++){
count++;
printf("Thread ID: %d, Count: %dn", *myid, count);
}
t1_wants_to_enter=FALSE;
// Print the argument and global variables
}
void *decrement(void *vargp)
{
// Store the value argument passed to this thread
int *myid = (int *)vargp;
t2_wants_to_enter=TRUE;
favoured=1;
while(t1_wants_to_enter==TRUE && favoured==1);
// Change global variable
for(int i=0;i<5;i++){
count--;
printf("Thread ID: %d, Count: %dn", *myid, count);
}
t2_wants_to_enter=FALSE;
// Print the argument and global variables
printf("Thread ID: %d, Count: %dn", *myid, count);
}
int main()
{
int i;
pthread_t tid1;
pthread_t tid2;
// Let us create three threads
pthread_create(&tid1, NULL, increment, (void *)&tid1);
pthread_create(&tid2, NULL, decrement, (void *)&tid2);
pthread_exit(NULL);
return 0;
}
OUTPUT:
Program-2:
Aim:
Consider that two threads of a process should access the critical section. Assume that the
critical section has a count variable with the initial value of 0. First thread increments the count
variable by one for five times and second thread decrements the count variable by one
for five times. Implement this job in C programming using Semaphore solution of process
synchronization.
Background:
CODE:
#include<pthread.h>
#include<stdio.h>
#include<semaphore.h>
#include<unistd.h>
void *fun1();
void *fun2();
int shared=1;
sem_t s;
int main()
{
sem_init(&s,0,1);
pthread_t thread1, thread2;
pthread_create(&thread1, NULL, fun1, NULL);
pthread_create(&thread2, NULL, fun2, NULL);
pthread_join(thread1, NULL);
pthread_join(thread2,NULL);
printf("Final value of shared is %dn",shared);
}
void *fun1()
{
int x;
sem_wait(&s);
x=shared;
printf("Thread1 reads the value as %dn",x);
for(int i=0;i<5;i++){
x++;
}
printf("Local updation by Thread1: %dn",x);
sleep(1);
shared=x;
printf("Value of shared variable updated by Thread1 is: %dn",shared);
sem_post(&s);
}
void *fun2()
{
int y;
sem_wait(&s);
y=shared;
printf("Thread2 reads the value as %dn",y);
for(int i=0;i<5;i++){
y--;
}
printf("Local updation by Thread2: %dn",y);
sleep(1);
shared=y;
printf("Value of shared variable updated by Thread2 is: %dn",shared);
sem_post(&s);
}
OUTPUT:

More Related Content

What's hot

Maharishi University of Management (MSc Computer Science test questions)
Maharishi University of Management (MSc Computer Science test questions)Maharishi University of Management (MSc Computer Science test questions)
Maharishi University of Management (MSc Computer Science test questions)Dharma Kshetri
 
Lecture#7 Call by value and reference in c++
Lecture#7 Call by value and reference in c++Lecture#7 Call by value and reference in c++
Lecture#7 Call by value and reference in c++NUST Stuff
 
Presentation on C language By Kirtika thakur
Presentation on C language By Kirtika thakurPresentation on C language By Kirtika thakur
Presentation on C language By Kirtika thakurThakurkirtika
 
Lecture#6 functions in c++
Lecture#6 functions in c++Lecture#6 functions in c++
Lecture#6 functions in c++NUST Stuff
 
Call by value or call by reference in C++
Call by value or call by reference in C++Call by value or call by reference in C++
Call by value or call by reference in C++Sachin Yadav
 
C standard library functions
C standard library functionsC standard library functions
C standard library functionsVaishnavee Sharma
 
Tail Recursion in data structure
Tail Recursion in data structureTail Recursion in data structure
Tail Recursion in data structureRumman Ansari
 
Call by value
Call by valueCall by value
Call by valueDharani G
 
Functions (Computer programming and utilization)
Functions (Computer programming and utilization)Functions (Computer programming and utilization)
Functions (Computer programming and utilization)Digvijaysinh Gohil
 
C++ Function
C++ FunctionC++ Function
C++ FunctionHajar
 
Functions in c
Functions in cFunctions in c
Functions in cInnovative
 

What's hot (20)

Maharishi University of Management (MSc Computer Science test questions)
Maharishi University of Management (MSc Computer Science test questions)Maharishi University of Management (MSc Computer Science test questions)
Maharishi University of Management (MSc Computer Science test questions)
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
Functionincprogram
FunctionincprogramFunctionincprogram
Functionincprogram
 
Lecture#7 Call by value and reference in c++
Lecture#7 Call by value and reference in c++Lecture#7 Call by value and reference in c++
Lecture#7 Call by value and reference in c++
 
C function
C functionC function
C function
 
Function & Recursion in C
Function & Recursion in CFunction & Recursion in C
Function & Recursion in C
 
Function in c program
Function in c programFunction in c program
Function in c program
 
Presentation on C language By Kirtika thakur
Presentation on C language By Kirtika thakurPresentation on C language By Kirtika thakur
Presentation on C language By Kirtika thakur
 
lets play with "c"..!!! :):)
lets play with "c"..!!! :):)lets play with "c"..!!! :):)
lets play with "c"..!!! :):)
 
Lecture#6 functions in c++
Lecture#6 functions in c++Lecture#6 functions in c++
Lecture#6 functions in c++
 
Call by value or call by reference in C++
Call by value or call by reference in C++Call by value or call by reference in C++
Call by value or call by reference in C++
 
C standard library functions
C standard library functionsC standard library functions
C standard library functions
 
Functions in c++,
Functions in c++,Functions in c++,
Functions in c++,
 
Tail Recursion in data structure
Tail Recursion in data structureTail Recursion in data structure
Tail Recursion in data structure
 
Call by value
Call by valueCall by value
Call by value
 
Functions (Computer programming and utilization)
Functions (Computer programming and utilization)Functions (Computer programming and utilization)
Functions (Computer programming and utilization)
 
C++ Function
C++ FunctionC++ Function
C++ Function
 
Functions in c
Functions in cFunctions in c
Functions in c
 
Pointer to function 2
Pointer to function 2Pointer to function 2
Pointer to function 2
 
Functions in C
Functions in CFunctions in C
Functions in C
 

Similar to 20BCE1734.pdf

Modify this code to use multiple threads with the same data1.Modif.pdf
Modify this code to use multiple threads with the same data1.Modif.pdfModify this code to use multiple threads with the same data1.Modif.pdf
Modify this code to use multiple threads with the same data1.Modif.pdfmallik3000
 
Object Oriented Technologies
Object Oriented TechnologiesObject Oriented Technologies
Object Oriented TechnologiesUmesh Nikam
 
Preprocessor directives
Preprocessor directivesPreprocessor directives
Preprocessor directivesVikash Dhal
 
Programming Fundamentals Functions in C and types
Programming Fundamentals  Functions in C  and typesProgramming Fundamentals  Functions in C  and types
Programming Fundamentals Functions in C and typesimtiazalijoono
 
friends functionToshu
friends functionToshufriends functionToshu
friends functionToshuSidd Singh
 
Debugging and Profiling C++ Template Metaprograms
Debugging and Profiling C++ Template MetaprogramsDebugging and Profiling C++ Template Metaprograms
Debugging and Profiling C++ Template MetaprogramsPlatonov Sergey
 
20145-5SumII_CSC407_assign1.htmlCSC 407 Computer Systems II.docx
20145-5SumII_CSC407_assign1.htmlCSC 407 Computer Systems II.docx20145-5SumII_CSC407_assign1.htmlCSC 407 Computer Systems II.docx
20145-5SumII_CSC407_assign1.htmlCSC 407 Computer Systems II.docxeugeniadean34240
 
C programming Lab 1
C programming Lab 1C programming Lab 1
C programming Lab 1Zaibi Gondal
 
python lab programs.pdf
python lab programs.pdfpython lab programs.pdf
python lab programs.pdfCBJWorld
 
JLK Chapter 5 – Methods and ModularityDRAFT January 2015 Edition.docx
JLK Chapter 5 – Methods and ModularityDRAFT January 2015 Edition.docxJLK Chapter 5 – Methods and ModularityDRAFT January 2015 Edition.docx
JLK Chapter 5 – Methods and ModularityDRAFT January 2015 Edition.docxvrickens
 
Parallel program design
Parallel program designParallel program design
Parallel program designZongYing Lyu
 
Advanced Web Technology ass.pdf
Advanced Web Technology ass.pdfAdvanced Web Technology ass.pdf
Advanced Web Technology ass.pdfsimenehanmut
 
Tdd with python unittest for embedded c
Tdd with python unittest for embedded cTdd with python unittest for embedded c
Tdd with python unittest for embedded cBenux Wei
 

Similar to 20BCE1734.pdf (20)

Modify this code to use multiple threads with the same data1.Modif.pdf
Modify this code to use multiple threads with the same data1.Modif.pdfModify this code to use multiple threads with the same data1.Modif.pdf
Modify this code to use multiple threads with the same data1.Modif.pdf
 
Object Oriented Technologies
Object Oriented TechnologiesObject Oriented Technologies
Object Oriented Technologies
 
Linux_C_LabBasics.ppt
Linux_C_LabBasics.pptLinux_C_LabBasics.ppt
Linux_C_LabBasics.ppt
 
Apclass (2)
Apclass (2)Apclass (2)
Apclass (2)
 
Apclass (2)
Apclass (2)Apclass (2)
Apclass (2)
 
Preprocessor directives
Preprocessor directivesPreprocessor directives
Preprocessor directives
 
Programming Fundamentals Functions in C and types
Programming Fundamentals  Functions in C  and typesProgramming Fundamentals  Functions in C  and types
Programming Fundamentals Functions in C and types
 
friends functionToshu
friends functionToshufriends functionToshu
friends functionToshu
 
CPP Homework Help
CPP Homework HelpCPP Homework Help
CPP Homework Help
 
Debugging and Profiling C++ Template Metaprograms
Debugging and Profiling C++ Template MetaprogramsDebugging and Profiling C++ Template Metaprograms
Debugging and Profiling C++ Template Metaprograms
 
Apclass
ApclassApclass
Apclass
 
20145-5SumII_CSC407_assign1.htmlCSC 407 Computer Systems II.docx
20145-5SumII_CSC407_assign1.htmlCSC 407 Computer Systems II.docx20145-5SumII_CSC407_assign1.htmlCSC 407 Computer Systems II.docx
20145-5SumII_CSC407_assign1.htmlCSC 407 Computer Systems II.docx
 
C programming Lab 1
C programming Lab 1C programming Lab 1
C programming Lab 1
 
python lab programs.pdf
python lab programs.pdfpython lab programs.pdf
python lab programs.pdf
 
JLK Chapter 5 – Methods and ModularityDRAFT January 2015 Edition.docx
JLK Chapter 5 – Methods and ModularityDRAFT January 2015 Edition.docxJLK Chapter 5 – Methods and ModularityDRAFT January 2015 Edition.docx
JLK Chapter 5 – Methods and ModularityDRAFT January 2015 Edition.docx
 
Parallel program design
Parallel program designParallel program design
Parallel program design
 
Advanced Web Technology ass.pdf
Advanced Web Technology ass.pdfAdvanced Web Technology ass.pdf
Advanced Web Technology ass.pdf
 
C++ Language
C++ LanguageC++ Language
C++ Language
 
Tdd with python unittest for embedded c
Tdd with python unittest for embedded cTdd with python unittest for embedded c
Tdd with python unittest for embedded c
 
Part - 2 Cpp programming Solved MCQ
Part - 2  Cpp programming Solved MCQ Part - 2  Cpp programming Solved MCQ
Part - 2 Cpp programming Solved MCQ
 

Recently uploaded

Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsArshad QA
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Modelsaagamshah0812
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...ICS
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsJhone kinadey
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️Delhi Call girls
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AIABDERRAOUF MEHENNI
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Steffen Staab
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfkalichargn70th171
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...harshavardhanraghave
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfkalichargn70th171
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providermohitmore19
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comFatema Valibhai
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdfWave PLM
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerThousandEyes
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVshikhaohhpro
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...OnePlan Solutions
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...MyIntelliSource, Inc.
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsAndolasoft Inc
 

Recently uploaded (20)

Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.js
 

20BCE1734.pdf

  • 1. Course Code: CSE2005 Course Name: Operating System Lab Faculty: Dr. Bhanu Chander Balusa Slot: L21+L22 Program-1: Aim: Consider that two threads of a process should access the critical section. Assume that the critical section has a count variable with the initial value of 0. First thread increments the count variable by one for five times and second thread decrements the count variable by one for five times. Implement this job in C programming using Peterson’s solution of process synchronization. Background: NAME: Mridul Jadon REGNO: 20BCE1734
  • 2. CODE: #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <pthread.h> #define TRUE 1 #define FALSE 0 // Let us create a global variable count to change it in threads int count = 0; int favoured; int t1_wants_to_enter =FALSE; int t2_wants_to_enter=FALSE; void *increment(void *vargp) { // Store the value argument passed to this thread int *myid = (int *)vargp; t1_wants_to_enter=TRUE; favoured=2; while(t2_wants_to_enter==TRUE && favoured==2); // Change global variable for(int i=0;i<5;i++){ count++; printf("Thread ID: %d, Count: %dn", *myid, count); } t1_wants_to_enter=FALSE; // Print the argument and global variables } void *decrement(void *vargp) { // Store the value argument passed to this thread int *myid = (int *)vargp; t2_wants_to_enter=TRUE; favoured=1; while(t1_wants_to_enter==TRUE && favoured==1); // Change global variable for(int i=0;i<5;i++){ count--; printf("Thread ID: %d, Count: %dn", *myid, count); } t2_wants_to_enter=FALSE; // Print the argument and global variables printf("Thread ID: %d, Count: %dn", *myid, count); } int main() { int i; pthread_t tid1; pthread_t tid2; // Let us create three threads pthread_create(&tid1, NULL, increment, (void *)&tid1); pthread_create(&tid2, NULL, decrement, (void *)&tid2); pthread_exit(NULL); return 0; }
  • 3. OUTPUT: Program-2: Aim: Consider that two threads of a process should access the critical section. Assume that the critical section has a count variable with the initial value of 0. First thread increments the count variable by one for five times and second thread decrements the count variable by one
  • 4. for five times. Implement this job in C programming using Semaphore solution of process synchronization. Background: CODE: #include<pthread.h> #include<stdio.h> #include<semaphore.h> #include<unistd.h> void *fun1(); void *fun2(); int shared=1; sem_t s; int main() { sem_init(&s,0,1); pthread_t thread1, thread2; pthread_create(&thread1, NULL, fun1, NULL); pthread_create(&thread2, NULL, fun2, NULL);
  • 5. pthread_join(thread1, NULL); pthread_join(thread2,NULL); printf("Final value of shared is %dn",shared); } void *fun1() { int x; sem_wait(&s); x=shared; printf("Thread1 reads the value as %dn",x); for(int i=0;i<5;i++){ x++; } printf("Local updation by Thread1: %dn",x); sleep(1); shared=x; printf("Value of shared variable updated by Thread1 is: %dn",shared); sem_post(&s); } void *fun2() { int y; sem_wait(&s); y=shared; printf("Thread2 reads the value as %dn",y); for(int i=0;i<5;i++){ y--; } printf("Local updation by Thread2: %dn",y); sleep(1); shared=y; printf("Value of shared variable updated by Thread2 is: %dn",shared); sem_post(&s); } OUTPUT: