Assignment 2
Assignment 2
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;
}
#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);
}
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.