Write a bash program called producer and a C program called consumer.c. A text-b
ID: 3724657 • Letter: W
Question
Write a bash program called producer and a C program called consumer.c. A text-based data file called storage initially contains an integer number between 5 and 90. The producer checks the number in the storage. If the current number plus 8 will be 100 or larger, it will terminate. Otherwise, it will increase the number in the storage by 8 and then call the consumer to continue. The consumer decreases the number in the storage by 3, and then calls the producer to continue. Both the producer and the consumer can be the starter. Sample runs:
assignment-producer more storage 80 from consumer: current total is 77 from producer current total is 85 from consumer: current total is 82 from producer: current total is 98 from consumer: current total is 87 from producer: current total is 95 from consumer: current total is 92 from producer: Now I will take a rest!Explanation / Answer
File for consumer.c
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *fp;
int i = 0;
fp = fopen("storage", "r");
fscanf(fp, "%d", &i);
fclose(fp);
i -= 3;
fp = fopen("storage", "w");
fprintf(fp, "%d", i);
fclose(fp);
printf("from consumer: current total is %d ", i);
system("bash ./producer.sh");
return 0;
}
-------------------------------------------------------------------------------------------------
File for producer.sh
#!/bin/bash
typeset -i value=$(<storage)
typeset -i value2=$value+8
if [ $value2 -ge 100 ]
then
echo "Now I will take a rest!"
else
echo "$value2" > storage
echo "from producer: current total is $value2"
./consumer
fi
---------------------------------------------------------------------------------------------------------------------
Execution instructions
Compile consumer.c using gcc -o consumer consumer.c
Run consumer.c using ./consumer
Run producer.sh using bash producer.sh
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.