In: Computer Science
Could someone point out the logical error in this code?
The code seems to start at the last input. But I want the code to output something like this:
Enter name: A
Enter time:5
Enter name:B
Enter time: 4
Enter name: C
Enter time:3
Enter name:D
Enter time: 9
Enter name: E
Enter time: 11
A process is started and ends in 5 sec.
1 sec passed...
2 sec passed...
3 sec passed...
4 sec passed...
5 sec passed...
B process is started and ends in 4 sec.
1 sec passed...
2 sec passed...
3 sec passed...
4 sec passed...
C process is started and ends in 3 sec.
1 sec passed...
2 sec passed...
3 sec passed...
D process is started and ends in 9 sec.
1 sec passed...
2 sec passed...
3 sec passed...
4 sec passed...
5 sec passed...
6 sec passed...
7 sec passed...
8 sec passed...
9 sec passed...
E process is started and ends in 10 sec.
1 sec passed...
2 sec passed...
3 sec passed...
4 sec passed...
5 sec passed...
6 sec passed...
7 sec passed...
8 sec passed...
9 sec passed...
10 sec passed...
10 sec reduced in E process with time left 1 sec.
E process is started and ends in 1 sec.
1 sec passed...
All Processes are executed...
Code:
#include #include #include #include #include #define S 4 typedef struct { char pname [S]; int ptime; }PROCESS; typedef struct { PROCESS queue [S]; int front; int rear; }QUE; void createQueue(QUE *); void enCQueue(QUE *, PROCESS); PROCESS deCQueue(QUE *); bool isCEmpty(int,int); bool isCFull(int,int); void tshareProc (PROCESS); int main(void) { PROCESS p; QUE q; createQueue (&q); enCQueue (&q, p); tshareProc(p); return 0; } void createQueue(QUE *q) { memset(q->queue,'\0',S); q->front = 0; q->rear = 0; } void enCQueue(QUE *q, PROCESS p) { q->queue[q->rear] = p; q->rear = (q->rear + 1) % S; } PROCESS deCQueue(QUE *q) { PROCESS p; q->queue[q->front]= p; q->front = (q->front + 1) % S; return; } bool isCEmpty(int front,int rear) { if(front == rear) return true; else return false; } void tshareProc (PROCESS p) { QUE q; int i, j, x; bool empty, full; createQueue (&q); for (i=0; i <= S; i++) { printf ("Enter username:"); scanf("%s",&p.pname); printf("Enter time in seconds:"); scanf("%d",&p.ptime); while(p.ptime < 1) { printf("Time must be greater than zero. \nInput again:"); scanf("%d",&p.ptime); } enCQueue (&q,p); } empty = isCEmpty (q.front, q.rear); while (!empty) { if (p.ptime <= 10){ printf ("\n %s's process is starting and will end in %d seconds \n", p.pname, p.ptime); for (j=0; j < p.ptime; j++){ printf(" %d seconds passed\n",j+1); Sleep(1000); //change to sleep(1000) if giving error } //end of for loop deCQueue (&q); } else { printf("\n %s's process is starting and ends in 10 seconds\n",p.pname); for(x = 0; x < 10; i++) { printf("%d seconds passed\n",x+1); Sleep(1000); //change to sleep(1000) if giving error } // end of for loop p.ptime = p.ptime - 10; printf("10 seconds is subtracted in %s's process with %d seconds left\n",p.pname,p.ptime); deCQueue(&q); } enCQueue(&q, p); } //end of while loop printf ("All processes were performed"); return; }
Following is the fixed code, containing comments for fixes appropriately :
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stdint.h>
#include <unistd.h>
/*
* We should be able to hold maximum 5 processes.
*/
#define S 5
typedef struct
{
char pname [S];
int ptime;
}PROCESS;
typedef struct
{
PROCESS queue [S];
int front;
int rear;
}QUE;
void createQueue(QUE *);
void enCQueue(QUE *, PROCESS);
PROCESS deCQueue(QUE *);
bool isCEmpty(int,int);
bool isCFull(int,int);
void tshareProc (PROCESS);
int main(void)
{
PROCESS p;
QUE q;
createQueue (&q);
enCQueue (&q, p);
tshareProc(p);
return 0;
}
void createQueue(QUE *q)
{
memset(q->queue,'\0',S);
q->front = 0;
q->rear = 0;
}
void enCQueue(QUE *q, PROCESS p)
{
/*
* Add check if queue is full.
*/
if(q->rear == S)
{
printf("\n\n!! Queue is full. Cannot add more !!\n\n");
return;
}
q->queue[q->rear] = p;
q->rear = q->rear + 1;
}
PROCESS deCQueue(QUE *q)
{
PROCESS p;
/*
* We need to properly assign the values "into" p.
*/
p = q->queue[q->front];
q->front = q->front + 1;
return p;
}
bool isCEmpty(int front,int rear)
{
if(front == S)
return true;
else
return false;
}
void tshareProc (PROCESS p)
{
QUE q;
int i, j, x;
bool empty, full;
createQueue (&q);
/*
* Maximum "S" processes should be used.
*/
for (i = 0; i < S; i++)
{
printf ("Enter username:");
scanf("%s",p.pname);
printf("Enter time in seconds:");
scanf("%d",&p.ptime);
while(p.ptime < 1) {
printf("Time must be greater than zero. \nInput again:");
scanf("%d",&p.ptime);
}
enCQueue (&q,p);
}
empty = isCEmpty (q.front, q.rear);
while (!empty)
{
p = deCQueue (&q);
if (p.ptime <= 10){
printf ("\n%s's process is starting and will end in %d seconds \n", p.pname, p.ptime);
for (j=0; j < p.ptime; j++) {
printf("%d seconds passed\n",j+1);
sleep(1); //change to sleep(1000) if giving error
} //end of for loop
}
else
{
while(p.ptime > 0)
{
printf("\n%s's process is starting and ends in %d seconds\n",p.pname, p.ptime < 10 ? p.ptime : 10);
for(j = 0; j < (p.ptime < 10 ? p.ptime : 10); j++) {
printf("%d seconds passed\n",j+1);
sleep(1); //change to sleep(1000) if giving error
} // end of for loop
p.ptime = p.ptime - 10;
if(p.ptime > 0)
{
printf("10 seconds is subtracted in %s's process with %d seconds left\n",p.pname,p.ptime);
}
}
}
/*
* Need to recheck the empty condition.
*/
empty = isCEmpty (q.front, q.rear);
//enCQueue(&q, p);
} //end of while loop
printf ("All processes were performed");
return;
}