You are on page 1of 8

Name: M.

Ahmed
Roll no : 17f-8168
Sec : A
Lab : 08

Problem 1:
Code:
#include<stdio.h>

#include<pthread.h>

#include<stdlib.h>

void * thread1()

int c=0;

while(c++ < 500)

printf("Hello !!\n");

void * thread2()

int c=0;

while(c++ < 500)

printf("How are you !!\n");

int main ()

int status;

pthread_t tid1,tid2;
pthread_create(&tid1,NULL,thread1,NULL);

pthread_create(&tid2,NULL,thread2,NULL);

pthread_join(tid1,NULL);

pthread_join(tid2,NULL);

return 0;

Output:
(i) For what purpose pthread_create(.....) and pthread_join(....) used in the
code?
Pthread_create() is used to create a thread and pthread_join(....) is used to wait for the created
thread.

(ii) How many times “Hello” will be printed and write the name of thread that
will print it?
Hello will be printed 500 times and thread1 will print it.

(iii) How many times ““How are you” will be printed and write the name of
thread that will print
it?
How are you will be printed 500 times and thread2 will print it.

(iv) Why does “Hello” not printed consecutively in the output, Explain?
This is because of context switching.each thread is getting cpu for a certain time.

(v) Modify the program to create four threads using the same two functions
and run both
versions and include screenshots of the output?
Code:

#include<stdio.h>

#include<pthread.h>

#include<stdlib.h>

int hcount=0;

void * thread1()

int c=0;

while(c++ < 500)

printf("Hello !!\n");

void * thread2()

{
int c=0;

while(c++ < 500)

printf("How are you !!\n");

void * thread3()

int c=0;

while(c++ < 500)

printf("How are you !!\n");

void * thread4()

int c=0;

while(c++ < 500)

printf("How are you !!\n");

int main ()

int status;

pthread_t tid1,tid2,tid3,tid4;

pthread_create(&tid1,NULL,thread1,NULL);

pthread_create(&tid2,NULL,thread2,NULL);

pthread_create(&tid3,NULL,thread3,NULL);

pthread_create(&tid4,NULL,thread4,NULL);

pthread_join(tid1,NULL);

pthread_join(tid2,NULL);

pthread_join(tid3,NULL);

pthread_join(tid4,NULL);
return 0;

Problem 2:
Code:
#include<stdio.h>

#include<pthread.h>

#include<stdlib.h>

int count=0,result=0;

void * thread1()

count++;

result+=count;

void * thread2()

count++;

result+=count;

void * thread3()

count++;

result+=count;

int main ()

int status;

pthread_t tid1,tid2,tid3,tid4;
pthread_create(&tid1,NULL,thread1,NULL);

pthread_create(&tid2,NULL,thread2,NULL);

pthread_create(&tid3,NULL,thread3,NULL);

pthread_join(tid1,NULL);

pthread_join(tid2,NULL);

pthread_join(tid3,NULL);

printf("\n\nCount:%d \nResult : %d \n",count,result);

return 0;

Output:

Problem 3:
Code:
#include<stdio.h>

#include<pthread.h>

#include<stdlib.h>

pthread_t * thread()

pthread_t pthread_self();

return pthread_self();

}
int main ()

int status;

pthread_t tid[4];

for(int i=0;i<4;i++)

pthread_create(&tid[i],NULL,thread,NULL);

for(int i=0;i<4;i++)

pthread_join(tid[i],NULL);

for(int i=0;i<4;i++)

printf("\n thread id of t%d = %d and process id=%d\n",i+1,tid[i],getpid());

return 0;

Output:

You might also like