0% found this document useful (0 votes)
97 views5 pages

Lab 5 and 6 Q1.1: Ankit Makhija (8103472) B3

This document contains code snippets and questions related to multithreading in C using pthreads. It discusses creating and joining threads, passing arguments to thread functions, using mutexes and semaphores for synchronization. The questions cover basic thread creation and synchronization, updating shared variables from multiple threads, and ensuring exclusive access to critical sections using mutexes.

Uploaded by

Aakshi Agarwal
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
97 views5 pages

Lab 5 and 6 Q1.1: Ankit Makhija (8103472) B3

This document contains code snippets and questions related to multithreading in C using pthreads. It discusses creating and joining threads, passing arguments to thread functions, using mutexes and semaphores for synchronization. The questions cover basic thread creation and synchronization, updating shared variables from multiple threads, and ensuring exclusive access to critical sections using mutexes.

Uploaded by

Aakshi Agarwal
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

ANKIT MAKHIJA

(8103472)

B3

Lab 5 and 6

Q1.1
#include <stdio.h>

#include <pthread.h>

void *run(void *p);

int main()

pthread_t tid,tid1;

pthread_attr_t a,a1;

void *p1="thread1";

void *p2="thread2";

pthread_attr_init(&a);

pthread_create(&tid,&a,run,p1);

pthread_attr_init(&a1);

pthread_create(&tid1,&a1,run,p2);

pthread_join(tid,NULL);

pthread_join(tid1,NULL);

pthread_exit(0);

void *run(void *p)

{char *p1=p;

int i;

for(i=1;i<=20;i++)
printf("%d->%s\n",i,p1);

pthread_exit(0);

//compile using "gcc -lpthread lab51.1.c"

//compile using "gcc -o outputfilename inputfilename -lpthread"

//now to run program use "./outputfilename" or "./a.out"

Q1.2

#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <sys/types.h>

void *run(void *p);


int main()
{
pthread_t tid1,tid2,tid3;
pthread_attr_t a1,a2,a3;
void *p1=1,*p2=2,*p3=3;
pthread_attr_init(&a1);
pthread_create(&tid1,&a1,run,p1);
//if we put all three pthread_join(tid,NULL)
commands together the order of execution of code of run
pthread_attr_init(&a2); //function for each thread would be
unpredictable
pthread_create(&tid2,&a2,run,p2);

pthread_attr_init(&a3);
pthread_create(&tid3,&a3,run,p3);
pthread_join(tid1,NULL);
pthread_join(tid2,NULL);
pthread_join(tid3,NULL);
pthread_exit(0);
}
void *run(void *p)
{
int n=p;
if(n==1)
{
printf("Thread->1\n");
system("ls");
}
if(n==2)
{
printf("Thread->2\n");
system("pwd");
}
if(n==3)
{
printf("Thread->3\n");
system("kill 1232");
}
pthread_exit(0);
}

Q2

#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <sys/types.h>
int counter=0;
void *run(void *p);
int main()
{
pthread_t tid1,tid2,tid3;
pthread_attr_t a1,a2,a3;
void *p1=5,*p2=10,*p3=20;
pthread_attr_init(&a1);
pthread_create(&tid1,&a1,run,p1);
pthread_attr_init(&a2);
pthread_create(&tid2,&a2,run,p2);
pthread_attr_init(&a3);
pthread_create(&tid3,&a3,run,p3);
pthread_join(tid1,NULL);
pthread_join(tid2,NULL);
pthread_join(tid3,NULL);
pthread_exit(0);
}
void *run(void *p)
{
int i=p;
counter=counter+i;
printf("%d\n",counter);
pthread_exit(0);
}

Q3

#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <sys/types.h>
int account1=0,account2=0;
void *run();
int main()
{
pthread_t tid0,tid1;
pthread_attr_t a1,a2;
int sum=account1+account2;
int i=0;
while(sum==0)
{i++;
pthread_attr_init(&a1);
pthread_create(&tid0,NULL,run,NULL);
pthread_attr_init(&a2);
pthread_create(&tid1,NULL,run,NULL);
pthread_join(tid0,NULL);
pthread_join(tid1,NULL);
sum=account1+account2;
}
printf("Number of iteration for race arround:- %d\n",i);
printf("SUM OF ACCOUNTS AFTER THREAD EXECUTION:- %d\n",(account1+account2));
pthread_exit(0);
}
void *run()
{
int num=5;
account1=account1+5;
account2=account2-5;
printf("SUM OF BOTH ACCOUNTS:- %d\n",(account1+account2));
pthread_exit(0);
}

Q4.1

#include <stdio.h>
#include <pthread.h>
#include <semaphore.h>
sem_t mutex;
void *run(void *p);
int main()
{
pthread_t tid,tid1;
void *p1="thread1";
void *p2="thread2";

sem_init(&mutex,0,1);
pthread_create(&tid,NULL,run,p1);
pthread_create(&tid1,NULL,run,p2);
pthread_join(tid,NULL);
pthread_join(tid1,NULL);
pthread_exit(0);
}
void *run(void *p)
{char *p1=p;
int i;
sem_wait(&mutex);
for(i=1;i<=20;i++)
printf("%d->%s\n",i,p1);
sem_post(&mutex);
pthread_exit(0);
}
Q4.2

#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <sys/types.h>
#include <semaphore.h>
sem_t mutex;
void *run(void *p);
int main()
{
pthread_t tid1,tid2,tid3;

void *p1=1,*p2=2,*p3=3;
sem_init(&mutex,0,1);

pthread_create(&tid1,NULL,run,p1);

pthread_create(&tid2,NULL,run,p2);

pthread_create(&tid3,NULL,run,p3);

pthread_join(tid1,NULL);
pthread_join(tid2,NULL);
pthread_join(tid3,NULL);
pthread_exit(0);
}
void *run(void *p)
{
int n=p;
sem_wait(&mutex);
if(n==1)
{
printf("Thread->1\n");
system("ls");
}
if(n==2)
{
printf("Thread->2\n");
system("pwd");
}
if(n==3)
{
printf("Thread->3\n");
system("kill 1232");
}
sem_post(&mutex);
pthread_exit(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