ood evening people Ive been working on this code that sends
ood evening people. I\'ve been working on this code that sends a burst on the client side as well as the private fifo name over a struct, to a server which will process several clients and the burst time that will be sent back to the client through the private fifo.
The issue is that once I enter the burst on the client side, it won\'t send the proper burst to the server, which in turn it will read the private fifo name but not the burst, since there is no burst (and the server has a set burst entered by the user), the actual burst will become a giantnumber.
I\'m attaching my code hoping than maybe another pair of eyes can see where I am getting my mistake and help me to correct it. Thanks
client:
my input for the client:
server:
the input and outcome from the server:
at this point I canceled the program with a ctrl + C to cancel the recurring burst in reversive count.
Again any help or suggestion is well received. Thanks
Solution
client:
#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(\"\ FIFO name is %s \", process.name);
if((mkfifo(process.name, 0666)<0 && errno != EEXIST))
{
perror(\"Can\'t create private FIFO\ \");
exit(-1);
}
printf(\"\ Enter burst: \ \")
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(\"\ arrival time: %d \ \", process.arrivalTime);
unlink (\"commFIFO\");
unlink (process.name);
close(fdIN);
close(fdOUT);
}
SERVER:
#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\ \");
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\ \");
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\ \");
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!!!\ \", c);
}
}
else{
printf(\"\ Not yet\ \");
}
}
////////////////////////////////////////////////
/////////////////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\ \");
exit(-1);
}
///////////////////////////////////////////////////////All variables defined and commFIFO open for clients
////////////////////////////UI & Enqueueing////////////////
//User Promts
printf(\"How many clients do you have? (Max of 10.)\ \");
scanf(\"%d\", &count);
int finalTimes[count];
printf(\"How big is the server\'s burst?\ \");
scanf(\"%d\", &timeQuaint);
if((fdIN=open(\"commFIFO\", O_RDONLY))<0) /*Opening commFIFO*/
printf(\"Can\'t open common FIFO to read\ \");
//Read from clients
int totalBurst =0; //will store total of burst from requests for final reporting
while( i < count) //For each process
{
printf(\"\ Waiting for client request...\ \ \");
read(fdIN, &process, sizeof(process));
printf(\"The size is %d\ \", process.burst);
printf(\"The Private FIFO name is %s.\ \", 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.\ \ \");
enqueue(¬Arrived, process);
}
else{
enqueue(&Ready, process);
}
i++;
printf(\"\ Size of Ready queue: %d\ Size of notArrived queue: %d\ \ \", Ready.sz, notArrived.sz);
}
printf(\"\ Total Bursts: %d\ \", totalBurst);
//Close commFIFO
close(fdIN);
unlink(\"commFIFO\");
printf(\"commFIFO closed and unlinked.\ \ \");
///////////////////////////////////////////////////////All user requests queued
////////////////////////////BODY///////////////////////Queue begins to roll
i = 0;
while( !isEmpty(&Ready) || !isEmpty(¬Arrived)){
if(!isEmpty(&Ready)){
printf(\"\ \ %d = qSize\ \ Head timeRemaining: %d\ \", 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( ¬Arrived, &Ready, clock ); //new line to check for new process
requeue(&Ready, timeQuaint);
}
else{
process = first(&Ready);
printf(\"Item dequeued.\ %s\ \ \", 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( ¬Arrived, &Ready, clock ); //new line to check for new process
printf(\"YAY\ \");
printf(\"A Process(%s) has finished at: %d time units\ \ \", process.name, clock);
fdOUT = open(process.name, O_WRONLY); //Open privFIFO
write(fdOUT, &process, sizeof(process)); //Write to privFIFO
}
}
else{
clock += 1;
checkNew( ¬Arrived, &Ready, clock);
}
}//END QUEUE
destroyQueue(&Ready);
destroyQueue(¬Arrived);
////////////////////////////////////////////////////////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(\"\ New PRINT\");
int t = turnaround/count;
int u = finalTimes[count-1];
printf(\"\ \ Average Turnaround Time: %d\ \ CPU Utilization: %d/%d\ \", t, totalBurst, u);
}







