21bai1892 Daa

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

Name : Madhuri Santhosh Srinivisan

Register No. : 21BAI1892

1.
#include <stdio.h>
#include <pthread.h>
void* threadFunction()
{
while(1)
{
printf("I am threadFunction.\n");
}
}
int main()
{
pthread_t id;
int ret;
ret=pthread_create(&id,NULL,&threadFunction,NULL);
if(ret==0){
printf("Thread created successfully.\n");
}
else{
printf("Thread not created.\n");
return 0;
}
while(1)
{
printf("I am main function.\n");
}
return 0;
}
2.
#include <iostream>
#include <cstdlib>
#include <pthread.h>
using namespace std;
#define NUM_THREADS 5
void *PrintHello(void *threadid) {
long tid;
tid = (long)threadid;
printf("Hello World! Thread ID, %d
", tid);
pthread_exit(NULL);
}
int main () {
pthread_t threads[NUM_THREADS];
int rc;
int i;
for( i = 0; i < NUM_THREADS; i++ ) {
cout << "main() : creating thread, " << i << endl;
rc = pthread_create(&threads[i], NULL, PrintHello, (void *)i);
if (rc) {
printf("Error:unable to create thread, %d", rc);
exit(-1);
}
}
pthread_exit(NULL);
}
3.
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
void* func(void* arg)
{
pthread_detach(pthread_self());
printf("Inside the thread\n");
pthread_exit(NULL);
}
void fun()
{
pthread_t ptid;
pthread_create(&ptid, NULL, &func, NULL);
printf("This line may be printed"
" before thread terminates\n");
if(pthread_equal(ptid, pthread_self()))
printf("Threads are equal\n");
else
printf("Threads are not equal\n");
pthread_join(ptid, NULL);

printf("This line will be printed"


" after thread ends\n");
pthread_exit(NULL);
}
int main()
{
fun();
return 0;
}
4.
#include <stdio.h>
#include <pthread.h>
#include <semaphore.h>
#include <unistd.h>
sem_t s;
void* T(void * arg) {
sem_wait(&s);
printf("Welcome! \n");
sleep(4);
printf("Bye!\n");
sem_post(&s);
}
int main() {
sem_init(&s, 0, 1);
pthread_t o1, o2;
printf("In a 1st Thread now...\n");
pthread_create(&o1,NULL,T,NULL);
sleep(4);
printf("In a 2nd Thread now...\n");
pthread_create(&o2,NULL,T,NULL);
pthread_join(o1,NULL);
pthread_join(o2,NULL);
sem_destroy(&s);
return 0;
}
5.
#include<stdio.h>
#include<string.h>
#include<semaphore.h>
#include<pthread.h>
sem_t sema1;
sem_t sema2;
sem_t sema3;

void* t1(void *arg)


{
for(int j=0;j<10;j++)
{
sem_wait(&sema1);
printf("Thread 1 \n");
sem_post(&sema2);
}
printf("T1 return\n");
return NULL;
}
void* t2(void *arg)
{
for(int j=0;j<10;j++)
{
sem_wait(&sema2);
printf("Thread 2 \n");
sem_post(&sema3);
}
printf("T2 return\n");
return NULL;
}

void* t3(void *arg)


{
for(int j=0;j<10;j++)
{
sem_wait(&sema3);
printf("Thread 3 \n");
sem_post(&sema1);
}
printf("T3 return \n");
return NULL;
}

int main()
{
pthread_t tid1, tid2,tid3;
sem_init(&sema1,0,1);
sem_init(&sema2,0,0);
sem_init(&sema3,0,0);
pthread_create(&tid1, NULL, t1, NULL);
usleep(100);
pthread_create(&tid2, NULL, t2, NULL);
usleep(100);
pthread_create(&tid3, NULL, t3, NULL);

pthread_join(tid3, NULL);
pthread_join(tid2, NULL);
pthread_join(tid1, NULL);
sem_destroy(&sema1);
sem_destroy(&sema2);
sem_destroy(&sema3);
return 0;
}
MULTI-THREADED PROGRAM
#include <stdio.h>
#include <pthread.h>

int sum = 0;
int n = 10;
int m = 5;
void *calculate_sum(void *arg) {
for(int i = 1; i <= n; i++) {
sum += i;
}
pthread_exit(NULL);
}
void *display_table(void *arg) {
for(int i = 1; i <= 10; i++) {
printf("%d x %d = %d\n", m, i, m*i);
}
pthread_exit(NULL);
}

int main() {
pthread_t thread1, thread2;
if(pthread_create(&thread1, NULL, calculate_sum, NULL)) {
printf("Error creating thread1\n");
return -1;
}
if(pthread_create(&thread2, NULL, display_table, NULL)) {
printf("Error creating thread2\n");
return -1;
}
if(pthread_join(thread1, NULL)) {
printf("Error joining thread1\n");
return -1;
}
if(pthread_join(thread2, NULL)) {
printf("Error joining thread2\n");
return -1;
}
printf("Sum of first %d natural numbers is: %d\n", n, sum);
printf("I am done");
return 0;
}

6.
#include <queue>
#include <iostream>
using namespace std;
struct Process {
int id;
};
struct Semaphore {
int value;
queue<Process*> q;
};
void P(Semaphore& s, Process* p) {
if (s.value == 1) {
s.value = 0;
}
else {
s.q.push(p);
cout << "Process " << p->id << " is blocked." << endl;
}
}
void V(Semaphore& s) {
if (s.q.empty()) {
s.value = 1;
}
else {
Process* p = s.q.front();
s.q.pop();
cout << "Process " << p->id << " is unblocked." << endl;
}
}
int main() {
Semaphore s = {1, queue<Process*>()};
Process p1 = {1};
Process p2 = {2};
P(s, &p1);
P(s, &p2);
V(s);
V(s);
return 0;
}
7.
#include <queue>
#include <iostream>
using namespace std;
struct Process {
int id;
};
struct Semaphore {
int value;
queue<Process*> q;
};
void P(Semaphore& s, Process* p) {
s.value--;
if (s.value < 0) {
s.q.push(p);
cout << "Process " << p->id << " is blocked." << endl;
}
}
void V(Semaphore& s) {
s.value++;
if (s.value <= 0) {
Process* p = s.q.front();
s.q.pop();
cout << "Process " << p->id << " is unblocked." << endl;
}
}
int main() {
Semaphore s = {1, queue<Process*>()};
Process p1 = {1};
Process p2 = {2};
P(s, &p1);
P(s, &p2);
V(s);
V(s);
return 0;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy