Thursday, 21 March 2013

priority.c 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;
}


//SHORTEST PRIORITY 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 INDICATES HIGHER PRIORITY.\n");
printf("Enter the number if processes you want to enter:"); //GEt the number of processws from the usser
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)
min=ready[i];
}
running = min; //Schedule a Process
printf("\n\nTime:%d to time: %d Running Process: %d",time,time+p[running].rem_time,running);//Print the process that was scheduled
time = time + p[running].rem_time; //Update the remaining time of the process that go the CPU
p[running].rem_time=0;



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


srtf.c Shortest right job first


#include<stdio.h>
#include<conio.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;
}


//SHORTEST REMAINING TIME  FIRST 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("Enter the number if processes you want to enter:");
scanf("%d",&n);


for(i=0;i<n;i++)
p[i]=read(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]].rem_time<p[min].rem_time)
min=ready[i];
}
running = min; //Schedule a process and print the process that was scheduled
printf("\n\nTime:%-2d to time: %-2d Running Process: %d",time,time+1,running);

time = time + 1;
p[running].rem_time--; //Update the entry in the PCB




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


SJF - Shortest Job First


#include<stdio.h>
#include<conio.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;
}



//SHORTEST JOB FIRST 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("Enter the number if processes you want to enter:");
scanf("%d",&n);


for(i=0;i<n;i++)
p[i]=read(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]].rem_time<p[min].rem_time)
min=ready[i];
}
running = min; //Schedule a process and print the process that was scheduled
printf("\n\nTime:%d to time: %d Running Process: %d",time,time+p[running].rem_time,running);

time = time + p[running].rem_time;
p[running].rem_time=0; //Remove the process from the ready queue



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


FCFS - First come first serve


/* This program mainly implements the first come first serve(FCFS) scheduling algorithm for concurrent processes P0, P1....Pn*/
#include<stdio.h>
#include<conio.h>

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


struct proc read(int i)
{
struct proc 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 proc readp(int i)
{
struct proc 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 proc *pi, struct proc *pj)
{
struct proc *temp;
temp = pi;
pi = pj;
pj = temp;
}


//FIRST COME FIRST SERVE SCHEDULING ALGO.

int main()
{
int  n; //To hold the no. of processes.
struct proc p[10],tmp; //To hold the details of the processes.
int i,j;
printf("Enter the number if processes you want to enter:");
scanf("%d",&n); //Get the total no. of processes from tje user


for(i=0;i<n;i++)
p[i]=read(i); //Get the details of all the processes


for(i=0;i<n-1;i++)
for(j=0;j<n-1-i;j++) //Create the ready queue and update every time a new process is scheduled
{
if(p[j].arr_time>p[j+1].arr_time)
//
// swap(&p[j],&p[j+1]);
{
tmp=p[j];
p[j]=p[j+1];
p[j+1]=tmp;
}
}
for(i=0;i<n;i++)
printf("%d ",p[i].no);
    getch(); // Print the order in which the processes are getting executed.
return 0;
}


udp server client


#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<string.h>
int main(int argc,char **argv){
            if(argc<2)
            {
                        printf("insuffcient parameters");
                        exit(0);
            }
            struct sockaddr_in servsock;
            int sockfd,size;
            char msg[1024],str[1024];
            if((sockfd=socket(AF_INET,SOCK_DGRAM,0))<0) {
                        perror("");
                        exit(0); }
            printf("socket created\n");
            size=sizeof(struct sockaddr);
            socklen_t len=sizeof(servsock);
            bzero(&servsock,size);
            servsock.sin_port=htons(3000);
            servsock.sin_family=AF_INET;
            servsock.sin_addr.s_addr=inet_addr(argv[1]);
            if(strcmp(msg,"bye")==0)
                        exit(0);
            while(strcmp(msg,"bye")!=0)

                        { printf("\n client: ");
                        scanf("%s",msg);
                        if((sendto(sockfd,msg,sizeof(msg),0,(struct sockaddr*)&servsock,len))<0)
                                    {perror("not send");
                                    exit(0); }
                        if(strcmp(msg,"bye")==0)
                                    exit(0);
                        printf("\n from server: ");
                        if((recvfrom(sockfd,str,1024,0,NULL,NULL))<0)
                        {
                                    perror("not received");
                                    exit(0); }
                        printf("%s",str);
            }
            close(sockfd);
            return 0;
}



Server Side:-


//server program
#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<string.h>
int main(int argc,char **argv)
{
            struct sockaddr_in mysock,newsock;
            int sockfd;
            char buf[1024];
            int size,val;
            if((sockfd=socket(AF_INET,SOCK_DGRAM,0))<0){
                        perror("no socket");
                        exit(0);}
            size=sizeof(struct sockaddr);
            socklen_t len=sizeof(newsock);
            bzero(&mysock,size);
            mysock.sin_family=AF_INET;
            mysock.sin_port=htons(3000);
            mysock.sin_addr.s_addr=htonl(INADDR_ANY);
            if((bind(sockfd,(struct sockaddr*)&mysock,size))<0){
                        perror("no bind");
                        exit(0);}
            if(strcmp(buf,"bye")==0)
                        exit(0);
            while(strcmp(buf,"bye")!=0)
            {
                        if((val=recvfrom(sockfd,buf,1024,0,(struct sockaddr*)&newsock,&len))<0){
                                    perror(" ");
                                    exit(0);}
                        printf("\n from client: ");
                        printf("%s",buf);
                        if(strcmp(buf,"bye")==0){
                                    printf("\nserver: ");
                                    scanf("%s",buf);
                                    exit(0);}
                        printf("\n server: ");
                        scanf("%s",buf);
                        if((sendto(sockfd,buf,val,0,(struct sockaddr*)&newsock,len))<0){
                                    perror("\n no send");
                                    exit(0);}
                        }
            close(sockfd);
            return 0;
}

chat c code


Server :-

#include<stdio.h>
#include<string.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
main()
{
    struct sockaddr_in client,server;
    int s,n;
    char buffer1[100],buffer2[100];
    s=socket(AF_INET,SOCK_DGRAM,0);
    server.sin_family=AF_INET;
    server.sin_port=2000;
    server.sin_addr.s_addr=inet_addr("127.0.0.1");
    bind(s,(struct sockaddr *)&server,sizeof(server));
    printf("\nServer ready,waiting for client....\n");
    n=sizeof(client);
    while(1)
    {
        recvfrom(s,buffer1,sizeof(buffer1),0,(struct sockaddr *) &client,&n);
        if(!(strcmp(buffer1,"end")))
            break;
        printf("\nClient:%s",buffer1);
        printf("\nServer:");
        gets(buffer2);
        sendto(s,buffer2,sizeof(buffer2),0,(struct sockaddr *) &client,n);
             
    }

}

Client

#include<stdio.h>
#include<string.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
main()
{
    struct sockaddr_in client,server;
    int s,n;
    char b1[100],b2[100];
    s=socket(AF_INET,SOCK_DGRAM,0);
    server.sin_family=AF_INET;
    server.sin_port=2000;
    server.sin_addr.s_addr=inet_addr("127.0.0.1");
    printf("\nClient ready....\n");
    n=sizeof(server);
    while(1)
    {
        printf("\nClient:");
        gets(b2);
        sendto(s,b2,sizeof(b2),0,(struct sockaddr *) &server,n);
        if(strcmp(b2,"end")==0)
            break;
        recvfrom(s,b1,sizeof(b1),0,NULL,NULL);
        printf("\nServer:%s",b1);
    }

}

muti threaded broadcast


#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <net/if.h>
#include <ifaddrs.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>

#define kPortNumber 1234
#define kBufferSize 1024
#define kMaxSockets 16



int transmit(char * data, int length) {

  int sock_fds[kMaxSockets];
 
  struct ifaddrs *addrs;
  if ( getifaddrs(&addrs) < 0 ) {
    // Error occurred
    return 0;
  }
 
  // Loop through interfaces, selecting those AF_INET devices that support broadcast, but aren't loopback or point-to-point
  const struct ifaddrs *cursor = addrs;
  struct sockaddr_in addr;
  int number_sockets = 0;
 
  while ( cursor != NULL && number_sockets < kMaxSockets ) {
    if ( cursor->ifa_addr->sa_family == AF_INET
            && !(cursor->ifa_flags & IFF_LOOPBACK)
            && !(cursor->ifa_flags & IFF_POINTOPOINT)
            &&  (cursor->ifa_flags & IFF_BROADCAST) ) {

      // Create socket
      sock_fds[number_sockets] = socket(AF_INET, SOCK_DGRAM, 0);
      if ( sock_fds[number_sockets] == -1 ) {
        // Error occurred
        return 0;
      }
     
      // Create address from which we want to send, and bind it
      memset(&addr, 0, sizeof(addr));
      addr.sin_family = AF_INET;
      addr.sin_addr = ((struct sockaddr_in *)cursor->ifa_addr)->sin_addr;
      addr.sin_port = 0;
     
      if ( bind(sock_fds[number_sockets], (struct sockaddr*)&addr, sizeof(addr)) < 0 ) {
        // Error occurred
        return 0;
      }
     
      // Enable broadcast
      int flag = 1;
      if ( setsockopt(sock_fds[number_sockets], SOL_SOCKET, SO_BROADCAST, &flag, sizeof(flag)) != 0 ) {
        // Error occurred
        return 0;
      }
     
      number_sockets++;
    }
    cursor = cursor->ifa_next;
  }
 
  // Initialise broadcast address
  memset(&addr, 0, sizeof(addr));
  addr.sin_family = AF_INET;
  addr.sin_addr.s_addr = INADDR_BROADCAST;
  addr.sin_port = htons(kPortNumber);
 
  // Send through each interface
  int i;
  for ( i=0; i<number_sockets; i++ ) {
    if ( sendto(sock_fds[i], data, length, 0, (struct sockaddr*)&addr, sizeof(addr)) < 0 ) {
      // Error occurred
      return 0;
    }
  }
 
  return 1;
}

int receive() {
 
  // Create socket
  int sock_fd = socket(AF_INET, SOCK_DGRAM, 0);
  if ( sock_fd == -1 ) {
    // Error occurred
    return 0;
  }
 
  // Create address from which we want to receive, and bind it
  struct sockaddr_in addr;
  memset(&addr, 0, sizeof(addr));
  addr.sin_family = AF_INET;
  addr.sin_addr.s_addr = INADDR_ANY;
  addr.sin_port = htons(kPortNumber);
  if ( bind(sock_fd, (struct sockaddr*)&addr, sizeof(addr)) < 0 ) {
    // Error occurred
    return 0;
  }
 
  char buffer[kBufferSize];
  int addr_len = sizeof(addr);
 
  while ( 1 ) {
   
    // Receive a message, waiting if there's nothing there yet
    int bytes_received = recvfrom(sock_fd, buffer, kBufferSize, 0, (struct sockaddr*)&addr, &addr_len);
    if ( bytes_received < 0 ) {
       // Error occurred
       return 0;
    }
   
    // Now we have bytes_received bytes of data in buffer. Print it!
    fwrite(buffer, sizeof(char), bytes_received, stdout);
    printf("\n");
  } 
}


int main(int argc, char** argv) {
  if ( argc < 2 ) {
    // No argument: Just listen
    printf("Listening...\n");
    if ( !receive() ) {
      printf("Error occurred: %s\n", strerror(errno));
      return 1;
    }
    return 0;
  }

  // Argument provided: Transmit this
  if ( transmit(argv[1], strlen(argv[1])) ) {
    printf("\"%s\" transmitted.\n", argv[1]);
  } else {
    printf("Error occurred: %s\n", strerror(errno));
    return 1;
  }
 
  return 0;
}