Showing posts with label cycle2. Show all posts
Showing posts with label cycle2. Show all posts

Tuesday, 8 November 2011

READERS WRITERS PROBLEM


//Program to implement readers writers problem
#include<stdio.h>
#include<pthread.h>
pthread_mutex_t mutex;
pthread_cond_t cond;
int nreaders=0,writing=0;

void acquire_read_lock()
{
  pthread_mutex_lock(&mutex);
  while(writing)
  pthread_cond_wait(&cond,&mutex);
  ++nreaders;
  pthread_mutex_unlock(&mutex);
}

void release_read_lock()
{
  pthread_mutex_lock(&mutex);
  --nreaders;
  if(nreaders==0)
  pthread_cond_broadcast(&cond);
  pthread_mutex_unlock(&mutex);
}

void acquire_write_lock()
{
  pthread_mutex_lock(&mutex);
  while(writing||nreaders>0)
  pthread_cond_wait(&cond,&mutex);
  writing=1;
  pthread_mutex_unlock(&mutex);
}

void release_write_lock()
{
  pthread_mutex_lock(&mutex);
  writing=0;
  pthread_cond_broadcast(&cond);
  pthread_mutex_unlock(&mutex);
}

void *reader(void *s)
{
  int i;
  while(1)
  {
   acquire_read_lock();
   for(i=0;i<5;i++)
   {
    printf("Reader id %d, count %d\n",(int)s,i);
    sleep(1);
   }
   release_read_lock();
   sched_yield();
  }
}

void *writer(void *s)
{
  int i;
  while(1)
  {
   acquire_write_lock();
   for(i=0;i<5;i++)
   {
    printf("Writer id %d, count %d\n",(int)s,i);
    sleep(1);
   }
   release_write_lock();
   sched_yield();
  }
}

main()
{
  pthread_t t[6];
  pthread_attr_t attr;
  int j;

  pthread_mutex_init(&mutex,0);
  pthread_cond_init(&cond,0);

  for(j=0;j<3;j++)
  pthread_create(&t[j],0,reader,(void*)j);
  for(;j<6;j++)
  pthread_create(&t[j],0,writer,(void*)j);
  while(1);
}

OUTPUT
gcc readerwriter.c -lpthread
./aout

Reader id 0, count 0
Reader id 1, count 0
Reader id 2, count 0
Reader id 0, count 1
Reader id 1, count 1
Reader id 2, count 1
Reader id 0, count 2
Reader id 1, count 2
Reader id 2, count 2
Reader id 0, count 3
Reader id 1, count 3
Reader id 2, count 3
Reader id 0, count 4
Reader id 1, count 4
Reader id 2, count 4
Writer id 4, count 0
Writer id 4, count 1
Writer id 4, count 2
Writer id 4, count 3
Writer id 4, count 4
Writer id 5, count 0
Writer id 5, count 1
Writer id 5, count 2
Writer id 5, count 3

BANKERS ALGORITHM


#include<stdio.h>
#include<pthread.h>
#include<semaphore.h>
#include<sys/types.h>
#include<sys/stat.h>
#define N 5
#define LEFT (N+n-1)%N
#define RIGHT (n+1)%N
enum{thinking,eating,hungry};
int philstate[N];
sem_t s[N],mutex;
void think(int n)
{ sleep(1);
}
void test(int n)
{
if(philstate[LEFT]!=eating && philstate[RIGHT]!=eating)
{
philstate[n]=eating;
sem_post(s+n);
}
sem_post(&mutex);
}
void getforks(int n)
{
sem_wait(&mutex);
philstate[n]=hungry;
printf("\n PHILOSOPHER %d IS HUNGRY ",n);
sleep(1);
test(n);
sem_post(&mutex);
sem_wait(s+n);
}
void eat(int n)
{
int i;
for(i=0;i<4;i++)
{
printf("\n PHILOSOPHER %d IS EATING",n);
sleep(1);
}
printf("\nPHILOSOPHER %d HAS STOPPED EATING",n);
sleep(1);
}
void putforks(int n)
{
sem_wait(&mutex);
philstate[n]=thinking;
printf("\n PHILOSOPHER  %d IS THINKING.",n);
sleep(1);
test(LEFT);
test(RIGHT);
sem_post(&mutex);
}
void *phil(void *argc)
{
int p;
p=atoi(argc);
printf("\n PHILOSOPHER %d:\n",p);
while(1)
{
think(p);
getforks(p);
eat(p);
putforks(p);
}
}
main()
{
sem_init(&mutex,0,1);
int i;
for(i=0;i<5;i++)
sem_init(&s[i],0,0);
pthread_t t1,t2,t3,t4,t5;
pthread_create(&t1,0,phil,"0");
pthread_create(&t2,0,phil,"1");
pthread_create(&t3,0,phil,"2");
pthread_create(&t4,0,phil,"3");
pthread_create(&t5,0,phil,"4");
while(1);
}


OUTPUT
./a.out

DINING PHILOSOPHERS PROBLEM


#include<stdio.h>
#include<pthread.h>
#include<semaphore.h>
#include<sys/types.h>
#include<sys/stat.h>
#define N 5
#define LEFT (N+n-1)%N
#define RIGHT (n+1)%N
enum{thinking,eating,hungry};
int philstate[N];
sem_t s[N],mutex;
void think(int n)
{ sleep(1);
}
void test(int n)
{
if(philstate[LEFT]!=eating && philstate[RIGHT]!=eating)
{
philstate[n]=eating;
sem_post(s+n);
}
sem_post(&mutex);
}
void getforks(int n)
{
sem_wait(&mutex);
philstate[n]=hungry;
printf("\n PHILOSOPHER %d IS HUNGRY ",n);
sleep(1);
test(n);
sem_post(&mutex);
sem_wait(s+n);
}
void eat(int n)
{
int i;
for(i=0;i<4;i++)
{
printf("\n PHILOSOPHER %d IS EATING",n);
sleep(1);
}
printf("\nPHILOSOPHER %d HAS STOPPED EATING",n);
sleep(1);
}
void putforks(int n)
{
sem_wait(&mutex);
philstate[n]=thinking;
printf("\n PHILOSOPHER  %d IS THINKING.",n);
sleep(1);
test(LEFT);
test(RIGHT);
sem_post(&mutex);
}
void *phil(void *argc)
{
int p;
p=atoi(argc);
printf("\n PHILOSOPHER %d:\n",p);
while(1)
{
think(p);
getforks(p);
eat(p);
putforks(p);
}
}
main()
{
sem_init(&mutex,0,1);
int i;
for(i=0;i<5;i++)
sem_init(&s[i],0,0);
pthread_t t1,t2,t3,t4,t5;
pthread_create(&t1,0,phil,"0");
pthread_create(&t2,0,phil,"1");
pthread_create(&t3,0,phil,"2");
pthread_create(&t4,0,phil,"3");
pthread_create(&t5,0,phil,"4");
while(1);
}


OUTPUT
./a.out

Time sharing


#include<stdio.h>
#include<sys/types.h>
main()
{
int d,i;
d=fork();
for(i=0;i<3;i++)
{
if(d==0)
{
printf("\n CHILD PROCESS \n");
sleep(3);
}
else
{
printf("\n PARENT PROCESS \n");
sleep(3);
}
}
}

OUTPUT
./a.out

Child Process

Child Process

Parent Process

Child Process

Parent Process

Parent Process

PROCESS CREATION


#include<sys/types.h>
#include<unistd.h>
#include<stdio.h>
main()
{
int p;
p=fork();
if(p==0)
{
printf("PROCESS ID IS %d\n",getpid());
printf("PARENT PROCESS ID IS %d\n",getppid());
}
}

OUTPUT
./a.out
process id of child : 8001

process id of parent : 8000

Disk Usage

#include<stdio.h>
#include<sys/vfs.h>
#include<sys/types.h>
#include<sys/stat.h>
main()
 {
  char *filename="/home"
struct statfs buf;
if(! statvfs(filename, &buf))
{
unsigned long bufsize,blocks,freeblks,disk_size,used,free,blksize;
blksize=buf.f_bsize;
blocks=buf.f_blocks;
freeblks=buf.f_bfree;
disk_size=((blocks*blksize)/1024)/1024;
free=((freeblks*blksize)/1024)/1024;
used=(disk_size)-(free);
printf("\n Disk Usage %d Free Space %d",used,free);
}
else
printf("couldnot get status\n");
retutn 0;
}

OUTPUT
./a.out
Disk Usage 242 Free space 2749