0% found this document useful (0 votes)
5 views

Assignment 1 - Solutions

Uploaded by

jeralax396
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Assignment 1 - Solutions

Uploaded by

jeralax396
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

CEN301 – Operating Systems

Assignment 1 - Solutions
Q1)

binarySemaphore Dough = 1, Olive = 1, Pizza = 1


countingSemaphore Dough_full = 0, Dough_empty = H, Olive_full = 0, Olive_empty = Z
/*Pizza machine*/ /*Dough machine*/ /*Olive machine*/
while (true){ while (true){ while (true){
wait(Dough_full); wait (Dough_empty); wait (Olive_empty);
wait(Olive_full); wait (Dough); wait (Olive);
wait(Olive_full); make_dough(); make_olive();
wait(pizza); signal (Dough); signal (Olive);
make_pizza(); signal (Dough_full); signal (Olive_full);
sigmal(pizza); } }
signal(Dough_empty);
signal(Olive_empty);
signal(Olive_empty);
}

Q2)

int main() {
pid_t pid1, pid2;
int x = 5;
int y = 3;
pid1 = fork();
if (pid1 == 0) {
writeToFile("factorial.txt",factorial(x));
exit(0);
}
pid2 = fork();
if (pid2 == 0) {
writeToFile("power.txt",power(y));
exit(0);
}
waitpid(pid1, NULL, 0);
waitpid(pid2, NULL, 0);
double fact_value, power_value;
fact_value = readFromFile("factorial.txt");
power_value = readFromFile("power.txt",);
double result = fact_value / power_value;
printf("Result: %lf\n",result);
return 0;
}

You might also like