Skip to main content

C Program

 #include <stdio.h>

#include <stdlib.h>

#include <unistd.h>

#include <pthread.h>


int SIZE=50000;

int TAIL;

int HEAD;

pid_t MYARRAY[50000];


pthread_mutex_t mutex;

pthread_cond_t condInserted;

pthread_cond_t condRemoved;



int isFull(){

if((HEAD+1)%SIZE==TAIL){

return 1;

}

return 0;

}


int isEmpty(){

if(HEAD==TAIL){

return 1;

}

return 0;

}


void * thread1(){

while(1){

pthread_mutex_lock(&mutex);//mutex object referenced by mutex shall be locked

if(isFull()){

printf("Full queue");

pthread_cond_wait(&condRemoved,&mutex);

}

pid_t tid = gettid();

HEAD = (HEAD+1)%SIZE;

MYARRAY[HEAD] = tid;

printf("\nWriter:%d\n",tid);

pthread_mutex_unlock(&mutex);//mutex object referenced by mutex  be unlocked

pthread_cond_signal(&condInserted);//call unblocks at least one of the threads that are blocked on the specified condition variable cond

usleep(300000);

}

}



void* thread2(){

while(1){

pthread_mutex_lock(&mutex);

pid_t a = -1;

pid_t tid = gettid();//returns the caller's thread ID

if(isEmpty()){

printf("Empty queue");

pthread_cond_wait(&condInserted,&mutex);//used to block on a condition variable

}

TAIL = (TAIL+1)%SIZE;

a = MYARRAY[TAIL];

MYARRAY[TAIL] = -1;

printf("\n\t\t\t\t\t\t\t\tReader : %d,READ:%d\n",tid,a);

pthread_mutex_unlock(&mutex);

pthread_cond_signal(&condRemoved);

usleep(200000);//suspends execution of the calling thread

       //for (at least) usec microseconds

}

}

int main() {

SIZE = 50000;

TAIL=HEAD=0;

pthread_mutex_init(&mutex,NULL);// initialises the mutex

pthread_cond_init(&condInserted,NULL);//initialises the condition variable

pthread_cond_init(&condRemoved,NULL);//initialises the condition variable

pthread_t writer_threads[20];

pthread_t reader_threads[2];

for(int i=0;i<20;i++){

pthread_create(&writer_threads[i],NULL,&thread1,NULL);

}

for (int i = 0; i < 2; i++) {

       pthread_create(&reader_threads[i], NULL, &thread2, NULL);

   }

    for (int i = 0; i < 20; i++) {

       pthread_join(writer_threads[i], NULL);

   }


   for (int i = 0; i < 2; i++) {

       pthread_join(reader_threads[i], NULL);

   }

pthread_mutex_destroy(&mutex);//shall destroy the mutex object referenced by mutex

pthread_cond_destroy(&condInserted);//destroy the given condition variable specified by cond

pthread_cond_destroy(&condRemoved);

return 0;

}

Comments

Popular posts from this blog

Servlet Project Book Shop Application in eclipse

 Servlet Project Book Shop Application in eclipse  For explanation watch video::: Note :: In this Project you must configure web server (for example tomcat) with eclipse ide Download Bootstrap  from ::         https://getbootstrap.com/docs/4.3/getting-started/download/ Download mysql jar file from :: https://mvnrepository.com/artifact/mysql/mysql-connector-java/8.0.22 adding MySQL Connector/J jar file in eclipse ide for jdbc video :: video link : https://youtu.be/4Fyd-k3eG_I Directory Structure:: web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0">   <welcome-file-list>     <welcome-file>home.html</welcome-file>   </welcome-file-list>   <display-...

JDBC basic example For Select Query

JDBC basic example For Select Query  For explanation watch video:  For Creating Table:: SQL> create table emp60(srno int,fname varchar2(10)); Table created. SQL> desc emp60;  Name                                      Null?    Type  ----------------------------------------- -------- ----------------------------  SRNO                                               NUMBER(38)  FNAME                                              VARCHAR2(10) SQL> insert into emp60 values(1,'allu'); 1 row created. SQL> insert into emp60 values(2,'vijay'); 1 row created. SQL> insert into emp60 values(3,'rajni'); 1 row created. SQL...

JDBC Program to access table data from mysql database

 import java.sql.*; class MysqlCon  { public static void main(String[] args)  { try{ Connection con = DriverManager.getConnection("jdbc:mysql:///new","root","root"); Statement st = con.createStatement(); String query = "select * from login"; ResultSet rs = st.executeQuery(query); while(rs.next()){ System.out.println(rs.getString(1)+" "+rs.getString(2)); } con.close(); }catch(SQLException e){ System.out.println("Error"); }catch(Exception e){ } } }