CMPSCI 377: Operating Systems
Solutions to Homework 1: Processes


1.What is multiprogramming and what are its advantages?

Multiprogramming is the ability of OS keeps several jobs in memory simultaneously.

Advantages: makes efficient use of the CPU and I/O devices by overlapping the demands

for CPU and it's I/O devices from various users. This allows OS to increase

system utilization and throughput.

2.What is the purpose of system call?

In order to protect the system from aberrant users there are some privileged instructions (such as: clear memory. etc.) that only can be execute by the kernel, system calls provide an interface between user and OS kernel, crossing the protection boundaries, let user invoke OS to execute some kernel procedures.

3. What is a context switch? Describe the actions taken by a kernel for a context switch?

Switching the CPU to another process requires saving the state of the old process and loading the saved state for the new process, this task is known as a context switch.

On a contact switch, the OS must save the current execution context (the PC, SP , register, memory mapping information, etc.) in the OS's PCB for this process, and changes the program's status to

"ready", "waiting" or "terminated" as appropriate. The OS then selects a process to execute from

the ready queue, changes this process's status to "running", and loads the process's execution context

from the OS's PCB for this process onto the hardware.

4. you can try the code on edlab machines like: gcc fork.cc, and see what happen.

#include<stdio.h>

#include<sys/wait.h>

#include<unistd.h>

int main() {

int child,grand_child,status;

char c[5];

child = fork();

if (child == 0) { /* this is the child */

grand_child = fork();

if(grand_child == 0) { /* this is the grand child */

printf("I am the grandchild with pid %d .\n",getpid());

sleep(60);

}

else { /* child */

printf("I am the child with pid %i.\n",getpid());

printf("hit any key to kill the grandchild\n");

gets(c);

kill(grand_child,SIGKILL);

exit(0);

}

}

else {/* this is the parent */

pid = getpid(); /*get pid for parent*/

printf("I am the parent with pid %i.\n",pid);

wait(&status); /* wait for child to finish; can also use waitpid(child) here */

printf("Child is finished.\n");

}

return 0;

}

5. 1) with the mailbox::send(ToID, msg) and mailbox::receive(FromID, msg)

it’s impossible for a process to wait to get a message from any one of a number

of processes because when the mailbox::receive(FromID, msg) called the receiver

will block until a specific sender(FromID) send message, if the sender has no message

to send, then the receiver will block the system, it can't move on.

With the mailbox::empty(FromID) it's possible, code like this:

//suppose n processes have id arranged from 0 to n-1

int FromID = 0;

While(true)

{

If(!mailbox:empty(FromID))

{

mailbox:receiver(FromID,msg);

????????????????

process msg

???????????????

}

FromID = (FromID++) %n;

}