Write program that has two threads one is reading numbers from the user and the other is printing them such that the first thread reads a new number only after it has been printed by the second thread. Declare one global varaible to use for reading and printing.
#include <stdio.h>
#include <pthread.h>
#include <semaphore.h>
sem_t m;
void *f1(void *arg)
{
sem_wait(&m);
scanf("%d",&n);
}
void *f2(void *arg)
{
printf("Second\n");
}
int main(){
int n;
pthread_t t1, t2;
sem_init(&m, 0, 1);
pthread_create(&t1, NULL, f1, NULL);
pthread_create(&t2, NULL, f2, NULL);
pthread_join(t1, NULL);
pthread_join(t2, NULL);
sem_destroy(&m);
sem_destroy(&m2);
return 0;
}