How reader and writer problem was implemented and come up
with effective solution for reader and writer problem in
case we have n readers and 1 writer.

Answer Posted / ashwini

#include<stdio.h>
#include<pthread.h>

pthread_mutex_t m;

void* writer(void*);
void* reader(void*);

FILE *fp;



main()
{
pthread_t tid1,tid2,tid3;
pthread_mutex_init(&m,NULL);
pthread_create(&tid2,NULL,reader,NULL);
pthread_create(&tid1,NULL,writer,NULL);
pthread_create(&tid3,NULL,reader,NULL);

pthread_join(tid2,NULL);
pthread_join(tid1,NULL);
pthread_join(tid3,NULL);
}
void* writer(void *str)
{
pthread_mutex_lock(&m);

fp=fopen("file1.txt","w");
global += 35;
fprintf(fp,"%d",100);
fclose(fp);
pthread_mutex_unlock(&m);
}
void* reader(void *param)
{
int abc;
pthread_mutex_lock(&m);
fp=fopen("file1.txt","r");
fscanf(fp,"%d",&abc);
printf("%d",global);
fclose(fp);
pthread_mutex_unlock(&m);
}

Is This Answer Correct ?    15 Yes 12 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

write a program that reverses the input number of n.Formulate an equation to come up with the answer.

7041


write a function that reverse the elements of an array in place.The function must accept only one pointer value and return void.

3958


Code for Method of Handling Factorials of Any Size?

2006


Write a C/C++ program that connects to a MySQL server and displays the global TIMEZONE.

4579


Write a program that print in screen a tree with its height taken from user by entering number of 4 digits and find the odd numbers then calculate the sum of odd numbers so he get the height of tree?

2915






We need to write the function to check the password entered is correct or not based on the following conditions.. a) It must have atleast one lower case character and one digit. b)It must not have any Upper case characters and any special characters c) length should be b/w 5-12. d) It should not have any same immediate patterns like abcanan1 : not acceptable coz of an an pattern abc11se: not acceptable, coz of pattern 11 123sd123 : acceptable, as not immediate pattern adfasdsdf : not acceptable, as no digits Aasdfasd12: not acceptable, as have uppercase character

3957


Given a table of the form: Product Sold on A 1/1/1980 B 1/1/1980 C 1/1/1980 A 1/1/1980 B 1/1/1980 C 2/1/1980 A 2/1/1980 There are 30 products and 10,000 records of such type. Also the month period during which sales happened is given to u. Write the program to display the result as: Product Month No. of copies A January 12 A February 15 A March 27 B January 54 B February 15 B March 10 C January 37

2051


Write a C++ program without using any loop (if, for, while etc) to print prime numbers from 1 to 100 and 100 to 1 (Do not use 200 print statements!!!)

3274


how to diplay a external image of output on winxp by using c & c++,

2938


write a program to convert temperature from fa height into celcius and vise versa,use modular programming

2441


How to Split Strings with Regex in Managed C++ Applications?

3128


Code for Small C++ Class to Transform Any Static Control into a Hyperlink Control?

2565


Code for Easily Using Hash Table?

2391


find level of following tree (state, parent) " J,D I,D H,C E,B F,B G,C B,A D,A C,A A,& K,E L,E L,F M,F N,G O,H P,I P,H Q,I R,J S,K U,P T,L

2010


Write a simple encryption program using string function which apply the substitution method.

5538