0% found this document useful (0 votes)
31 views4 pages

Assignment 2

Uploaded by

Mohamed Waseem
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)
31 views4 pages

Assignment 2

Uploaded by

Mohamed Waseem
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/ 4

Zagazig University Spring 2024

Faculty of Engineering 3rd Year


Computer and Systems Dept. CSE325: Operating Systems

Assignment #2
Covers: Chapter 3, 4 & 5 Due Date: Thursday March 28, 2024

Chapter 3:
1. When a process creates a new process using the fork() operation, which of the following states
is shared between the parent process and the child process?
a. Stack
b. Heap
c. Shared memory segments
2. Using the program shown below, explain what the output will be at LINE A.

#include <sys/types.h>
#include <stdio.h>
#include <unistd.h>
int value = 25;
int main() {
pid_t pid;
pid = fork();
if (pid == 0) {
value += 25;
return 0;
}
else if (pid > 0) {
wait(NULL);
printf("value = %d", value); /* LINE A */
return 0;
}
}

3. Including the initial parent process, how many processes are created by the program shown
below

#include <stdio.h>
#include <unistd.h>
int main() {
int i;

fork();
fork();
for (i = 0; i < 4; i++)
fork();
return 0;
}

4. Using the program shown below, identify the values of pid at lines A, B, C, and D. (Assume that
the actual pids of the parent and child are 1111 and 4444, respectively.)
Assignment #2 CSE325: Operating Systems, Spring 2024 1/4
#include <sys/types.h>
#include <stdio.h>
#include <unistd.h>
int main() {
pid t pid, pid1;
pid = fork();
if (pid > 0) {
pid1 = getpid();
printf("pid = %d", pid); /* A */
printf("pid1 = %d", pid1); /* B */
wait(NULL);
}
else if (pid == 0) {
pid1 = getpid();
printf("pid = %d", pid); /* C */
printf("pid1 = %d", pid1); /* D */
}
return 0;
}

5. Using the program shown below, explain what the output will be at lines X and Y.

#include <sys/types.h>
#include <stdio.h>
#include <unistd.h>
#define SIZE 5
int nums[SIZE] = { 4,3,2,1,0 };
int main() {
int i;
pid t pid;
pid = fork();
if (pid == 0) {
for (i = 0; i < SIZE; i++) {
nums[i] += -i;
printf("LineX: %d ", nums[i]); /* LINE X */
}
}
else if (pid > 0) {
wait(NULL);
for (i = 0; i < SIZE; i++)
printf("LineY: %d ", nums[i]); /* LINE Y */
}
return 0;
}

Assignment #2 CSE325: Operating Systems, Spring 2024 2/4


Chapter 4:
1. Using Amdahl’s Law, calculate the speedup gain of an application that has a 99 percent parallel
component for
a. two processing cores
b. four processing cores
c. ten processing cores
2. Which of the following components of program state are shared across threads in a
multithreaded process?
a. Register values
b. Heap memory
c. Global variables
d. Stack memory
3. Is it possible to have concurrency but not parallelism? Explain.
4. The program shown Below uses the Pthreads API. What would be the output from the program
at LINE A and LINE B?

#include <pthread.h>
#include <stdio.h>
int value = 0;
void* runner(void* param);
int main(int argc, char* argv[])
{
pid t pid;
pthread t tid;
pthread attr t attr;
pid = fork();
if (pid == 0) {
value += 3;
printf("LINE A: value = %d", value); /* LINE A */
}
else if (pid > 0) {
wait(NULL);
pthread attr init(&attr);
pthread create(&tid, &attr, runner, NULL);
pthread join(tid, NULL);
printf("LINE B: value = %d", value); /* LINE B */
}
}
void* runner(void* param) {
value = 77;
pthread exit(0);
}

Assignment #2 CSE325: Operating Systems, Spring 2024 3/4


Chapter 5:
1. Explain the difference between preemptive and non-preemptive scheduling.
2. Which of the following scheduling algorithms could result in starvation?
a. First-come, first-served
b. Shortest job first
c. Round robin
d. Priority
3. Consider the following set of processes, with the length of the CPU burst and arrival times given
in milliseconds:
Process Arrival Time Burst Time Priority
P1 4.0 15 2
P2 0.0 7 1
P3 2.0 2 4
P4 5.0 6 3
P5 3.0 5 1
P6 1.0 3 5

a. Draw five Gantt charts that illustrate the execution of these processes using the following
scheduling algorithms: FCFS (non-preemptive), SJF (non-preemptive), SRF (preemptive)
preemptive priority (a smaller priority number implies a higher priority), and RR
(quantum = 2).
b. What is the turnaround time of each process for each of the scheduling algorithms in part
a?
c. What is the waiting time of each process for each of these scheduling algorithms?
d. Which of the algorithms results in the minimum average waiting time (over all
processes)?
e. Which of the algorithms results in the minimum average turnaround time (over all
processes)?

Submission
On or before 28/3/2024, your solution to the assignment should be submitted. Follow the following
instructions carefully, otherwise your submission will not be accepted:
• Create a pdf file that contains the solutions. Do not forget to add a cover page that contains your
name and code.

Important Notes:
This assignment must be done individually; any act of plagiarism will be penalized and reported.

Assignment #2 CSE325: Operating Systems, Spring 2024 4/4

You might also like