Archive for C Code

Combinatory Logic Program

 
Source Code in C

Read the rest of this entry »

Binary Search

 
Source Code in C

Read the rest of this entry »

Linear Search

 
Source Code in C

Read the rest of this entry »

Merge - Sort

A merge sort algorithm used to sort an array o...Image via Wikipedia
Source Code in C

Read the rest of this entry »

Merge 2 Arrays

Word Editor with Trees

Record File

Hashing in C

Carousel in OpenGL

Unix - Processes

Paging - LRU - FIFO - Clock - Belady

Unix - Crontab

#! /bin/sh
# Usage : ./crontabintf

   1:  set -f
   2:  choice=0
   3:  while [ 0 ]
   4:  do 
   5:   
   6:  echo "1.Display crontab jobs."
   7:   
   8:  echo "2.Insert a job."
   9:   
  10:  echo "3.Remove a job."
  11:   
  12:  echo "4.Exit."
  13:   
  14:  echo -n "Select>"
  15:   
  16:  read choice
  17:   
  18:  case $choice in
  19:   
  20:  1) line=1
  21:   
  22:  crontab -l > $HOME/.jobs
  23:   
  24:  res="`wc -l $HOME/.jobs`"
  25:   
  26:  set - $res;lines=$1;
  27:   
  28:  while [ $line -le $lines ] 
  29:   
  30:  do
  31:   
  32:  res="`head -$line $HOME/.jobs | tail -1`"
  33:   
  34:  set - $res;disp=" ";
  35:   
  36:  for arg
  37:   
  38:  do
  39:   
  40:  if [ "$arg" = '*' ]
  41:   
  42:  then 
  43:   
  44:  disp="$disp Any"
  45:   
  46:  else
  47:   
  48:  disp="$disp $arg"
  49:   
  50:  fi
  51:   
  52:  done 
  53:   
  54:  set - $disp
  55:   
  56:  echo "JOB $line–>"
  57:   
  58:  echo "minute: $1";shift;
  59:   
  60:  echo "Hour: $1";shift;
  61:   
  62:  echo "Date: $1/$2";shift;shift;
  63:   
  64:  echo "Day of week: $1";shift;
  65:   
  66:  echo "Command:$*"
  67:   
  68:  line=`expr $line + 1`
  69:   
  70:  done;;
  71:   
  72:  2) crontab -l >$HOME/.newjobs
  73:   
  74:  selection=0
  75:   
  76:  minutes="any"
  77:   
  78:  hours="any"
  79:   
  80:  monthday="any"
  81:   
  82:  month="any"
  83:   
  84:  weekday="any"
  85:   
  86:  while [ $selection -ne 7 ]
  87:   
  88:  do
  89:   
  90:  echo "1.Set the command(-obligatory):"
  91:   
  92:  echo "Set periodicity of command."
  93:   
  94:  echo "2.Set minutes:0-59"
  95:   
  96:  echo "3.Set hours:0-23"
  97:   
  98:  echo "4.Set day of month:1-31"
  99:   
 100:  echo "5.Set month:1-12"
 101:   
 102:  echo "6.Set day of week:Mon-Sun"
 103:   
 104:  echo "7.Ready."
 105:   
 106:  echo -n "Enter>"
 107:   
 108:  read selection 
 109:   
 110:  case $selection in
 111:   
 112:  1)echo -n "Enter>"
 113:   
 114:  read command;;
 115:   
 116:  2) echo -n "If more than one choice,seperate by comma..Enter>"
 117:   
 118:  read minutes;;
 119:   
 120:  3) echo -n "If more than one choice,seperate by comma..Enter>"
 121:   
 122:  read hours;;
 123:   
 124:  4) echo -n "If more than one choice,seperate by comma..Enter>"
 125:   
 126:  read monthday;;
 127:   
 128:  5) echo -n "If more than one choice,seperate by comma..Enter>"
 129:   
 130:  read month;;
 131:   
 132:  6) echo -n "If more than one choice,seperate by comma..Enter>"
 133:   
 134:  read weekday;;
 135:   
 136:  7) break;;
 137:   
 138:  *) continue;;
 139:   
 140:  esac
 141:   
 142:  done
 143:   
 144:  periodicity=" "
 145:   
 146:  for inst in $minutes $hours $monthday $month $weekday
 147:   
 148:  do
 149:   
 150:  if [ "$inst" = "any" ] 
 151:   
 152:  then
 153:   
 154:  periodicity="$periodicity *"
 155:   
 156:  else
 157:   
 158:  periodicity="$periodicity $inst"
 159:   
 160:  fi
 161:   
 162:  done
 163:   
 164:  echo "$periodicity $command">>$HOME/.newjobs
 165:   
 166:  crontab $HOME/.newjobs;;
 167:   
 168:  3) crontab -l >$HOME/.jobs
 169:   
 170:  echo "Job to remove"
 171:   
 172:  echo -n "Enter Job’s number as listed above>"
 173:   
 174:  read jobnmb
 175:   
 176:  res="`wc -l $HOME/.jobs`"
 177:   
 178:  set - $res;
 179:   
 180:  rest=`expr $1 - $jobnmb`
 181:   
 182:  jobnmb=`expr $jobnmb - 1`
 183:   
 184:  head -$jobnmb $HOME/.jobs | cat >$HOME/.njobs
 185:   
 186:  tail -$rest $HOME/.jobs | cat >>$HOME/.njobs
 187:   
 188:  crontab $HOME/.njobs;;
 189:   
 190:  4) rm $HOME/.jobs $HOME/.newjobs
 191:   
 192:  exit;;
 193:   
 194:  *) continue;;
 195:   
 196:  esac
 197:   
 198:  done

Distributed Algorithms – Ring Orientation

C Code

File Main.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include “list.c”

void main()
{
        int i,megethos,id,num_arx,*arx,*temp,j,vrethike,k,messages,p,q,random_num,allagi;
        char state[5];
        int **link;
        typos_deikti prodeiktis,arxi;
        typos_deikti *riza;

        printf(”Doste to megethos tou daktiliou:\n”);
        scanf(”%d”,&megethos);

        riza=malloc(megethos*sizeof(typos_deikti));

        //—————-Desmeusi pinaka 2-diastaseon gia ta links
        link = (int **) malloc(megethos*sizeof(int));
        for(i=0;i < megethos;i++)
        {
            link[i] = malloc(megethos*sizeof(int));
        }
        //——————-Arxikopoiisi tou pinaka ton links me 0
        //—————— link[p][q] = epomenos = 1
        //——————-link[p][q] = proigoumenos = 0
        for(i=0;i < megethos;i++)
        {
            for(j=0;j < megethos;j++)
            {
                link[i][j] = 0;
            }
        }

        dimiourgia(riza);
        prodeiktis=NULL;

        sprintf(state,”recei”);
        id = 1;
        eisagogi(riza,state,id,prodeiktis);
        prodeiktis = *riza;

        for(i=0;i<megethos-1;i++)
        {
            id++;
            eisagogi(riza,state,id,prodeiktis);
            proxorise(&prodeiktis);
        }

        printf(”\nEpilextikan tixaia oi parakato komvoi senders:\n”);
        num_arx=fmod(rand(),megethos);
        arx=malloc(num_arx*sizeof(int));
        temp=malloc(num_arx*sizeof(int));
        for(i=0;i<num_arx;i++)
        {
            arx[i] = fmod(rand(),megethos);
            for(j=0;j<num_arx;j++)
            {
                if(temp[j]==arx[i])
                {
                    vrethike=1;
                    break;
                }
                else
                {
                        vrethike=0;
                }
            }

            if(vrethike==0)
            {
                temp[i] = arx[i];
                printf(”%d\n”,arx[i]);

                prodeiktis = *riza;
                for(k=0;k<arx[i];k++)
                {
                    proxorise(&prodeiktis);   
                }
                sprintf(prodeiktis->state,”sende”);

                //—————-Enimerosi ton links gia tin kateuthnisi ton senders——————//
                if(arx[i]!=0 && arx[i]!=megethos-1)
                {
                    p=arx[i];
                    q=arx[i]+1;
                    random_num = fmod(rand(),2);
                    if (random_num ==0)
                    {
                        link[p][q] = 1;
                    }
                    else
                    {
                        link[p][q-2] = 1;
                    }
                }
                else if(arx[i]==0)
                {
                    p=0;
                    q=1;
                    link[p][q] = 1;
                }
                else
                {
                    p=megethos-1;
                    q=0;
                    link[p][q] = 1;
                }

            }
            else
            {
                i–;
            }
        }

        //————–SET SUCC-PRED TO RECEIVERS————//
        prodeiktis = *riza;
        for(i=0;i<megethos;i++)
        {
            if(strcmp(prodeiktis->state,”recei”)==0)
            {
                //Receiver = Node 0
                if(i==0)
                {
                    link[i][1]=1;
                    link[i][megethos-1]=0;   
                }
                //Receiver = Last Node
                if(i==megethos-1)
                {
                    link[i][0]=1;
                    link[i][i-1]=0;   
                }
                else
                {
                    link[i][i+1]=1;
                    link[i][i-1]=0;
                }
            }
            proxorise(&prodeiktis);
        }

        //———Tipose arxiki katastasi———-//
        printf(”Katastasi Komvon Daktiliou\n”);
        printf(”————————–\n”);
        prodeiktis = *riza;
        for(i=0;i<megethos;i++)
        {
            printf(”Komvos %d : “,i);
            printf(”ID = %d “,node_id(prodeiktis));
            printf(”State = %s\n”,periexomeno(prodeiktis));
            proxorise(&prodeiktis);
        }

        //———-Tipose ta links—————–//
        printf(”Pinakas Zeukseon Succ kai Pred metaksi komvon\n”);
        printf(”———————————————\n”);
        for(i=0;i < megethos;i++)
        {
            for(j=0;j < megethos;j++)
            {
                printf(”%d “,link[i][j]);
            }
            printf(”\n”);
        }
        printf(”Kateuthinseis zeukseon metaksi komvon\n”);
        printf(”————————————-\n”);
        for(i=0;i < megethos;i++)
        {
            for(j=0;j < megethos;j++)
            {
                if(link[i][j] == 1)
                {
                    printf(”%d->%d\n”,i,j);
                }
            }
        }

        //———–Ring Orientation—————-//
        allagi =1;
        messages=0;
        prodeiktis = *riza;
        while(allagi==1)
        {
            allagi=0;

            for(i=0;i<megethos-2;i++)
            {
                ////////Kainouria fasi - allagi katastaseon////////

                if (prodeiktis->temp == 1)
                {
                    sprintf(prodeiktis->state,”sende”);
                }
                if (prodeiktis->temp == 2)
                {
                    sprintf(prodeiktis->state,”recei”);
                }
                if (prodeiktis->temp == 3)
                {
                    sprintf(prodeiktis->state,”inter”);
                }

                ///////////////
                if((strcmp(prodeiktis->state,”sende”)==0) && (strcmp(prodeiktis->epomenos->state,”inter”)==0) && link[i][i+1]==1)
                {
                    prodeiktis->epomenos->temp = 2;
                    link[i+1][i+2]=1;
                    link[i+1][i]=0;
                    allagi=1;
                }
                if((strcmp(prodeiktis->state,”sende”)==0) && (strcmp(prodeiktis->epomenos->state,”recei”)==0) && (link[i][i+1]==1) && (link[i+1][i]==0))
                {
                    prodeiktis->temp = 3;
                    prodeiktis->epomenos->temp =1;
                    allagi=1;
                }
                if((strcmp(prodeiktis->state,”sende”)==0) && (strcmp(prodeiktis->epomenos->state,”sende”)==0) && (link[i][i+1]==1) && (link[i+1][i]==1))
                {
                    prodeiktis->epomenos->temp =2;
                    link[i+1][i+2]=1;
                    link[i+1][i]=0;
                    allagi=1;
                }
                if((strcmp(prodeiktis->state,”inter”)==0) && (strcmp(prodeiktis->epomenos->state,”inter”)==0) && (link[i][i+1]==1) && (link[i+1][i]==1))
                {
                    prodeiktis->temp =1;
                    allagi=1;
                }
                proxorise(&prodeiktis);
            }

            //——————-Kikliki Lista -> Proteleutaios komvos——————//
            if (prodeiktis->temp == 1)
            {
                sprintf(prodeiktis->state,”sende”);
            }
            if (prodeiktis->temp == 2)
            {
                sprintf(prodeiktis->state,”recei”);
            }
            if (prodeiktis->temp == 3)
            {
                sprintf(prodeiktis->state,”inter”);
            }
            if((strcmp(prodeiktis->state,”sende”)==0) && (strcmp(prodeiktis->epomenos->state,”inter”)==0) && link[i][i+1]==1)
                {
                    prodeiktis->epomenos->temp = 2;
                    link[i+1][0]=1; //——-
                    link[i+1][i]=0;
                    allagi=1;
                }
                if((strcmp(prodeiktis->state,”sende”)==0) && (strcmp(prodeiktis->epomenos->state,”recei”)==0) && (link[i][i+1]==1) && (link[i+1][i]==0))
                {
                    prodeiktis->temp = 3;
                    prodeiktis->epomenos->temp =1;
                    allagi=1;
                }
                if((strcmp(prodeiktis->state,”sende”)==0) && (strcmp(prodeiktis->epomenos->state,”sende”)==0) && (link[i][i+1]==1) && (link[i+1][i]==1))
                {
                    prodeiktis->epomenos->temp =2;
                    link[i+1][0]=1; //——
                    link[i+1][i]=0;
                    allagi=1;
                }
                if((strcmp(prodeiktis->state,”inter”)==0) && (strcmp(prodeiktis->epomenos->state,”inter”)==0) && (link[i][i+1]==1) && (link[i+1][i]==1))
                {
                    prodeiktis->temp =1;
                    allagi=1;
                }
                proxorise(&prodeiktis);

                //——————-Kikliki Lista -> Teleutaios komvos——————//
                arxi = *riza;

                if (prodeiktis->temp == 1)
            {
                sprintf(prodeiktis->state,”sende”);
            }
            if (prodeiktis->temp == 2)
            {
                sprintf(prodeiktis->state,”recei”);
            }
            if (prodeiktis->temp == 3)
            {
                sprintf(prodeiktis->state,”inter”);
            }
            if((strcmp(prodeiktis->state,”sende”)==0) && (strcmp(arxi->state,”inter”)==0) && link[i][0]==1)
                {
                    arxi->temp = 2;
                    link[0][1]=1;
                    link[0][i]=0;
                    allagi=1;
                }
                if((strcmp(prodeiktis->state,”sende”)==0) && (strcmp(arxi->state,”recei”)==0) && (link[i][0]==1) && (link[0][i]==0))
                {
                    prodeiktis->temp = 3;
                    arxi->temp =1;
                    allagi=1;
                }
                if((strcmp(prodeiktis->state,”sende”)==0) && (strcmp(arxi->state,”sende”)==0) && (link[i][0]==1) && (link[0][i]==1))
                {
                    arxi->temp =2;
                    link[0][1]=1;
                    link[0][i]=0;
                    allagi=1;
                }
                if((strcmp(prodeiktis->state,”inter”)==0) && (strcmp(arxi->state,”inter”)==0) && (link[i][0]==1) && (link[0][i]==1))
                {
                    prodeiktis->temp =1;
                    allagi=1;
                }
                prodeiktis = *riza;

        }

        //////////////////APOTELESMATA////////////////////////////
        printf(”\n\n****************Apotelesmata*****************\n\n”);
        printf(”Katastasi Komvon Daktiliou\n”);
        printf(”————————–\n”);
        prodeiktis = *riza;
        for(i=0;i<megethos;i++)
        {
            printf(”Komvos %d : “,i);
            printf(”ID = %d “,node_id(prodeiktis));
            printf(”State = %s\n”,periexomeno(prodeiktis));
            proxorise(&prodeiktis);
        }

        /////////Tipose ta links//////////////
        printf(”Pinakas Zeukseon Succ kai Pred metaksi komvon\n”);
        printf(”———————————————\n”);
        for(i=0;i < megethos;i++)
        {
            for(j=0;j < megethos;j++)
            {
                printf(”%d “,link[i][j]);
            }
            printf(”\n”);
        }
        printf(”Kateuthinseis zeukseon metaksi komvon\n”);
        printf(”————————————-\n”);
        for(i=0;i < megethos;i++)
        {
            for(j=0;j < megethos;j++)
            {
                if(link[i][j] == 1)
                {
                    printf(”%d->%d\n”,i,j);
                }
            }
        }
        //////////////////APOTELESMATA////////////////////////////
}

File List.c

/*************************************************************************
Αρχείο Υλoπoίησης    : list.c
Σκοπός                : Υλοποίηση με δείκτες ΑΤΔ, κυκλικά Συνδεδεμένη Λίστα
**************************************************************************/
#include <stdio.h>
#include <stdlib.h>

typedef char   typos_stoixeiou;
typedef struct typos_komvou *typos_deikti;
typedef struct typos_komvou
{
    typos_stoixeiou state[5];
    typos_deikti epomenos;
    int id;
    int tok;
    int temp;
};
typos_deikti lista;

void dimiourgia(typos_deikti *lista);
int keni(typos_deikti lista);
void proxorise(typos_deikti *p);
typos_stoixeiou *periexomeno(typos_deikti p);
void diagrafi(typos_deikti *lista, typos_deikti prodeiktis);

void eisagogi(typos_deikti *lista,typos_stoixeiou *stoixeio,int id,typos_deikti prodeiktis);
void eisagogi_arxi(typos_deikti *lista,typos_stoixeiou *stoixeio,int id);
void eisagogi_meta(typos_deikti prodeiktis,typos_stoixeiou *stoixeio,int id);

int node_id(typos_deikti p);
int send(int tok,typos_deikti p);

void dimiourgia(typos_deikti *lista)
{
/*   
*    Πρo:         καμία
*    Μέτά:         Δημιoυργία κενής συνδεδεμένης λίστα
*/   
    *lista = NULL;
}

int keni(typos_deikti lista)
{
/*   
*    Πρo:         Δημιoυργία λίστας
*    Μέτά:        επιστρέφει 1 αν η λίστα είναι κενή, διαφoρετικά 0
*/
    return ( lista == NULL );
}

void proxorise(typos_deikti *p)
{
/*   
*    Πρo:         Ο δείκτης p δείχνει ένα κόμβο στην λίστα
*    Μέτά:        Ο δείκτης p δείχνει στον επόμενο κόμβο στην λίστα
*/
    *p = (*p)->epomenos;
}

typos_stoixeiou *periexomeno(typos_deikti p)
{
/*   
*    Πρo:         Ο δείκτης p δείχνει ένα κόμβο στην λίστα
*    Μέτά:        επιστρέφει τα δεδομένα στον κόμβο που δείχνει ο p
*/
    return (p->state);
}

int node_id(typos_deikti p)
{
/*   
*    Πρo:         Ο δείκτης p δείχνει ένα κόμβο στην λίστα
*    Μέτά:        επιστρέφει τα δεδομένα στον κόμβο που δείχνει ο p
*/
    return (p->id);
}

int send(int tok,typos_deikti p)
{
/*   
*    Πρo:         Ο δείκτης p δείχνει ένα κόμβο στην λίστα
*    Μέτά:        επιστρέφει τα δεδομένα στον κόμβο που δείχνει ο p
*/
    if(p->id == tok)
    {
        sprintf(p->state,”leade”);
        return 1;
    }
    p->temp = tok;

    return 0;

}

void eisagogi_meta(typos_deikti prodeiktis,typos_stoixeiou *stoixeio,int id)
{
/*   
*    Πρo:         Δημιoυργία λίστα
*    Μέτά:         Ο κόμβος με τα δεδομένα stoixeio έχει εισαχθεί
*                μετά τον κόμβο που δείχνει ο prodeiktis
*/    int i;
    typedef struct
    {
        typos_stoixeiou state[5];
        typos_deikti epomenos;
        int id;
        int tok;
        int temp;
    }typos_komvou;
    typos_deikti prosorinos; /*Δείχνει τον νέο κόμβο που πρόκειτε να εισαχθεί*/

    prosorinos = (typos_deikti)malloc(sizeof(typos_komvou));
    if ( prosorinos == NULL )
    {
        fprintf(stderr,”Η μνήμη είναι γεμάτη\n”);
        exit(-1);
    }
    for(i=0;i<5;i++)
        prosorinos->state[i] = stoixeio[i];
    prosorinos->state[i] = ‘\0′;
    prosorinos->id = id;
    prosorinos->tok = id;
    prosorinos->temp = 0;
    prosorinos->epomenos = prodeiktis->epomenos;
    prodeiktis->epomenos = prosorinos;
}

void eisagogi_arxi(typos_deikti *lista,typos_stoixeiou *stoixeio,int id)
{
/*   
*    Πρo:         O δείκτης p δείχνει ττην αρχή της λίστας
*    Μέτά:         Ο κόμβος με τα δεδομένα stoixeio έχει εισαχθεί
*                πριν τον κόμβο που εδείχνει ο p, ο p δείχνει πλέον την νέα αρχή της λίστας
*/
    int i;
    typedef struct
    {
        typos_stoixeiou state[5];
        typos_deikti epomenos;
        int id;
        int tok;
        int temp;
    }typos_komvou;
    typos_deikti prosorinos; /*Δείχνει τον νέο κόμβο που πρόκειτε να εισαχθεί*/

    prosorinos = (typos_deikti)malloc(sizeof(typos_komvou));
    if ( prosorinos == NULL )
    {
        fprintf(stderr,”Η μνήμη είναι γεμάτη\n”);
        exit(-1);
    }
    for(i=0;i<5;i++)
        prosorinos->state[i] = stoixeio[i];
    prosorinos->state[i] = ‘\0′;
    prosorinos->id = id;
    prosorinos->tok = id;
    prosorinos->temp = 0;
    prosorinos->epomenos = *lista;
    *lista = prosorinos;
}

void eisagogi(typos_deikti *lista,typos_stoixeiou *stoixeio,int id,typos_deikti prodeiktis)
{
/*   
*    Πρo:         Ο prodeiktis δείχνει ένα κόμβο στην λίστα ή είναι NULL
*    Μέτά:         Αν ο prodeiktis είναι NULL τοτε ο κόμβος με τα δεδομένα
*                stoixeio έχει εισαχθεί στην αρχή της λίστας αλλιώς ο εισάγεται μετά
*                τον κόμβο που δείχνει ο prodeiktis
*/
    if (keni(prodeiktis)) /*εισαγωγή στην αρχή της λίστας*/
        eisagogi_arxi(lista,stoixeio,id);
    else
        eisagogi_meta(prodeiktis,stoixeio,id);
}

Distributed Algorithms, Chang & Roberts


C Code


Main.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include “list.c”

void main()
{
        int i,megethos,id,num_arx,*arx,*temp,j,vrethike,k,messages;
        char state[5];
        typos_deikti prodeiktis;
        typos_deikti *riza;

        printf(”Doste to megethos tou daktiliou:\n”);
        scanf(”%d”,&megethos);

        riza=malloc(megethos*sizeof(typos_deikti));
        dimiourgia(riza);
        prodeiktis=NULL;

        sprintf(state,”sleep”);
        id = 1;
        eisagogi(riza,state,id,prodeiktis);
        prodeiktis = *riza;

        for(i=0;i<megethos-1;i++)
        {
            id++;
            //id=id*rand();
            eisagogi(riza,state,id,prodeiktis);
            proxorise(&prodeiktis);
        }

        prodeiktis = *riza;
        for(i=0;i<megethos;i++)
        {
            printf(”Komvos %d : “,i);
            printf(”ID = %d “,node_id(prodeiktis));
            printf(”State = %s\n”,periexomeno(prodeiktis));
            proxorise(&prodeiktis);
        }

        printf(”\nEpilextikan tixaia oi parakato komvoi arxikopoiites:\n”);
        num_arx=fmod(rand(),megethos);
        arx=malloc(num_arx*sizeof(int));
        temp=malloc(num_arx*sizeof(int));
        for(i=0;i<num_arx;i++)
        {
            arx[i] = fmod(rand(),megethos);
            for(j=0;j<num_arx;j++)
            {
                if(temp[j]==arx[i])
                {
                    vrethike=1;
                    break;
                }
                else
                {
                        vrethike=0;
                }
            }

            if(vrethike==0)
            {
                temp[i] = arx[i];
                printf(”%d\n”,arx[i]);

                prodeiktis = *riza;
                for(k=0;k<arx[i];k++)
                {
                    proxorise(&prodeiktis);   
                }
                sprintf(prodeiktis->state,”candi”);
            }
            else
            {
                i–;
            }
        }

        ////////////////TEST - NODE CANDIDATES//////////
        /*prodeiktis = *riza;
                for(i=0;i<megethos;i++)
                {
                    sprintf(prodeiktis->state,”sleep”);
                    proxorise(&prodeiktis);   
                }

        prodeiktis = *riza;   
        for(i=0;i<3;i++)
                {
                    sprintf(prodeiktis->state,”candi”);
                    proxorise(&prodeiktis);
                }*/
        ///////////////////////////////////////////

        printf(”\n”);
        //////////////////DEBUG////////////////////////////
        prodeiktis = *riza;
        for(i=0;i<megethos;i++)
        {
            printf(”Komvos %d : “,i);
            printf(”ID = %d “,node_id(prodeiktis));
            printf(”State = %s\n”,periexomeno(prodeiktis));
            proxorise(&prodeiktis);
        }
        //////////////////DEBUG////////////////////////////
        vrethike =0;
        messages=0;
        prodeiktis = *riza;
        while(vrethike==0)
        {
            for(i=0;i<megethos-1;i++)
            {
                //ean den einai arxikopoiitis apla proothei to minima
                if(strcmp(prodeiktis->state,”sleep”)==0)
                {
                    if(prodeiktis->temp!=prodeiktis->tok)
                    {
                        vrethike = send(prodeiktis->temp ,prodeiktis->epomenos);
                        messages++;
                    }
                }
                //ean einai arxikopoitis
                if(strcmp(prodeiktis->state,”candi”)==0)
                {
                    vrethike = send(prodeiktis->tok ,prodeiktis->epomenos);   
                    messages++;
                    if(prodeiktis->temp>prodeiktis->tok)
                    {
                        prodeiktis->tok = prodeiktis->temp;
                    }                   
                }
                if(vrethike==1)
                {
                    break;
                }
                proxorise(&prodeiktis);
            }

            if(vrethike==1)
            {
                break;
            }

            //////Kikliki Lista -> o teleutaios komvos send sti riza////////

            //ean den einai arxikopoiitis apla proothei to minima
            if(strcmp(prodeiktis->state,”sleep”)==0)
            {
                if(prodeiktis->temp!=prodeiktis->tok)
                {
                    vrethike = send(prodeiktis->temp ,*riza);
                    messages++;
                }
            }
            //ean einai arxikopoitis
            if(strcmp(prodeiktis->state,”candi”)==0)
            {
                vrethike = send(prodeiktis->tok ,*riza);
                messages++;
                if(prodeiktis->temp>prodeiktis->tok)
                {
                        prodeiktis->tok = prodeiktis->temp;
                }
            }

            if(vrethike==1)
            {
                break;
            }

            ///Epistrofi sti riza - ksekinaei o epomenos kiklos me tis nees times
            prodeiktis = *riza;
            if(strcmp(prodeiktis->state,”candi”)==0)
            {
                if(prodeiktis->temp>prodeiktis->tok)
                {
                    prodeiktis->tok = prodeiktis->temp;
                }
            }
        }

        printf(”\n”);
        //////////////////APOTELESMATA////////////////////////////
        prodeiktis = *riza;
        for(i=0;i<megethos;i++)
        {
            if(strcmp(prodeiktis->state,”leade”)==0)
            {
                printf(”Komvos %d : “,i);
                printf(”ID = %d “,node_id(prodeiktis));
                printf(”State = %s\n”,periexomeno(prodeiktis));   
            }
            proxorise(&prodeiktis);
        }

        printf(”Arithmos Minimaton = %d\n\n”,messages);
        //////////////////APOTELESMATA////////////////////////////
}

List.c

/*************************************************************************
Αρχείο Υλoπoίησης    : list.c
Σκοπός                : Υλοποίηση με δείκτες ΑΤΔ, κυκλικά Συνδεδεμένη Λίστα
**************************************************************************/
#include <stdio.h>
#include <stdlib.h>

typedef char   typos_stoixeiou;
typedef struct typos_komvou *typos_deikti;
typedef struct typos_komvou
{
    typos_stoixeiou state[5];
    typos_deikti epomenos;
    int id;
    int tok;
    int temp;
};
typos_deikti lista;

void dimiourgia(typos_deikti *lista);
int keni(typos_deikti lista);
void proxorise(typos_deikti *p);
typos_stoixeiou *periexomeno(typos_deikti p);
void diagrafi(typos_deikti *lista, typos_deikti prodeiktis);

void eisagogi(typos_deikti *lista,typos_stoixeiou *stoixeio,int id,typos_deikti prodeiktis);
void eisagogi_arxi(typos_deikti *lista,typos_stoixeiou *stoixeio,int id);
void eisagogi_meta(typos_deikti prodeiktis,typos_stoixeiou *stoixeio,int id);

int node_id(typos_deikti p);
int send(int tok,typos_deikti p);

void dimiourgia(typos_deikti *lista)
{
/*   
*    Πρo:         καμία
*    Μέτά:         Δημιoυργία κενής συνδεδεμένης λίστα
*/   
    *lista = NULL;
}

int keni(typos_deikti lista)
{
/*   
*    Πρo:         Δημιoυργία λίστας
*    Μέτά:        επιστρέφει 1 αν η λίστα είναι κενή, διαφoρετικά 0
*/
    return ( lista == NULL );
}

void proxorise(typos_deikti *p)
{
/*   
*    Πρo:         Ο δείκτης p δείχνει ένα κόμβο στην λίστα
*    Μέτά:        Ο δείκτης p δείχνει στον επόμενο κόμβο στην λίστα
*/
    *p = (*p)->epomenos;
}

typos_stoixeiou *periexomeno(typos_deikti p)
{
/*   
*    Πρo:         Ο δείκτης p δείχνει ένα κόμβο στην λίστα
*    Μέτά:        επιστρέφει τα δεδομένα στον κόμβο που δείχνει ο p
*/
    return (p->state);
}

int node_id(typos_deikti p)
{
/*   
*    Πρo:         Ο δείκτης p δείχνει ένα κόμβο στην λίστα
*    Μέτά:        επιστρέφει τα δεδομένα στον κόμβο που δείχνει ο p
*/
    return (p->id);
}

int send(int tok,typos_deikti p)
{
/*   
*    Πρo:         Ο δείκτης p δείχνει ένα κόμβο στην λίστα
*    Μέτά:        επιστρέφει τα δεδομένα στον κόμβο που δείχνει ο p
*/
    if(p->id == tok)
    {
        sprintf(p->state,”leade”);
        return 1;
    }
    p->temp = tok;

    return 0;

}

void eisagogi_meta(typos_deikti prodeiktis,typos_stoixeiou *stoixeio,int id)
{
/*   
*    Πρo:         Δημιoυργία λίστα
*    Μέτά:         Ο κόμβος με τα δεδομένα stoixeio έχει εισαχθεί
*                μετά τον κόμβο που δείχνει ο prodeiktis
*/    int i;
    typedef struct
    {
        typos_stoixeiou state[5];
        typos_deikti epomenos;
        int id;
        int tok;
        int temp;
    }typos_komvou;
    typos_deikti prosorinos; /*Δείχνει τον νέο κόμβο που πρόκειτε να εισαχθεί*/

    prosorinos = (typos_deikti)malloc(sizeof(typos_komvou));
    if ( prosorinos == NULL )
    {
        fprintf(stderr,”Η μνήμη είναι γεμάτη\n”);
        exit(-1);
    }
    for(i=0;i<5;i++)
        prosorinos->state[i] = stoixeio[i];
    prosorinos->state[i] = ‘\0′;
    prosorinos->id = id;
    prosorinos->tok = id;
    prosorinos->temp = id;
    prosorinos->epomenos = prodeiktis->epomenos;
    prodeiktis->epomenos = prosorinos;
}

void eisagogi_arxi(typos_deikti *lista,typos_stoixeiou *stoixeio,int id)
{
/*   
*    Πρo:         O δείκτης p δείχνει ττην αρχή της λίστας
*    Μέτά:         Ο κόμβος με τα δεδομένα stoixeio έχει εισαχθεί
*                πριν τον κόμβο που εδείχνει ο p, ο p δείχνει πλέον την νέα αρχή της λίστας
*/
    int i;
    typedef struct
    {
        typos_stoixeiou state[5];
        typos_deikti epomenos;
        int id;
        int tok;
        int temp;
    }typos_komvou;
    typos_deikti prosorinos; /*Δείχνει τον νέο κόμβο που πρόκειτε να εισαχθεί*/

    prosorinos = (typos_deikti)malloc(sizeof(typos_komvou));
    if ( prosorinos == NULL )
    {
        fprintf(stderr,”Η μνήμη είναι γεμάτη\n”);
        exit(-1);
    }
    for(i=0;i<5;i++)
        prosorinos->state[i] = stoixeio[i];
    prosorinos->state[i] = ‘\0′;
    prosorinos->id = id;
    prosorinos->tok = id;
    prosorinos->temp = id;
    prosorinos->epomenos = *lista;
    *lista = prosorinos;
}

void eisagogi(typos_deikti *lista,typos_stoixeiou *stoixeio,int id,typos_deikti prodeiktis)
{
/*   
*    Πρo:         Ο prodeiktis δείχνει ένα κόμβο στην λίστα ή είναι NULL
*    Μέτά:         Αν ο prodeiktis είναι NULL τοτε ο κόμβος με τα δεδομένα
*                stoixeio έχει εισαχθεί στην αρχή της λίστας αλλιώς ο εισάγεται μετά
*                τον κόμβο που δείχνει ο prodeiktis
*/
    if (keni(prodeiktis)) /*εισαγωγή στην αρχή της λίστας*/
        eisagogi_arxi(lista,stoixeio,id);
    else
        eisagogi_meta(prodeiktis,stoixeio,id);
}

TCP Server - Client

C Code

TCP - Server

defs.h

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define SERV_TCP_PORT 6000

#define SERV_HOST_ADDR “127.0.0.1″

char *pname;

utils.c

#include “defs.h”

#define MAXLINE 512

/* Read n bytes from a descriptor. Use it in place of read() when fd is a stream socket. */
int readn(fd, ptr, nbytes)
register int fd;
register char *ptr;
register int nbytes;
{
    int nleft, nread;
     nleft=nbytes;

     while (nleft>0)
    {
        nread=read(fd, ptr, nleft);

        if (nread<0) return(nread); /* error */
        else if (nread==0) break; /* EOF */
         nleft -=nread; ptr+=nread;
     }
return(nbytes-nleft);
}

/* Write n bytes from a descriptor. Use it in place of write() when fd is a stream socket. */
int writen(fd, ptr, nbytes)
register int fd;
register char *ptr;
register int nbytes;
{
    int nleft, nwritten;
     nleft=nbytes;
     while (nleft>0)
    {
        nwritten=write(fd,ptr, nleft);
        if (nwritten<=0)
        return(nwritten); /* error */
        nleft -=nwritten;
        ptr+=nwritten;
    }

return(nbytes-nleft);
}

/* Read a line from a descriptor. Read the line one byte at a time,
looking for the newline. We store the newline in the buffer, then
follow it with a null. We return the number of characters up to,
but not including, the null. */

int readline(fd, ptr, maxlen)
register int fd;
register char *ptr;
register int maxlen;

{
    int n,rc;
     char c;
     for (n=1; n<maxlen;n++)
     {
         if ((rc=read(fd,&c,1))==1)
        {
             *ptr++=c;
            if(c==’\n’) break;
        }
         else if (rc==0)
         {
             if (n==1) return(0); /* EOF no data read */
            else break; /* EOF, some data was read */
         }
         else return(-1); /* error */
    }
    *ptr=0;

    return(n);
}

/* Read a stream socket one line at a time and write each line back to the sender.
Return when the connection is terminated. */
str_echo(sockfd) int sockfd;
{
     int n; char line[MAXLINE];
     for ( ; ; )
    {
         n = readline(sockfd, line, MAXLINE);

        if (n==0) return; /* connection terminated */
         else if (n < 0) printf(”str_echo: readline error\n”);
         if (writen(sockfd, line, n) != n)
             printf(”str_echo: writen error\n”);
    }
}

/* read the contents of the FILE *fp, write each line to the stream socket (to the server process),
then read a line back from the socket and write it to the standard output.
Return to caller when an EOF is encountered on the input file. */

str_cli(fp, sockfd)
register FILE *fp;
register int sockfd;
{

    int n;
    char sendline[MAXLINE], recvline[MAXLINE+1];

    while(fgets(sendline, MAXLINE, fp) !=NULL)
    {
        n=strlen(sendline);
        if (writen(sockfd,sendline,n)!=n)
            printf(”str_cli: writen error on socket\n”);
        /* Now read a line from the socket and write it to our standard output */
        n=readline(sockfd,recvline,MAXLINE);
        if (n<0) printf(”str_cli: readline error\n”); recvline[n]=0; /* null terminate */
        fputs(recvline,stdout);
    }
}

server.c

#include “defs.h”
#include “utils.c”

main(argc, argv)
int argc;
char *argv[];
{
    FILE *fp;
    char filename[100];
    int sockfd, newsockfd, clilen, childpid,n;
    struct sockaddr_in cli_addr, serv_addr; pname=argv[0];

    if ((sockfd=socket(AF_INET,SOCK_STREAM,0)) <0)
    {
        printf(”server: can’t open stream socket\n”);
        exit(0);
    }

    bzero((char*) &serv_addr, sizeof(serv_addr));

    serv_addr.sin_family=AF_INET;
    serv_addr.sin_addr.s_addr=htonl(INADDR_ANY);
    serv_addr.sin_port=htons(SERV_TCP_PORT);

    if(bind(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0)
    {
        printf(”server: can’t bind local address\n”);
        exit(0);
    }

    listen(sockfd,5);

    for ( ; ; )
    {
        clilen=sizeof(cli_addr);
        newsockfd=accept(sockfd,(struct sockaddr *) &cli_addr,&clilen);

        if(newsockfd < 0)
        {
            printf(”server: accept error\n”);
            exit(0);

        }

        if((childpid=fork()) <0)
        {
            printf(”server: fork error\n”);
            exit(0);
        }
        else if (childpid==0)
        {
            close(sockfd);
            if((n=readn(newsockfd,filename,sizeof(filename))) <= 0)
            {
                printf(”server: filename read error\n”);
            }
            filename[n]=’\0′;
            if ((fp=fopen(filename,”r”)) == NULL)
            {
                printf(”De mporo na anoikso to arxeio\n”);
                exit(-1);
            }
            str_cli(fp, newsockfd);
            /*str_echo(newsockfd);*/
            fclose(fp);
            exit(0);
        }
        close(newsockfd);
    }
}

TCP - Client

defs.h

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define SERV_TCP_PORT 6000

#define SERV_HOST_ADDR “127.0.0.1″

char *pname;

utils.c

#include “defs.h”

#define MAXLINE 512

/* Read n bytes from a descriptor. Use it in place of read() when fd is a stream socket. */
int readn(fd, ptr, nbytes)
register int fd;
register char *ptr;
register int nbytes;
{
    int nleft, nread;
     nleft=nbytes;

     while (nleft>0)
    {
        nread=read(fd, ptr, nleft);

        if (nread<0) return(nread); /* error */
        else if (nread==0) break; /* EOF */
         nleft -=nread; ptr+=nread;
     }
return(nbytes-nleft);
}

/* Write n bytes from a descriptor. Use it in place of write() when fd is a stream socket. */
int writen(fd, ptr, nbytes)
register int fd;
register char *ptr;
register int nbytes;
{
    int nleft, nwritten;
     nleft=nbytes;
     while (nleft>0)
    {
        nwritten=write(fd,ptr, nleft);
        if (nwritten<=0)
        return(nwritten); /* error */
        nleft -=nwritten;
        ptr+=nwritten;
    }

return(nbytes-nleft);
}

/* Read a line from a descriptor. Read the line one byte at a time,
looking for the newline. We store the newline in the buffer, then
follow it with a null. We return the number of characters up to,
but not including, the null. */

int readline(fd, ptr, maxlen)
register int fd;
register char *ptr;
register int maxlen;

{
    int n,rc;
     char c;
     for (n=1; n<maxlen;n++)
     {
         if ((rc=read(fd,&c,1))==1)
        {
             *ptr++=c;
            if(c==’\n’) break;
        }
         else if (rc==0)
         {
             if (n==1) return(0); /* EOF no data read */
            else break; /* EOF, some data was read */
         }
         else return(-1); /* error */
    }
    *ptr=0;

    return(n);
}

/* Read a stream socket one line at a time and write each line back to the sender.
Return when the connection is terminated. */
str_echo(sockfd) int sockfd;
{
     int n; char line[MAXLINE];
     for ( ; ; )
    {
         n = readline(sockfd, line, MAXLINE);

        if (n==0) return; /* connection terminated */
         else if (n < 0) printf(”str_echo: readline error\n”);
         if (writen(sockfd, line, n) != n)
             printf(”str_echo: writen error\n”);
    }
}

/* read the contents of the FILE *fp, write each line to the stream socket (to the server process),
then read a line back from the socket and write it to the standard output.
Return to caller when an EOF is encountered on the input file. */

str_cli(fp, sockfd)
register FILE *fp;
register int sockfd;
{

    int n;
    char sendline[MAXLINE], recvline[MAXLINE+1];

    while(fgets(sendline, MAXLINE, fp) !=NULL)
    {
        n=strlen(sendline);
        if (writen(sockfd,sendline,n)!=n)
            printf(”str_cli: writen error on socket\n”);
        /* Now read a line from the socket and write it to our standard output */
        n=readline(sockfd,recvline,MAXLINE);
        if (n<0) printf(”str_cli: readline error\n”); recvline[n]=0; /* null terminate */
        fputs(recvline,stdout);
    }
}

client.c

#include “defs.h”
#include “utils.c”

main(argc, argv)
int argc;
char *argv[];
{
    char filename[100];
    int sockfd;
    struct sockaddr_in serv_addr;
    pname=argv[0];
    bzero((char*) &serv_addr, sizeof(serv_addr));
    serv_addr.sin_family=AF_INET;
    serv_addr.sin_addr.s_addr=inet_addr(SERV_HOST_ADDR);
    serv_addr.sin_port=htons(SERV_TCP_PORT);

    if((sockfd=socket(AF_INET,SOCK_STREAM,0)) < 0)
    {
        printf(”client: can’t open stream socket\n”);
        exit(0);
    }

    if (connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0)
    {
        printf(”client: can’t connect to server\n”);
        exit(0);
    }

    /*////////////////////////*/
    printf(”Dose to onoma tou arxeiou:\n”);
    scanf(”%s”,&filename);
    writen(sockfd,filename,sizeof(filename));

    /*str_cli(stdin, sockfd);*/
    str_echo(sockfd);
    /*////////////////////////*/

    close(sockfd);
    exit(0);
}