客户端服务器使用fifos,发送突发和接收处理时间

时间:2016-10-29 01:58:17

标签: c server client fifo

晚上好人。我一直在研究这个代码,它在客户端发送一个突发,在结构上发送私有fifo名称,发送到一个服务器,它将处理多个客户端以及将通过私有方式发送回客户端的突发时间FIFO。

问题是,一旦我在客户端进入突发,它就不会向服务器发送正确的突发,而服务器又将读取私有的fifo名称而不是突发,因为没有突发(并且服务器具有用户输入的设置突发,实际突发将成为一个巨大的数字。

我正在附上我的代码希望比另一双眼睛可以看到我在哪里弄错了并帮我纠正它。感谢

客户:

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>



typedef struct values {
     char name[30];
     int arrivalTime;
     int burst;           //used for both the initial size of the process and to send the completion time
} Process;  /*Datatype of the elements in the queue*/


int main( int argc, char *argv[] ){

    Process process;

    int fdIN; //to write to character server
    int fdOUT; //to read from character server
    int clientID;

    clientID = getpid();
    sprintf(process.name, "FIFO_%d", clientID);
    printf("\nFIFO name is %s ", process.name);

  if((mkfifo(process.name, 0666)<0 && errno != EEXIST))
    {
      perror("Can't create private FIFO\n");
      exit(-1);

    }

    printf("\nEnter burst: \n");

    scanf("%d", process.burst);




  if((fdIN=open("commFIFO", O_WRONLY))<0)  //writting into fifo
     printf("cant open fifo to write");

     write(fdIN, &process, sizeof(process));


  if((fdOUT=open(process.name, O_RDONLY))<0) //reading from fifo
     printf("cant open fifo to read");


  read(fdOUT, &process, sizeof(process));

  printf("\n arrival time: %d \n", process.arrivalTime);

  unlink ("commFIFO");
  unlink (process.name);

  close(fdIN);
  close(fdOUT);

}

我对客户的意见:

FIFO name is FIFO_24494
Enter burst:
3

服务器:

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>



//Inbox/Outbox structure
typedef struct values {
     char name[30];
     int arrivalTime;
     int burst;           //used for both the initial size of the process and to send the completion time
} Process;


//Node definition
typedef struct node{                         /*Nodes stored in the linked list*/
        struct values request;
        struct node *next;
} Node;


//Queue Definition
typedef struct queue{                     /*A struct facilitates passing a queue as an argument*/
        Node *head;                       /*Pointer to the first node holding the queue's data*/
        Node *tail;                       /*Pointer to the last node holding the queue's data*/
        int sz;                           /*Number of nodes in the queue*/
} Queue;


int size( Queue *Q ){
        return Q->sz;
}


int isEmpty( Queue *Q ){
        if( Q->sz == 0 ) return 1;
        return 0;
}


void enqueue( Queue *Q, struct values elem ){
        Node *v = (Node*)malloc(sizeof(Node));/*Allocate memory for the Node*/
        if( !v ){
                printf("ERROR: Insufficient memory\n");
                return;
        }
        v->request = elem;
        v->next = NULL;
        if( isEmpty(Q) ) Q->head = v;
        else Q->tail->next = v;
        Q->tail = v;
        Q->sz++;
}


struct values dequeue( Queue *Q ){
        Node *oldHead;
        struct values temp;
        if( isEmpty(Q) ){
                printf("ERROR: Queue is empty\n");
                return temp;
        }
        oldHead = Q->head;
        temp = Q->head->request;  //72
        Q->head = Q->head->next;
        free(oldHead);
        Q->sz--;
        return temp;
}


void requeue(Queue *Q, int t){
        Process temp = dequeue(Q);
        temp.burst -= t;
        enqueue(Q, temp);
}


struct values first( Queue *Q ){
        if( isEmpty(Q) ){
                printf("ERROR: Queue is empty\n");
                return Q->head->request;
        }
        return Q->head->request;
}

int *getBurst(struct values r){
        return &r.burst;
}

void setBurst(struct values *r, int a){
        r->burst = a;
}


char *getName(struct values r){
        return r.name;
}

void destroyQueue( Queue *Q ){
        while( !isEmpty(Q) ) dequeue(Q);
}

void checkNew( Queue *Q, Queue *R, int c ){
         if (!isEmpty(Q)){
                struct values temp1 = first(Q);
                if(temp1.arrivalTime <= c){
                      struct values temp2 = dequeue(Q);
                      enqueue( R, temp2 );
                      printf("NEW process arrived at %d time!!!\n", c);
                }
         }
         else{
               printf("\nNot yet\n");
         }
}

////////////////////////////////////////////////
/////////////////MAIN///////////////////////////

main (void)
{
     int fdIN;          //common fifo file descriptor
     int fdOUT;         //private FIFOs' file desriptors
     int finish;        //status of fifos
     int count;         //Total number of clients, set by user
     int clock = 0;     //System Clock
     int i = 0;         //Iterative counter used for each section
     int timeQuaint;    //Size of clock burst, set by user
     int turnaround = 0;     //will store total turnaround time to calculate the average at the end

     Process process;

     /*Declare a queue and initialize its fields*/
     Queue Ready;
     Ready.head = NULL;
     Ready.tail = NULL;
     Ready.sz = 0;

     /*Declare a queue and initialize its fields*/
     Queue notArrived;
     notArrived.head = NULL;
     notArrived.tail = NULL;
     notArrived.sz = 0;


    //Prep commFIFO for clients
    if ((mkfifo("commFIFO",0666)<0 && errno != EEXIST))         /*Creating commFIFO*/
    {
        perror("Can't create Common FIFO\n");
        exit(-1);
    }


///////////////////////////////////////////////////////All variables defined and commFIFO open for clients
////////////////////////////UI & Enqueueing////////////////

    //User Promts
    printf("How many clients do you have? (Max of 10.)\n");
    scanf("%d", &count);
    int finalTimes[count];

    printf("How big is the server's burst?\n");
    scanf("%d", &timeQuaint);


    if((fdIN=open("commFIFO", O_RDONLY))<0)                       /*Opening commFIFO*/
        printf("Can't open common FIFO to read\n");

    //Read from clients
    int totalBurst =0; //will store total of burst from requests for final reporting
    while( i < count)                                               //For each process
     {
          printf("\nWaiting for client request...\n\n");

          read(fdIN, &process, sizeof(process));
          printf("The size is %d\n", process.burst);
          printf("The Private FIFO name is %s.\n", process.name);
          int b = process.burst;

          totalBurst = b + totalBurst;
                                  //added for reporting

          //Assign process to Ready or notArrived
          if(process.arrivalTime > 0){
                    printf("This process has not yet arrived.\n\n");
                    enqueue(&notArrived, process);
          }
          else{
                    enqueue(&Ready, process);
          }
          i++;
          printf("\nSize of Ready queue: %d\nSize of notArrived queue: %d\n\n", Ready.sz, notArrived.sz);
     }
     printf("\nTotal Bursts: %d\n", totalBurst);
     //Close commFIFO
     close(fdIN);
     unlink("commFIFO");
     printf("commFIFO closed and unlinked.\n\n");

///////////////////////////////////////////////////////All user requests queued
////////////////////////////BODY///////////////////////Queue begins to roll
     i = 0;
     while( !isEmpty(&Ready) || !isEmpty(&notArrived)){
         if(!isEmpty(&Ready)){
              printf("\n\n%d  = qSize\n\nHead timeRemaining: %d\n", Ready.sz, *getBurst(first(&Ready)));

              //Return Data to each client
              if(*getBurst(first(&Ready)) > timeQuaint || isEmpty(&Ready)){    //If there is more process left then 1 burst or Ready Queue is empty
                  clock += timeQuaint;

                  checkNew( &notArrived, &Ready, clock );                       //new line to check for new process
                  requeue(&Ready, timeQuaint);
              }
              else{
                  process = first(&Ready);
                  printf("Item dequeued.\n%s\n\n", process.name);
                  clock += *getBurst(first(&Ready));
                  process.burst = clock;                                     //reset burst as completion time
                  process.arrivalTime = clock  - process.arrivalTime;
                  turnaround += process.arrivalTime;
                  finalTimes[i] = clock;
                  i++;
                  dequeue(&Ready);
                  checkNew( &notArrived, &Ready, clock );                          //new line to check for new process
                  printf("YAY\n");
                  printf("A Process(%s) has finished at: %d time units\n\n", process.name, clock);
                  fdOUT = open(process.name, O_WRONLY);             //Open privFIFO
                  write(fdOUT, &process, sizeof(process));          //Write to privFIFO
              }
          }
        else{
              clock += 1;
              checkNew( &notArrived, &Ready, clock);
        }
     }//END QUEUE
     destroyQueue(&Ready);
     destroyQueue(&notArrived);

////////////////////////////////////////////////////////All processes have been exhausted and Queue is empty
/////////////////Post Queue Reporting///////////////////

     i = 0;
     int x = 0;


     while(i < count){
          if(x< finalTimes[i]&&x<250){
               printf("*");
               x++;
          }
          else{
               printf("Client Complete@%d", finalTimes[i]);
               i++;
          }
     }//END printing WHILE
     printf("\nNew PRINT");
     int t = turnaround/count;
     int u = finalTimes[count-1];
     printf("\n\nAverage Turnaround Time: %d\n\nCPU Utilization: %d/%d\n", t, totalBurst, u);
}

来自服务器的输入和结果:

How many clients do you have? (Max of 10.)
1
How big is the server's burst?
5

Waiting for client request...

The size is 134615364
The Private FIFO name is FIFO_24494.
This process has not yet arrived.


Size of Ready queue: 0
Size of notArrived queue: 1


Total Bursts: 134615364
commFIFO closed and unlinked.

此时我用ctrl + C取消了程序以取消反转计数中的重复突发。

再次提供任何帮助或建议。感谢

1 个答案:

答案 0 :(得分:0)

我发现了自己的错误,我希望将来可以帮助其他人,唯一缺少的就是小小的&amp; (符号)。

客户:

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>



typedef struct values {
     char name[30];
     int arrivalTime;
     int burst;           //used for both the initial size of the process and to send the completion time
} Process;  /*Datatype of the elements in the queue*/


int main( int argc, char *argv[] ){

    Process process;

    int fdIN; //to write to character server
    int fdOUT; //to read from character server
    int clientID;

    clientID = getpid();
    sprintf(process.name, "FIFO_%d", clientID);
    printf("\nFIFO name is %s ", process.name);

  if((mkfifo(process.name, 0666)<0 && errno != EEXIST))
    {
      perror("Can't create private FIFO\n");
      exit(-1);

    }

    printf("\nEnter burst: \n");

    scanf("%d", &process.burst);




  if((fdIN=open("commFIFO", O_WRONLY))<0)  //writting into fifo
     printf("cant open fifo to write");

     write(fdIN, &process, sizeof(process));


  if((fdOUT=open(process.name, O_RDONLY))<0) //reading from fifo
     printf("cant open fifo to read");


  read(fdOUT, &process, sizeof(process));

  printf("\n arrival time: %d \n", process.arrivalTime);

  unlink ("commFIFO");
  unlink (process.name);

  close(fdIN);
  close(fdOUT);

}

服务器:

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>



//Inbox/Outbox structure
typedef struct values {
     char name[30];
     int arrivalTime;
     int burst;           //used for both the initial size of the process and to send the completion time
} Process;


//Node definition
typedef struct node{                         /*Nodes stored in the linked list*/
        struct values request;
        struct node *next;
} Node;


//Queue Definition
typedef struct queue{                     /*A struct facilitates passing a queue as an argument*/
        Node *head;                       /*Pointer to the first node holding the queue's data*/
        Node *tail;                       /*Pointer to the last node holding the queue's data*/
        int sz;                           /*Number of nodes in the queue*/
} Queue;


int size( Queue *Q ){
        return Q->sz;
}


int isEmpty( Queue *Q ){
        if( Q->sz == 0 ) return 1;
        return 0;
}


void enqueue( Queue *Q, struct values elem ){
        Node *v = (Node*)malloc(sizeof(Node));/*Allocate memory for the Node*/
        if( !v ){
                printf("ERROR: Insufficient memory\n");
                return;
        }
        v->request = elem;
        v->next = NULL;
        if( isEmpty(Q) ) Q->head = v;
        else Q->tail->next = v;
        Q->tail = v;
        Q->sz++;
}


struct values dequeue( Queue *Q ){
        Node *oldHead;
        struct values temp;
        if( isEmpty(Q) ){
                printf("ERROR: Queue is empty\n");
                return temp;
        }
        oldHead = Q->head;
        temp = Q->head->request;  //72
        Q->head = Q->head->next;
        free(oldHead);
        Q->sz--;
        return temp;
}


void requeue(Queue *Q, int t){
        Process temp = dequeue(Q);
        temp.burst -= t;
        enqueue(Q, temp);
}


struct values first( Queue *Q ){
        if( isEmpty(Q) ){
                printf("ERROR: Queue is empty\n");
                return Q->head->request;
        }
        return Q->head->request;
}

int *getBurst(struct values r){
        return &r.burst;
}

void setBurst(struct values *r, int a){
        r->burst = a;
}


char *getName(struct values r){
        return r.name;
}

void destroyQueue( Queue *Q ){
        while( !isEmpty(Q) ) dequeue(Q);
}

void checkNew( Queue *Q, Queue *R, int c ){
         if (!isEmpty(Q)){
                struct values temp1 = first(Q);
                if(temp1.arrivalTime <= c){
                      struct values temp2 = dequeue(Q);
                      enqueue( R, temp2 );
                      printf("NEW process arrived at %d time!!!\n", c);
                }
         }
         else{
               printf("\nNot yet\n");
         }
}

////////////////////////////////////////////////
/////////////////MAIN///////////////////////////

main (void)
{
     int fdIN;          //common fifo file descriptor
     int fdOUT;         //private FIFOs' file desriptors
     int finish;        //status of fifos
     int count;         //Total number of clients, set by user
     int clock = 0;     //System Clock
     int i = 0;         //Iterative counter used for each section
     int timeQuaint;    //Size of clock burst, set by user
     int turnaround = 0;     //will store total turnaround time to calculate the average at the end

     Process process;

     /*Declare a queue and initialize its fields*/
     Queue Ready;
     Ready.head = NULL;
     Ready.tail = NULL;
     Ready.sz = 0;

     /*Declare a queue and initialize its fields*/
     Queue notArrived;
     notArrived.head = NULL;
     notArrived.tail = NULL;
     notArrived.sz = 0;


    //Prep commFIFO for clients
    if ((mkfifo("commFIFO",0666)<0 && errno != EEXIST))         /*Creating commFIFO*/
    {
        perror("Can't create Common FIFO\n");
        exit(-1);
    }


///////////////////////////////////////////////////////All variables defined and commFIFO open for clients
////////////////////////////UI & Enqueueing////////////////

    //User Promts
    printf("How many clients do you have? (Max of 10.)\n");
    scanf("%d", &count);
    int finalTimes[count];

    printf("How big is the server's burst?\n");
    scanf("%d", &timeQuaint);


    if((fdIN=open("commFIFO", O_RDONLY))<0)                       /*Opening commFIFO*/
        printf("Can't open common FIFO to read\n");

    //Read from clients
    int totalBurst =0; //will store total of burst from requests for final reporting
    while( i < count)                                               //For each process
     {
          printf("\nWaiting for client request...\n\n");

          read(fdIN, &process, sizeof(process));
          printf("The size is %d\n", process.burst);
          printf("The Private FIFO name is %s.\n", process.name);
          int b = process.burst;

          totalBurst = b + totalBurst;
                                  //added for reporting

          //Assign process to Ready or notArrived
          if(process.arrivalTime > 0){
                    printf("This process has not yet arrived.\n\n");
                    enqueue(&notArrived, process);
          }
          else{
                    enqueue(&Ready, process);
          }
          i++;
          printf("\nSize of Ready queue: %d\nSize of notArrived queue: %d\n\n", Ready.sz, notArrived.sz);
     }
     printf("\nTotal Bursts: %d\n", totalBurst);
     //Close commFIFO
     close(fdIN);
     unlink("commFIFO");
     printf("commFIFO closed and unlinked.\n\n");

///////////////////////////////////////////////////////All user requests queued
////////////////////////////BODY///////////////////////Queue begins to roll
     i = 0;
     while( !isEmpty(&Ready) || !isEmpty(&notArrived)){
         if(!isEmpty(&Ready)){
              printf("\n\n%d  = qSize\n\nHead timeRemaining: %d\n", Ready.sz, *getBurst(first(&Ready)));

              //Return Data to each client
              if(*getBurst(first(&Ready)) > timeQuaint || isEmpty(&Ready)){    //If there is more process left then 1 burst or Ready Queue is empty
                  clock += timeQuaint;

                  checkNew( &notArrived, &Ready, clock );                       //new line to check for new process
                  requeue(&Ready, timeQuaint);
              }
              else{
                  process = first(&Ready);
                  printf("Item dequeued.\n%s\n\n", process.name);
                  clock += *getBurst(first(&Ready));
                  process.burst = clock;                                     //reset burst as completion time
                  process.arrivalTime = clock  - process.arrivalTime;
                  turnaround += process.arrivalTime;
                  finalTimes[i] = clock;
                  i++;
                  dequeue(&Ready);
                  checkNew( &notArrived, &Ready, clock );                          //new line to check for new process
                  printf("YAY\n");
                  printf("A Process(%s) has finished at: %d time units\n\n", process.name, clock);
                  fdOUT = open(process.name, O_WRONLY);             //Open privFIFO
                  write(fdOUT, &process, sizeof(process));          //Write to privFIFO
              }
          }
        else{
              clock += 1;
              checkNew( &notArrived, &Ready, clock);
        }
     }//END QUEUE
     destroyQueue(&Ready);
     destroyQueue(&notArrived);

////////////////////////////////////////////////////////All processes have been exhausted and Queue is empty
/////////////////Post Queue Reporting///////////////////

     i = 0;
     int x = 0;


     while(i < count){
          if(x< finalTimes[i]&&x<250){
               printf("*");
               x++;
          }
          else{
               printf("Client Complete@%d", finalTimes[i]);
               i++;
          }
     }//END printing WHILE
     printf("\nNew PRINT");
     int t = turnaround/count;
     int u = finalTimes[count-1];
     printf("\n\nAverage Turnaround Time: %d\n\nCPU Utilization: %d/%d\n", t, totalBurst, u);
}