#include< stdio.h> #include< pthread.h > #include< stdlib.h > #include< string.h > #define N 200 pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; char *n; int i=1,comp=1; void * read1() { while(comp) { pthread_mutex_lock(&mutex); n=(char *)malloc(N*sizeof(char)); printf("\nEnter a string:\t"); gets(n); if((i=strcmp("end",n))==0) comp=0; pthread_mutex_unlock(&mutex); usleep(10); }} void * write1() { while(comp) { usleep(50); pthread_mutex_lock(&mutex); if(i!=0) printf("\nThe string entered is : %s\n",n); pthread_mutex_unlock(&mutex); }} int main() { pthread_t tr, tw; int res; pthread_mutexattr_t attr; pthread_mutexattr_settype(&attr,PTHREAD_MUTEX_RECURSIVE); res=pthread_mutex_init(&mutex,&attr); pthread_create(&tr,NULL,read1,NULL); pthread_create(&tw,NULL,write1,NULL); //pthread_exit(NULL); return 0; }
main()
pthread_exit(NULL)
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)