Friday, November 13, 2015

Implement Reader-Writer problem using OPENMP

PROGRAM:

#include<stdio.h>
#include<omp.h>
#include<string.h>

int main()
{

char buf1[100];
strcpy(buf1,"hi");
int i;
omp_set_num_threads(2);
omp_lock_t writelock;

omp_init_lock(&writelock);
    
#pragma omp parallel
{
     int tid=omp_get_thread_num();
if(tid==0)
     printf("I am THREAD:%d writing",omp_get_thread_num());
 
     omp_set_lock(&writelock);
strcat(buf1,"writing to the buffer");
   omp_unset_lock(&writelock);
}
 //strcat(buf1,"contents are getting written");
   if(tid==1)
{
omp_set_lock(&writelock);
 printf("\nI am THREAD:%d reading",omp_get_thread_num());
     printf("\nthe buffer contents are:\n %s",buf1);
omp_unset_lock(&writelock);
 }    


}
return 0;
}


OUTPUT :

[pict@localhost ~]$ gcc b9_1.c -fopenmp 
[pict@localhost ~]$ ./a.out
I am THREAD:0 writing
I am THREAD:1 reading
the buffer contents are:
 hi[pict@localhost ~]$ 

No comments:

Post a Comment

Perform a suitable assignment using Xen Hypervisor or equivalent open source to configure it. Give necessary GUI.

 To install kvm on Fedora:  yum install kvm  yum install virt-manager libvirt libvirt-python python-virtinst  su -c "yum install @v...