answersLogoWhite

0


Best Answer

/* program to study Reader/Writer problem with Semaphore along with output*/

#include<stdio.h>

#include<pthread.h>

#include<semaphore.h>

void reader1();

void writer1();

void reader2();

void writer2();

sem_t rsem;

sem_t wsem;

sem_t x,y,z;

int rc=0, wc=0,p=0;

int main()

{

int r,w,i=0,ch=2;

pthread_t t1[5],t2[5];

printf ("\n\nEnter the number of READERS: ");

scanf ("%d", &r);

printf ("\n\nEnter the number of WRITERS: ");

scanf ("%d", &w);

printf ("\n\nMENU\n\n\t1.READERS HAVE PRIORITY");

printf ("\n\n\t2.WRITER HAVE PRIORITY");

printf ("\n\n\tENTER YOUR CHOICE:");

scanf ("%d", &ch);

sem_init(&rsem, 0, 1);

sem_init(&wsem, 0, 1);

sem_init(&x, 0, 1);

sem_init(&y, 0, 1);

sem_init(&z, 0, 1);

if (ch==1)

{

for (i=0;i<r;i++)

pthread_create(&t1[i], NULL, (void *)reader1, (void *)i );

for (i=0; i<w; i++)

pthread_create(&t2[i], NULL, (void *)writer1, (void *)i);

//pthread_create(&t1[w-1], NULL, (void *)reader1, (void *)(w-1) );

//pthread_create(&t2[r-1], NULL, (void *)writer1, (void *)(r-1) );

for (i=0;i<r;i++)

pthread_join(t1[i], NULL);

for (i=0;i<w;i++)

pthread_join(t2[i], NULL);

}

else if (ch==2)

{

//pthread_create(&t2[0], NULL, (void *)writer2, 0);

//pthread_create(&t1[0], NULL, (void *)reader2, 0);

for (i=0;i<r;i++)

pthread_create(&t2[i], NULL, (void *)reader2, (void *)i );

for (i=1;i<w;i++)

pthread_create(&t1[i], NULL, (void *)writer2, (void *)i );

//pthread_create (&t2[r-1], NULL, (void *)writer2, (void *)(r-1) );

//pthread_create (&t1[w-1], NULL, (void *)reader2, (void *)(w-1) );

for(i=0; i<r; i++)

pthread_join(t1[i], NULL);

for(i=0; i<w; i++)

pthread_join(t2[i], NULL);

}

else

printf ("\nINVALID CHOICE....PROGRAM TERMINATED.");

sem_destroy(&rsem);

sem_destroy(&wsem);

return 0;

} // main function close

void reader1 (void *i)

{ int i1;

for(i1=0;i1<10;i1++)

{

sem_wait(&x);

rc++;

if (rc==1)

sem_wait(&wsem);

sem_post(&x);

printf ("\n\t \t \t \tREADER %d IS READING NOW %d", (int)i,p);

sem_wait(&x);

rc--;

if(rc==0)

sem_post(&wsem);

sem_post(&x);

sleep(2);

//printf ("\nREADER %d HAS FINISHED READING\n");

}

}

void writer1(void *i)

{ int i1;

for(i1=0;i1<10;i1++)

{ sem_wait(&wsem);

p++;

printf ("\nWRITER %d IS WRITING NOW %d", (int) i,p);

sem_post(&wsem);

if ((int)i % 2 == 0)sleep(1); else sleep(6);

// printf ("\nWRITER %d HAS FINISHED WRITING\n");

}

}

void reader2(void *i)

{

sem_wait(&z);

sem_wait(&rsem);

sem_wait(&x);

rc++;

if(rc==1)

sem_wait(&wsem);

printf ("\nREADER %d IS READING NOW", (int)i);

sem_post(&x);

sem_post(&rsem);

sem_post(&z);

sleep(1);

sem_wait(&x);

rc--;

if (rc==0)

sem_post(&wsem);

sem_post(&x);

//printf ("\nREADER %d HAS FINISHED READING\n");

}

void writer2(void *i)

{

sem_wait(&y);

wc++;

if (wc==1)

sem_wait(&rsem);

sem_post(&y);

sem_wait(&wsem);

printf ("\nWRITER %d IS WRITING NOW", (int) i);

sem_post(&wsem);

sem_wait(&y);

wc--;

if(wc==0)

sem_post(&rsem);

sem_post(&y);

sleep(1);

//printf ("\nWRITER %d HAS FINISHED WRITING\n");

}

/*

student@student-desktop:~/Documents$ ./rdwrt

Enter the number of READERS: 4

Enter the number of WRITERS: 4

MENU

1.READERS HAVE PRIORITY

2.WRITER HAVE PRIORITY

ENTER YOUR CHOICE:1

READER 0 IS READING NOW

READER 1 IS READING NOW

READER 2 IS READING NOW

READER 1 IS READING NOW

READER 2 IS READING NOW

READER 3 IS READING NOW

READER 1 HAS FINISHED READING

READER 0 HAS FINISHED READING

READER 1 HAS FINISHED READING

READER 2 HAS FINISHED READING

READER 2 HAS FINISHED READING

READER 3 HAS FINISHED READING

WRITER 0 IS WRITING NOW

WRITER 0 HAS FINISHED WRITING

WRITER 1 IS WRITING NOW

WRITER 1 HAS FINISHED WRITING

WRITER 2 IS WRITING NOW

WRITER 2 HAS FINISHED WRITING

WRITER 3 IS WRITING NOW

WRITER 3 HAS FINISHED WRITING

student@student-desktop:~/Documents$ ./rdwrt

Enter the number of READERS: 4

Enter the number of WRITERS: 4

MENU

1.READERS HAVE PRIORITY

2.WRITER HAVE PRIORITY

ENTER YOUR CHOICE:2

WRITER 0 IS WRITING NOW

WRITER 0 HAS FINISHED WRITING

WRITER 1 IS WRITING NOW

WRITER 1 HAS FINISHED WRITING

WRITER 2 IS WRITING NOW

WRITER 2 HAS FINISHED WRITING

WRITER 3 IS WRITING NOW

WRITER 3 HAS FINISHED WRITING

READER 1 IS READING NOW

READER 0 IS READING NOW

READER 2 IS READING NOW

READER 3 IS READING NOW

READER 2 HAS FINISHED READING

READER 1 HAS FINISHED READING

READER 0 HAS FINISHED READING

READER 3 HAS FINISHED READING

*/

Written by: Fabianski Benjamin

User Avatar

Wiki User

11y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you write a c program for reader writer problem?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering
Related questions

How does dignity relate to technical writing?

Technical writing can read like its writer knows more than the reader. This denies the reader the dignity the reader has earned. To avoid this, a technical writer can write as though the writer sits at the reader's elbow offering kind guidance. The writer can write in the third-person, in the present tense and in an active voice. This can help the reader preserve his or her dignity.


What is one reason a writer might choose to write in third person limited?

The writer wants to write from the perspective of one character. -apex


How do you write an exppository essays?

An expository essay is an introduction to the reader about a certain thing. The writer assumes that the reader knows nothing about the topic and explains it all.


How do you write the average n numbers program in c language?

Write your program and if you are having a problem post it here with a description of the problem you are having. What you are asking is for someone to do your homework for you. no i am asking to verify my answer


Write a problom that can be solved by making a table and using a pattern Then solve the problem?

You can draw a flowchart of solving a particular problem and then write a program to solve that problem.


How do the reader and writer relate to one another?

The writer must keep in mind that he or she is writing for an audience, and that their primary goal (with a few exceptions) is to be understood. Therefore, they are first and foremost a communicator, then a storyteller, and (last but not least) hopefully an entertainer. The reader's job (some might say responsibility) is to comprehend what the writer is trying to share. This means immersing themselves in the story, doing their best to understand what drives the characters, and (hopefully) keeping a piece of the story alive inside them after the reading is done. Without a writer, there are no readers; and without a reader, there's nobody to write for, and therefore no reason to write.


How do you write a c programme for elecricity bill?

Write your program and if you are having a problem post it here with a description of the problem you are having. What you are asking is for someone to do your homework for you.


A c program to square matrix?

A C program to square matrix is a math problem. In the math problem you write down all the outer boundary values of matrix in a circle, then write down the inner value.


What is writer based prose versus reader based prose?

Writer-based prose is prose that is written for the writer, whereas reader-based prose is prose that is written for the reader. What that means is that writer-based prose is composition that is probably early in the draft stages and aids the writer in formulating and articulating her argument. Writers learn as they write (writing is a process, not a product), so writer-based prose is written without regard to the audience's reading experience. If a draft does not follow a thesis all the way through and the argument changes because of new information the writer has garnered from her source, then the draft would be considered writer-based prose. Reader-based prose, on the other hand, is composition that is likely to be in it's final drafting stages and aids the reader in navigating through the writer's argument. Reader-based prose has been revised to take into account the reader's experience, not the writer's. The argument is easy to follow, the organization is pleasing, and the thesis sustains itself all the way through. Though reader-based prose is the goal for all writers, writing writer-based drafts is a necessary part of the writing process and is just as (if not more) valuable than a polished essay.


Write programs for String Reversal and Palindrome check?

Write your program and if you are having a problem post it here with a description of the problem you are having. What you are asking is for someone to do your homework for you.


Write a program in C programming language that computes the roots of the quadratic equation ax2 plus bx plus c?

Write your program and if you are having a problem post it here with a description of the problem you are having. What you are asking is for someone to do your homework for you.


Why does excellence in writing matter?

Excellence in writing means that you are able to express yourself fully. The better you write, the better you are communicating, and communicating with the reader is what makes a good writer into a great writer. The best stories - the ones that everyone remembers and talks about - are the ones where the writer put a little bit of his or her own heart down onto the page. You can't do that if you cannot write well.The better you write, the better you can convey your message.