Thursday, 21 March 2013

cyrus.c computer graphics


#include<stdio.h>
#include<conio.h>
#include<graphics.h>

void clip(double*,double*,double*,double*,int *);
int clipt(double denom,double num,double *te,double *tl);

double xmin,ymin,xmax,ymax;
int main()
{
double x0,y0,x1,y1;
int vis;
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
printf("Enter coordinates of window:\n");
scanf("%lf%lf%lf%lf",&xmin,&ymin,&xmax,&ymax);
printf("Enter coordinates of line:\n");
scanf("%lf%lf%lf%lf",&x0,&y0,&x1,&y1);
cleardevice();
rectangle(xmin,ymin,xmax,ymax);
line(x0,y0,x1,y1);
getch();
cleardevice();
rectangle(xmin,ymin,xmax,ymax);
clip(&x0,&y0,&x1,&y1,&vis);
if(vis)
{
line(x0,y0,x1,y1);
}
getch();
getch();
closegraph();

return 0;
}

void clip(double *x0,double *y0,double *x1,double *y1,int *vis)
{
double dx=*x1-*x0;
double dy=*y1-*y0;
double te=0.0,tl=1.0;
*vis=0;
if(clipt(dx,xmin-*x0,&te,&tl))
if(clipt(-dx,*x0-xmax,&te,&tl))
if(clipt(dy,ymin-*y0,&te,&tl))
if(clipt(-dy,*y0-ymax,&te,&tl))
{
*vis=1;
if(tl<1)
{
*x1=*x0+tl*dx;
*y1=*y0+tl*dy;
}

if(te>0)
{
*x0=*x0+te*dx;
*y0=*y0+te*dy;
}

}
 return ;
}

int clipt(double denom,double num,double *te,double *tl)
{
double t;
if(denom>0)//PE
{
t=num/denom;
if(t>*tl)
return 0;
else if(t>*te)
*te=t;
}
else if(denom<0)//PL
{
t=num/denom;
if(t<*te)
return 0;
else
*tl=t;
}
else if(num>0)
return 0;
return 1;
}

liba.c computer graphics


#include<stdio.h>
#include<conio.h>
#include<graphics.h>

void clip(double*,double*,double*,double*,int *);
int clipt(double denom,double num,double *te,double *tl);

double xmin,ymin,xmax,ymax;
int main()
{
double x0,y0,x1,y1;
int vis;
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
printf("Enter coordinates of window:\n");
scanf("%lf%lf%lf%lf",&xmin,&ymin,&xmax,&ymax);
printf("Enter coordinates of line:\n");
scanf("%lf%lf%lf%lf",&x0,&y0,&x1,&y1);
cleardevice();
rectangle(xmin,ymin,xmax,ymax);
line(x0,y0,x1,y1);
getch();
cleardevice();
rectangle(xmin,ymin,xmax,ymax);
clip(&x0,&y0,&x1,&y1,&vis);
if(vis)
{
line(x0,y0,x1,y1);
}
getch();
getch();
closegraph();

return 0;
}

void clip(double *x0,double *y0,double *x1,double *y1,int *vis)
{
double dx=*x1-*x0;
double dy=*y1-*y0;
double te=0.0,tl=1.0;
*vis=0;
if(clipt(dx,xmin-*x0,&te,&tl))
if(clipt(-dx,*x0-xmax,&te,&tl))
if(clipt(dy,ymin-*y0,&te,&tl))
if(clipt(-dy,*y0-ymax,&te,&tl))
{
*vis=1;
if(tl<1)
{
*x1=*x0+tl*dx;
*y1=*y0+tl*dy;
}

if(te>0)
{
*x0=*x0+te*dx;
*y0=*y0+te*dy;
}

}
 return ;
}

int clipt(double denom,double num,double *te,double *tl)
{
double t;
if(denom>0)//PE
{
t=num/denom;
if(t>*tl)
return 0;
else if(t>*te)
*te=t;
}
else if(denom<0)//PL
{
t=num/denom;
if(t<*te)
return 0;
else
*tl=t;
}
else if(num>0)
return 0;
return 1;
}

look disk scheduling


#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
int max,min,disk,mark[100],q[100],rear=0,size,head,seek;
void ins();
void dis();
void look();
void fmaxmin();
main()
{
int i,ch;
printf("\nEnter the size of the disk (in tracks) : ");
scanf("%d",&disk);
printf("\nEnter the current position of the disk head : ");
scanf("%d",&head);
printf("\nEnter the number of requests : ");
scanf("%d",&size);
if(size!= 0)
printf("\nEnter the requests - : \n\n");
else
{
printf("No requests to be made ");
return;
}
for(i=1;rear<=size;i++)
{
ins();
mark[i] = 0;
}
fmaxmin();
q[0] = head;
printf("\n\n\t\t\t\tDISK SCHEDULING\n\n");
head = q[0];
seek = 0;
look();
getch();
}
void ins()
{
int i;
rear++;
if(rear<=size)
{
printf("Enter the request %d to track no. : ",rear);
scanf("%d",&i);
}
if(i>disk)
{
printf("\nEnter valid track request\n\n");
rear--;
}
else
q[rear] = i;
}
void dis()
{
int i;
for(i=1;i<=size;i++)
printf("%d\t",q[i]);
}
void look()
{
int i,j;
printf("\n\n\t\t\t\tLOOK SCHEDULING\n\n");
printf("\nWORK QUEUE : \n");
dis();
printf("\n\n--------------------------------------------------------------------------------\n");
printf("\nThe sequence is. . .\n\n");
for(j=q[0];j<=max;j++)
{
for(i=1 ;i<=size;i++)
if(j == q[i] && mark[i] == 0)
{
mark[i] = 1;
printf("\nHead moves from %d to %d...",head,q[i]);
seek += abs(head -j);
head = q[i];
}
}
if(head>min)
{
for(j=q[0];j>=0;j--)
{
for(i=1 ;i<=size;i++)
if(j == q[i] && mark[i] == 0)
{
mark[i] = 1;
printf("\nHead moves from %d to %d...",head,q[i]);
seek += abs(head -j);
head = q[i];
}
}
}
printf("\n\nTotal number of head movements is : %d",seek);
}
void fmaxmin()
{
int i;
for(i = 1;i<=size;i++)
{
if(q[i]>max)
max = q[i];
if(q[i]<min)
min = q[i];
}
}

scan disk scheduling


#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
int max,min,disk,mark[100],q[100],rear=0,size,head,seek;
void ins();
void dis();
void clook();
void fmaxmin();
main()
{
int i,ch;
printf("\nEnter the size of the disk (in tracks) : ");
scanf("%d",&disk);
printf("\nEnter the current position of the disk head : ");
scanf("%d",&head);
printf("\nEnter the number of requests : ");
scanf("%d",&size);
if(size!= 0)
printf("\nEnter the requests - : \n\n");
else
{
printf("No requests to be made ");
return;
}
for(i=1;rear<=size;i++)
{
ins();
mark[i] = 0;
}
fmaxmin();
q[0] = head;
printf("\n\n\t\t\t\tDISK SCHEDULING\n\n");
head = q[0];
seek = 0;
clook();
getch();
}
void ins()
{
int i;
rear++;
if(rear<=size)
{
printf("Enter the request %d to track no. : ",rear);
scanf("%d",&i);
}
if(i>disk)
{
printf("\nEnter valid track request\n\n");
rear--;
}
else
q[rear] = i;
}
void dis()
{
int i;
for(i=1;i<=size;i++)
printf("%d\t",q[i]);
}
void clook()
{
int i,j;
printf("\n\n\t\t\t\tCLOOK SCHEDULING\n\n");
printf("\nWORK QUEUE : \n");
dis();
printf("\n\n--------------------------------------------------------------------------------\n");
printf("\nThe sequence is. . .\n\n");
for(j=q[0];j<=max;j++)
{
for(i=1 ;i<=size;i++)
if(j == q[i] && mark[i] == 0)
{
mark[i] = 1;
printf("\nHead moves from %d to %d...",head,q[i]);
seek += abs(head -j);
head = q[i];
}
}
if(head>min)
{
for(j=0;j<=q[0];j++)
{
for(i=1 ;i<=size;i++)
if(j == q[i] && mark[i] == 0)
{
mark[i] = 1;
printf("\nHead moves from %d to %d...",head,q[i]);
seek += abs(head -j);
head = q[i];
}
}
}
printf("\n\nTotal number of head movements is : %d",seek);
}
void fmaxmin()
{
int i;
for(i = 1;i<=size;i++)
{
if(q[i]>max)
max = q[i];
if(q[i]<min)
min = q[i];
}
}

sstf disk scheduling


#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
int max,min,disk,mark[100],q[100],rear=0,size,head,seek;
void ins();
void dis();
void sstf();
void fmaxmin();
main()
{
int i,ch;
printf("\nEnter the size of the disk (in tracks) : ");
scanf("%d",&disk);
printf("\nEnter the current position of the disk head : ");
scanf("%d",&head);
printf("\nEnter the number of requests : ");
scanf("%d",&size);
if(size!= 0)
printf("\nEnter the requests - : \n\n");
else
{
printf("No requests to be made ");
return;
}
for(i=1;rear<=size;i++)
{
ins();
mark[i] = 0;
}
fmaxmin();
q[0] = head;
printf("\n\n\t\t\t\tDISK SCHEDULING\n\n");
head = q[0];
seek = 0;
sstf();
getch();
}
void ins()
{
int i;
rear++;
if(rear<=size)
{
printf("Enter the request %d to track no. : ",rear);
scanf("%d",&i);
}
if(i>disk)
{
printf("\nEnter valid track request\n\n");
rear--;
}
else
q[rear] = i;
}
void dis()
{
int i;
for(i=1;i<=size;i++)
printf("%d\t",q[i]);
}
void sstf()
{
int low = disk,j,flag;
for(j=1;j<=size;j++)
{
if(abs(head-q[j]) < low && mark[j] == 0)
{
low = abs(head-q[j]);
flag = j;
}
}
mark[flag] = 1;
printf("\nHead moves from %d to %d...",head,q[flag]);
head = q[flag];
seek += low;
}
void fmaxmin()
{
int i;
for(i = 1;i<=size;i++)
{
if(q[i]>max)
max = q[i];
if(q[i]<min)
min = q[i];
}
}

dinning problem


#include<stdio.h>
#include<semaphore.h>
#include<pthread.h>
 
#define N 5 /*Number of philosphers*/
#define THINKING 0 /*Philospher is thinking*/
#define HUNGRY 1 /*Philospher is trying to get forks*/
#define EATING 2 /*Philospher is eating*/
#define LEFT (ph_num+4)%N /*Number of left neighbor*/
#define RIGHT (ph_num+1)%N /*Number of right neighbor*/
 
sem_t mutex;
sem_t S[N]; /*Array to keep track of everyone's state*/
 
void * philospher(void *num);
void take_fork(int);
void put_fork(int);
void test(int);
 
int state[N];
int phil_num[N]={0,1,2,3,4};
 
int main()
{
    int i;
    pthread_t thread_id[N];
    sem_init(&mutex,0,1);
    for(i=0;i<N;i++)
        sem_init(&S[i],0,0);
    for(i=0;i<N;i++)
    {
        pthread_create(&thread_id[i],NULL,philospher,&phil_num[i]);
        printf("Philosopher %d is thinking\n",i+1);
    }
    for(i=0;i<N;i++)
        pthread_join(thread_id[i],NULL);
}
 
void *philospher(void *num) /* num: philospher's number from 0 to N-1*/
{
    while(1) /*repeat forever*/
    {
        int *i = num;
        sleep(1); /* philospher is thinking*/
        take_fork(*i); /*aquire forks or block*/
        sleep(0); /*Philospher is eating*/
        put_fork(*i); /*put both forks back on table*/
    }
}
 
void take_fork(int ph_num) /* ph_num: philospher's number from 0 to N-1*/
{
    sem_wait(&mutex); /*enter critical section*/
    state[ph_num] = HUNGRY; /* State is philospher is hungry*/
    printf("Philosopher %d is Hungry\n",ph_num+1);
    test(ph_num); /*try to aquire two forks*/
    sem_post(&mutex); /* exit crtical section*/
    sem_wait(&S[ph_num]); /*block if forks were not aquired*/
    sleep(1);
}
 
void test(int ph_num) /* ph_num: philospher's number from 0 to N-1*/
{
    if (state[ph_num] == HUNGRY && state[LEFT] != EATING && state[RIGHT] != EATING)
    {
        state[ph_num] = EATING;
        sleep(2);
        printf("Philosopher %d takes fork %d and %d\n",ph_num+1,LEFT+1,ph_num+1);
        printf("Philosopher %d is Eating\n",ph_num+1);
        sem_post(&S[ph_num]);
    }
}
 
void put_fork(int ph_num) /* ph_num: philospher's number from 0 to N-1*/
{
    sem_wait(&mutex); /*enter critical section*/
    state[ph_num] = THINKING; /*philospher has finished eating*/
    printf("Philosopher %d putting fork %d and %d down\n",ph_num+1,LEFT+1,ph_num+1);
    printf("Philosopher %d is thinking\n",ph_num+1);
    test(LEFT); /*see if left neighbor can now eat*/
    test(RIGHT); /*see if right neighbor can now eat*/
    sem_post(&mutex); /* exit crtical section*/
}

c look disk scheduling


#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
int max,min,disk,mark[100],q[100],rear=0,size,head,seek;
void ins();
void dis();
void clook();
void fmaxmin();
main()
{
int i,ch;
printf("\nEnter the size of the disk (in tracks) : ");
scanf("%d",&disk);
printf("\nEnter the current position of the disk head : ");
scanf("%d",&head);
printf("\nEnter the number of requests : ");
scanf("%d",&size);
if(size!= 0)
printf("\nEnter the requests - : \n\n");
else
{
printf("No requests to be made ");
return;
}
for(i=1;rear<=size;i++)
{
ins();
mark[i] = 0;
}
fmaxmin();
q[0] = head;
printf("\n\n\t\t\t\tDISK SCHEDULING\n\n");
head = q[0];
seek = 0;
clook();
getch();
}
void ins()
{
int i;
rear++;
if(rear<=size)
{
printf("Enter the request %d to track no. : ",rear);
scanf("%d",&i);
}
if(i>disk)
{
printf("\nEnter valid track request\n\n");
rear--;
}
else
q[rear] = i;
}
void dis()
{
int i;
for(i=1;i<=size;i++)
printf("%d\t",q[i]);
}
void clook()
{
int i,j;
printf("\n\n\t\t\t\tCLOOK SCHEDULING\n\n");
printf("\nWORK QUEUE : \n");
dis();
printf("\n\n--------------------------------------------------------------------------------\n");
printf("\nThe sequence is. . .\n\n");
for(j=q[0];j<=max;j++)
{
for(i=1 ;i<=size;i++)
if(j == q[i] && mark[i] == 0)
{
mark[i] = 1;
printf("\nHead moves from %d to %d...",head,q[i]);
seek += abs(head -j);
head = q[i];
}
}
if(head>min)
{
for(j=0;j<=q[0];j++)
{
for(i=1 ;i<=size;i++)
if(j == q[i] && mark[i] == 0)
{
mark[i] = 1;
printf("\nHead moves from %d to %d...",head,q[i]);
seek += abs(head -j);
head = q[i];
}
}
}
printf("\n\nTotal number of head movements is : %d",seek);
}
void fmaxmin()
{
int i;
for(i = 1;i<=size;i++)
{
if(q[i]>max)
max = q[i];
if(q[i]<min)
min = q[i];
}
}

non simple mutex


#include<stdio.h>
#include<string.h>
#include<pthread.h>
#include<stdlib.h>
#include<unistd.h>

pthread_t tid[2];
int counter;

void* doSomeThing(void *arg)
{
    unsigned long i = 0;
    counter += 1;
    printf("\n Job %d started\n", counter);

    for(i=0; i<(0xFFFFFFFF);i++); //this is just wait
    printf("\n Job %d finished\n", counter);

    return NULL;
}

int main(void)
{
    int i = 0;
    int err;

    while(i < 2)
    {
        err = pthread_create(&(tid[i]), NULL, &doSomeThing, NULL);
        if (err != 0)
            printf("\ncan't create thread :[%s]", strerror(err));
        i++;
    }

    pthread_join(tid[0], NULL);
    pthread_join(tid[1], NULL);

    return 0;
}

mutex simple c code


#include<stdio.h>
#include<string.h>
#include<pthread.h>
#include<stdlib.h>
#include<unistd.h>

pthread_t tid[2];
int counter;
pthread_mutex_t lock;

void* doSomeThing(void *arg)
{
    pthread_mutex_lock(&lock);

    unsigned long i = 0;
    counter += 1;
    printf("\n Job %d started\n", counter);

    for(i=0; i<(0xFFFFFFFF);i++); //this is just wait

    printf("\n Job %d finished\n", counter);

    pthread_mutex_unlock(&lock);

    return NULL;
}

int main(void)
{
    int i = 0;
    int err;

    if (pthread_mutex_init(&lock, NULL) != 0)
    {
        printf("\n mutex init failed\n");
        return 1;
    }

    while(i < 2)
    {
        err = pthread_create(&(tid[i]), NULL, &doSomeThing, NULL);
        if (err != 0)
            printf("\ncan't create thread :[%s]", strerror(err));
        i++;
    }

    pthread_join(tid[0], NULL);
    pthread_join(tid[1], NULL);
    pthread_mutex_destroy(&lock);

    return 0;
}

preempt priority scheduling


#include<stdio.h>

struct process
{
int arr_time;
int burst_time;
int no;
int rem_time;
int priority;
};


struct process read(int i)
{
struct process p;
printf("\n\n The process no.:%d.\n",i);
p.no=i;
printf("Enter the arrival time:");
scanf("%d",&p.arr_time);
printf("Enter the burst time:");
scanf("%d",&p.burst_time);
p.rem_time=p.burst_time;
return p;
}



struct process readp(int i)
{
struct process p;
printf("\n\n The process no.:%d.\n",i);
p.no=i;
printf("Enter the arrival time:");
scanf("%d",&p.arr_time);
printf("Enter the burst time:");
scanf("%d",&p.burst_time);
p.rem_time=p.burst_time;
printf("Enter the priority:");
scanf("%d",&p.priority);
return p;
}



void swap(struct process *i, struct process *j)
{
struct process *t;
i=t;
i=j;
j=t;
}


//PRIORITY BASED PREEEMPTIVE  SCHEDULING ALGO.
int main()
{
int  n; //To hold the no. of processes.
struct process p[10],tmp; //To hold the details of the processes.
int i,j;
int ready[10]; //List of ready processes  index
int running; //Running process index
int t; //Time variable
int last,min;
int time;
printf("LOWER NUMBER INDIACTES HIGHER PRIORITY\n");
printf("Enter the number if processes you want to enter:"); //Get the total number of processes from the user

scanf("%d",&n);


for(i=0;i<n;i++)
p[i]=readp(i); //Read the details of the processes


t=0;
last=-1;
min=0;
time=0;

do
{
last=-1;
for(i=0;i<n;i++)
{
if(p[i].arr_time<=time && p[i].rem_time>0)
{
ready[++last]=i; //update the ready queue

}

}

min = ready[0];
if(last<0)
continue;
for(i=0;i<=last;i++)
{
if(p[ready[i]].priority<p[min].priority) //Schedule the process with lowest priority for execution
min=ready[i];
}
running = min;
printf("\n\nTime:%-2d to time: %-2d Running Process: %d",time,time+1,running);
//Print the time for which the particuler process was running
time = time + 1;
p[running].rem_time--; //Update the remaining time




}while(last>=0);
printf("\n");
return 0;
}