2016-10-29 18 views
-1

おはよう人。私はこのコードを使ってクライアント側のバーストと構造体のプライベート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 [email protected]%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でプログラムをキャンセルしました。

また、助けや助言もよく受け取ります。ありがとう

答えて

0

私は間違いを発見しました。これは他の誰かを助けてくれることを願っています。唯一欠けているものは小さい&(アンパサンド)でした。

クライアント:

#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 [email protected]%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); 
} 
関連する問題