NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

LAB 6-7

Ques 4

pthread_create() for thread 1 returns: 0 Thread 1 pthread_create() for thread 2 returns: 0 Thread 2



Ques 5

Thread number 140542581389056

Thread number 140542547830528

Thread number 140542514272000

Thread number 140542480713472

Thread number 140542447154944

Thread number 140542413596416

Thread number 140542380037888

Thread number 140542312920832

Thread number 140542346479360

Thread number 140542279362304

Final counter value: 10



Thread 1: Exiting critical region...



Ques 6

#include<stdio.h>

#include<semaphore.h>

#include<pthread.h>

#include<stdlib.h>

#define buffersize 10

pthread_mutex_t mutex;

pthread_t tidP,tidC;

sem_t full,empty;

int counter;

int buffer[buffersize];



void initialize()

{

pthread_mutex_init(&mutex,NULL);

sem_init(&full,1,0);

sem_init(&empty,1,buffersize);

counter=0;

}



void write(int item)

{

buffer[counter++]=item;

}



int read()

{

return(buffer[--counter]);

}



void * producer (void * param)

{

int waittime,item,i;

item=rand()%5;

waittime=rand()%5;

sem_wait(&empty);

pthread_mutex_lock(&mutex);

printf("nProducer has produced item: %dn",item);

write(item);

pthread_mutex_unlock(&mutex);

sem_post(&full);

}



void * consumer (void * param)

{

int waittime,item;

waittime=rand()%5;

sem_wait(&full);

pthread_mutex_lock(&mutex);

item=read();

printf("nConsumer has consumed item: %dn",item);

pthread_mutex_unlock(&mutex);

sem_post(&empty);

}



int main()

{

int n1,n2,i;

initialize();



pthread_create(&tidP,NULL,producer,NULL);



pthread_create(&tidC,NULL,consumer,NULL);



pthread_join(tidP,NULL);



pthread_join(tidC,NULL);



//sleep(5);

exit(0);

}



Ques 7

#include<stdio.h>

#include<pthread.h>

#include<semaphore.h>



sem_t mutex;

sem_t db;

int readercount=0;

pthread_t reader1,reader2,writer1,writer2;

void *reader(void *);

void *writer(void *);

void main()

{

sem_init(&mutex,0,1);

sem_init(&db,0,1);

int i=1;

while(i<=3)

{

pthread_create(&reader1,NULL,reader,"1");

pthread_create(&reader2,NULL,reader,"2");

pthread_create(&writer1,NULL,writer,"1");

pthread_join(reader1,NULL);

pthread_join(reader2,NULL);

pthread_join(writer1,NULL);

i++;



}





}

void *reader(void *p)

{

printf("prevoius value %dn",mutex);

sem_wait(&mutex);

printf("Mutex acquired by reader %dn",mutex);

readercount++;

if(readercount==1) sem_wait(&db);

sem_post(&mutex);

printf("Mutex returned by reader %dn",mutex);

printf("Reader %s is Readingn",p);

//sleep(3);

sem_wait(&mutex);

printf("Reader %s Completed Readingn",p);

readercount--;

if(readercount==0) sem_post(&db);

sem_post(&mutex);

}



void *writer(void *p)

{

printf("Writer is Waiting n");

sem_wait(&db);

printf("Writer %s is writingn ",p);

sem_post(&db);

//sleep(2);

}







     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.