Showing Page:
1/3
OS LAB 10:
Process syncronization:
#include <stdio.h>
#include <pthread.h>
void myfun1(void *ptr);
void myfun2(void *ptr);
int main(int argc,char *argv[])
{
pthread_t thread1;
pthread_t thread2;
char *msg1="Thread 1";
char *msg2="Thread 2";
pthread_create(&thread1,NULL,(void *) &myfun1,(void *)msg1);
pthread_create(&thread2,NULL,(void *) &myfun2,(void *)msg2);
pthread_join(thread1,NULL);
pthread_join(thread2,NULL);
return 0;
}
void myfun1(void *ptr)
{
char *msg=(char *)ptr;
printf("%s\n",msg);
pthread_exit(0);
}
void myfun2(void *ptr)
{
char *msg=(char *)ptr;
printf("%s\n",msg);
pthread_exit(0);
}
WHEN WE COMBINE ALL THREAD AND RUN BY SEM
#include <stdio.h>
#include <pthread.h>
#include <semaphore.h>
#include <stdlib.h>
char buf[24];
Showing Page:
2/3
sem_t mutex;
void myfunc1(void *ptr)
{
char *msg=(char *)ptr;
printf("%s",msg);
sem_wait(&mutex);
sprintf(buf,"%s","hello there");
sem_post(&mutex);
pthread_exit(0);
}
void myfunc2(void *ptr)
{
char *msg=(char *)ptr;
printf("%s",msg);
sem_wait(&mutex);
sprintf(buf,"%s","hello there");
sem_post(&mutex);
pthread_exit(0);
}
int main()
{
pthread_t thread1;
pthread_t thread2;
char *msg1="thread 1";
char *msg2="thread 2";
sem_init(&mutex,0,1);
pthread_create(&thread1,NULL,(void *) &myfunc1,(void *) msg1);
pthread_create(&thread2,NULL,(void *) &myfunc2,(void *) msg2);
pthread_join(thread1,NULL);
pthread_join(thread2,NULL);
sem_destroy(&mutex);
return 0;
}
MUTX
#include <stdio.h>
#include <pthread.h>
void myfnc1(void *ptr);
void myfnc2(void *ptr);
char buf[24];
Showing Page:
3/3
int main(int argc,char *argv[])
{
pthread_t thread1;
pthread_t thread2;
char *msg1="Thread 1";
char *msg2="Thread 2";
pthread_create(&thread1,NULL,(void *) &myfnc1,(void *)msg1);
pthread_create(&thread2,NULL,(void *) &myfnc2,(void *)msg2);
pthread_join(thread1, NULL);
pthread_join(thread2, NULL);
return 0;
}
void myfnc1(void *ptr)
{
char *msg=(char *)ptr;
printf("%s\n",msg);
sprintf(buf, "%s", "Hello There!");
pthread_exit(0);
}
void myfnc2(void *ptr)
{
char *msg=(char *)ptr;
printf("%s\n",msg);
printf("%s\n",buf);
pthread_exit(0);
}

Unformatted Attachment Preview

OS LAB 10: Process syncronization: #include #include void myfun1(void *ptr); void myfun2(void *ptr); int main(int argc,char *argv[]) { pthread_t thread1; pthread_t thread2; char *msg1="Thread 1"; char *msg2="Thread 2"; pthread_create(&thread1,NULL,(void *) &myfun1,(void *)msg1); pthread_create(&thread2,NULL,(void *) &myfun2,(void *)msg2); pthread_join(thread1,NULL); pthread_join(thread2,NULL); return 0; } void myfun1(void *ptr) { char *msg=(char *)ptr; printf("%s\n",msg); pthread_exit(0); } void myfun2(void *ptr) { char *msg=(char *)ptr; printf("%s\n",msg); pthread_exit(0); } WHEN WE COMBINE ALL THREAD AND RUN BY SEM #include #include #include #include char buf[24]; sem_t mutex; void myfunc1(void *ptr) { char *msg=(char *)ptr; printf("%s",msg); sem_wait(&mutex); sprintf(buf,"%s","hello there"); sem_post(&mutex); pthread_exit(0); } void myfunc2(void *ptr) { char *msg=(char *)ptr; printf("%s",msg); sem_wait(&mutex); sprintf(buf,"%s","hello there"); sem_post(&mutex); pthread_exit(0); } int main() { pthread_t thread1; pthread_t thread2; char *msg1="thread 1"; char *msg2="thread 2"; sem_init(&mutex,0,1); pthread_create(&thread1,NULL,(void *) &myfunc1,(void *) msg1); pthread_create(&thread2,NULL,(void *) &myfunc2,(void *) msg2); pthread_join(thread1,NULL); pthread_join(thread2,NULL); sem_destroy(&mutex); return 0; } MUTX #include #include void myfnc1(void *ptr); void myfnc2(void *ptr); char buf[24]; int main(int argc,char *argv[]) { pthread_t thread1; pthread_t thread2; char *msg1="Thread 1"; char *msg2="Thread 2"; pthread_create(&thread1,NULL,(void *) &myfnc1,(void *)msg1); pthread_create(&thread2,NULL,(void *) &myfnc2,(void *)msg2); pthread_join(thread1, NULL); pthread_join(thread2, NULL); return 0; } void myfnc1(void *ptr) { char *msg=(char *)ptr; printf("%s\n",msg); sprintf(buf, "%s", "Hello There!"); pthread_exit(0); } void myfnc2(void *ptr) { char *msg=(char *)ptr; printf("%s\n",msg); printf("%s\n",buf); pthread_exit(0); } Name: Description: ...
User generated content is uploaded by users for the purposes of learning and should be used following Studypool's honor code & terms of service.
Studypool
4.7
Trustpilot
4.5
Sitejabber
4.4