Great Deal! Get Instant $10 FREE in Account on First Order + 10% Cashback on Every Order Order Now

Programming Interface The Programming Interface Main Points • Creating and managing processes – fork, exec, wait • Performing I/O – open, read, write, close • Communicating between processes – pipe,...

1 answer below »
Programming Interface
The Programming Interface
Main Points
• Creating and managing processes
– fork, exec, wait
• Performing I/O
– open, read, write, close
• Communicating between processes
– pipe, dup, select, connect
• Example: implementing a shell
Shell
• A shell is a job control system
– Allows programmer to create and manage a set of
programs to do some task
– Windows, MacOS, Linux all have shells
• Example: to compile a C program
cc –c sourcefile1.c
cc –c sourcefile2.c
ln –o program sourcefile1.o sourcefile2.o
Question
• If the shell runs at user-level, what system
calls does it make to run each of the
programs?
– Ex: cc, ln
Windows CreateProcess
• System call to create a new process to run a
program
– Create and initialize the process control block (PCB) in
the kernel
– Create and initialize a new address space
– Load the program into the address space
– Copy arguments into memory in the address space
– Initialize the hardware context to start execution at
``start'’
– Inform the scheduler that the new process is ready to
un
Windows CreateProcess API
(simplified)
if (!CreateProcess(
NULL,
No module name (use command line)
argv[1],
Command line
NULL,
Process handle not inheritable
NULL,
Thread handle not inheritable
FALSE,
Set handle inheritance to FALSE
0,
No creation flags
NULL,
Use parent's environment block
NULL,
Use parent's starting directory
&si,
Pointer to STARTUPINFO structure
&pi )
Pointer to PROCESS_INFORMATION structure
)
UNIX Process Management
• UNIX fork – system call to create a copy of the
cu
ent process, and start it running
– No arguments!
• UNIX exec – system call to change the program
eing run by the cu
ent process
• UNIX wait – system call to wait for a process to
finish
• UNIX signal – system call to send a notification
to another process
UNIX Process Management
Question: What does this code print?
int child_pid = fork();
if (child_pid == 0) {
I'm the child process
printf("I am process #%d\n", getpid());
eturn 0;
} else {
I'm the parent process
printf("I am parent of process #%d\n", child_pid);
eturn 0;
}
Questions
• Can UNIX fork() return an e
or? Why?
• Can UNIX exec() return an e
or? Why?
• Can UNIX wait() ever return immediately?
Why?
Implementing UNIX fork
Steps to implement UNIX fork
– Create and initialize the process control block
(PCB) in the kernel
– Create a new address space
– Initialize the address space with a copy of the
entire contents of the address space of the parent
– Inherit the execution context of the parent (e.g.,
any open files)
– Inform the scheduler that the new process is
eady to run
Implementing UNIX exec
• Steps to implement UNIX fork
– Load the program into the cu
ent address space
– Copy arguments into memory in the address
space
– Initialize the hardware context to start execution
at ``start''
UNIX I/O
• Uniformity
– All operations on all files, devices use the same set of
system calls: open, close, read, write
• Open before use
– Open returns a handle (file descriptor) for use in later
calls on the file
• Byte-oriented
• Kernel-buffered read/write
• Explicit close
– To ga
age collect the open file descripto
UNIX File System Interface
• UNIX file open is a Swiss Army knife:
– Open the file, return file descripto
– Options:
• if file doesn’t exist, return an e
o
• If file doesn’t exist, create file and open it
• If file does exist, return an e
o
• If file does exist, open file
• If file exists but isn’t empty, nix it then open
• If file exists but isn’t empty, return an e
o
• …
Interface Design Question
• Why not separate syscalls for
open/create/exists?
if (!exists(name))
create(name);
can create fail?
fd = open(name);
does the file exist?
Implementing a Shell
char *prog, **args;
int child_pid;
Read and parse the input a line at a time
while (readAndParseCmdLine(&prog, &args)) {
child_pid = fork();
create a child process
if (child_pid == 0) {
exec(prog, args);
I'm the child process. Run program
NOT REACHED
} else {
wait(child_pid);
I'm the parent, wait for child
eturn 0;
}
}
Answered Same Day Jul 13, 2021

Solution

Neha answered on Jul 14 2021
130 Votes
Kernel
The kernel can be defined as the central module present in an operating system. This part of the OS is the first one which is loading in the system and once loaded it remains in the main memory. As it remains in memory, we should make sure that the size of kernel should be small, but it should provide all the essential services which are important for other parts of the OS and its application. The code is generally loaded in protected area which helps to prevent it from overwriting (Cristianini, N., Shawe-Taylor, J., Elisseeff, A., & Kandola, J. S).
Switching from kernel mode to user mode
1. Starting a new process.
When we a start a new process it can help us to switch from the kernel mode to user mode and the steps are executed to start a new process. The new code is loaded in the main memory and the program counter and stack pointer values are updated to change the mode from kernel to user mode.
2. Resuming execution after a syscall or an inte
upt or an exception.
When a system call is invocated or...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here