Problem: 1.Create four threads.
2. Thread 1 should display - "Hi im thread 1" and go to sleep for 1 second. This should loop 100times.
3. After 100 times of looping, thread 2 should display,sleep and loop.
4. All four threads must do the same.
Solutionn : I have tried writing a program and im new at this. So it would be a great help if you review this and tell me if its right.
#include<pthread.h>
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
void* thread1()
{
for(i=0;i<=100;i++)
{
printf("Hi,I'm thread 1\n");
sleep(1);
}
}
void* thread2()
{
for(i=0;i<=100;i++)
{
printf("Hi,I'm thread 2\n");
sleep(1)
}
}
//similarly thread3 and thread4
void main()
{
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;
}
Is this right ?![Smile :) :)](/styles/bom/smilies/smile.gif)
Thanks
2. Thread 1 should display - "Hi im thread 1" and go to sleep for 1 second. This should loop 100times.
3. After 100 times of looping, thread 2 should display,sleep and loop.
4. All four threads must do the same.
Solutionn : I have tried writing a program and im new at this. So it would be a great help if you review this and tell me if its right.
#include<pthread.h>
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
void* thread1()
{
for(i=0;i<=100;i++)
{
printf("Hi,I'm thread 1\n");
sleep(1);
}
}
void* thread2()
{
for(i=0;i<=100;i++)
{
printf("Hi,I'm thread 2\n");
sleep(1)
}
}
//similarly thread3 and thread4
void main()
{
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;
}
Is this right ?
![Smile :) :)](/styles/bom/smilies/smile.gif)
Thanks