• Home
  • Textbooks
  • Operating System Concepts Essentials
  • Processes

Operating System Concepts Essentials

Abraham Silberschatz, Peter B. Galvin, Greg Gagne

Chapter 3

Processes - all with Video Answers

Educators


Chapter Questions

Problem 1

Palm OS provides no means of concurrent processing. Discuss three major complications that concurrent processing adds to an operating system.

Check back soon!

Problem 2

The Sun UltraSPARC processor has multiple register sets. Describe what happens when a context switch occurs if the new context is already loaded into one of the register sets. What happens if the new context is in memory rather than in a register set and all the register sets are in use?

Check back soon!
01:10

Problem 3

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

James Kiss
James Kiss
Numerade Educator

Problem 4

With respect to the RPC mechanism, consider the "exactly once" semantic. Does the algorithm for implementing this semantic execute correctly even if the ACK message back to the client is lost due to a network problem? Describe the sequence of messages and discuss whether "exactly once" is still preserved.
#include <stdio.h>
#include <unistd.h>
int main()
{
/* fork a child process */
fork();
/* fork another child process */
fork();
/* and fork another */
fork();
return 0;
}
Figure 3.22 How many processes are created?

Check back soon!

Problem 5

Assume that a distributed system is susceptible to server failure. What mechanisms would be required to guarantee the "exactly once" semantic for execution of RPCs?

Check back soon!

Problem 6

Describe the differences among short-term, medium-term, and longterm scheduling.

Check back soon!

Problem 7

Describe the actions taken by a kernel to context-switch between processes.

Check back soon!

Problem 8

Construct a process tree similar to Figure 3.9. To obtain process information for the UNIX or Linux system, use the command ps -ael. Use the command man ps to get more information about the ps command. On Windows systems, you will have to use the task manager.

Check back soon!
00:59

Problem 9

Including the initial parent process, how many processes are created by the program shown in Figure 3.22?

Adam Conner
Adam Conner
Numerade Educator
01:10

Problem 10

Using the program in Figure 3.23, identify the values of pid at lines A, B, $C$, and D. (Assume that the actual pids of the parent and child are 2600 and 2603, respectively.)

James Kiss
James Kiss
Numerade Educator

Problem 11

Consider the RPC mechanism. Describe the undesirable consequences that could arise from not enforcing either the "at most once" or "exactly once" semantic. Describe possible uses for a mechanism that has neither of these guarantees.

Check back soon!
01:08

Problem 12

Using the program shown in Figure 3.24, explain what the output will be at Line $A$.

Willis James
Willis James
Numerade Educator
01:19

Problem 13

What are the benefits and the disadvantages of each of the following? Consider both the system level and the programmer level.
a. Synchronous and asynchronous communication
b. Automatic and explicit buffering
c. Send by copy and send by reference
d. Fixed-sized and variable-sized messages

James Kiss
James Kiss
Numerade Educator
06:04

Problem 14

The Fibonacci sequence is the series of numbers $0,1,1,2,3,5,8, \ldots$ Formally, it can be expressed as:
$$
\begin{aligned}
& f i b_0=0 \\
& f i b_1=1 \\
& \text { fib }=f i b_{n-1}+f i b_{n-2}
\end{aligned}
$$
#include <sys/types.h>
#include <stdio.h>
#include <unistd.h>
int main()
{
pid t pid, pid1;
/* fork a child process */
pid = fork();
if (pid < 0) { /* error occurred */
fprintf(stderr, "Fork Failed");
return 1;
}
else if (pid == 0) { /* child process */
pid1 = getpid();
printf("child: pid = %d",pid); /* A */
printf("child: pid1 = %d",pid1); /* B */
}
else { /* parent process */
pid1 = getpid();
printf("parent: pid = %d",pid); /* C */
printf("parent: pid1 = %d",pid1); /* D */
wait(NULL);
}
return 0;
}

Figure 3.23 What are the pid values?
Write a C program using the fork () system call that generates the Fibonacci sequence in the child process. The number of the sequence will be provided in the command line. For example, if 5 is provided, the first five numbers in the Fibonacci sequence will be output by the child process. Because the parent and child processes have their own copies of the data, it will be necessary for the child to output the sequence. Have the parent invoke the wait () call to wait for the child process to complete before exiting the program. Perform necessary error checking to ensure that a non-negative number is passed on the command line.

Angela Guo
Angela Guo
Numerade Educator

Problem 15

Repeat the preceding exercise, this time using the CreateProcess() function in the Win32 API. In this instance, you will need to specify a separate program to be invoked from CreateProcess(). It is this separate program that will run as a child process outputting the Fibonacci sequence. Perform necessary error checking to ensure that a non-negative number is passed on the command line.

Check back soon!
06:54

Problem 16

Modify the date server shown in Figure 3.19 so that it delivers random jokes rather than the current date. Allow the jokes to contain multiple
#include <sys/types.h>
#include <stdio.h>
#include <unistd.h>
int value = 5;
int main()
{
pid t pid;
pid = fork();
if (pid == 0) { /* child process */
value += 15;
return 0;
}
else if (pid > 0) { /* parent process */
wait(NULL);
printf("PARENT: value = %d",value); /* LINE A */
return 0;
}
}

Figure 3.24 What output will be at Line A?
lines. The date client shown in Figure 3.20 can be used to read the multi-line jokes returned by the joke server.

Harriet O'Brien
Harriet O'Brien
Numerade Educator

Problem 17

An echo server echoes back whatever it receives from a client. For example, if a client sends the server the string Hello there! the server will respond with the exact data it received from the client-that is, Hello there!
Write an echo server using the Java networking API described in Section 3.6.1. This server will wait for a client connection using the accept () method. When a client connection is received, the server will loop, performing the following steps:
$\bullet$ Read data from the socket into a buffer.
$\bullet$ Write the contents of the buffer back to the client.
The server will break out of the loop only when it has determined that the client has closed the connection.
The server shown in Figure 3.19 uses the java. io. BufferedReader class. BufferedReader extends the java.io. Reader class, which is used for reading character streams. However, the echo server cannot guarantee that it will read characters from clients; it may receive binary data as well. The class java. io. InputStream deals with data at the byte level rather than the character level. Thus, this echo server must use an object that extends java.io. InputStream. The read() method in the java. io. InputStream class returns -1 when the client has closed its end of the socket connection.

Check back soon!

Problem 18

In Exercise 3.14, the child process must output the Fibonacci sequence, since the parent and child have their own copies of the data. Another approach to designing this program is to establish a shared-memory segment between the parent and child processes. This technique allows the child to write the contents of the Fibonacci sequence to the sharedmemory segment and has the parent output the sequence when the child completes. Because the memory is shared, any changes the child makes will be reflected in the parent process as well.
This program will be structured using POSIX shared memory as described in Section 3.5.1. The program first requires creating the data structure for the shared-memory segment. This is most easily accomplished using a struct. This data structure will contain two items: (1) a fixed-sized array of size MAX_SEQUENCE that will hold the Fibonacci values and (2) the size of the sequence the child process is to generatesequence_size, where sequence_size $\leq$ MAX_SEQUENCE. These items can be represented in a struct as follows:
#define MAX SEQUENCE 10
typedef struct {
long fib sequence[MAX SEQUENCE];
int sequence size;
} shared data;
The parent process will progress through the following steps:
a. Accept the parameter passed on the command line and perform
error checking to ensure that the parameter is ? MAX SEQUENCE.
b. Create a shared-memory segment of size shared data.
c. Attach the shared-memory segment to its address space.
d. Set the value of sequence size to the parameter on the command
line.
e. Fork the child process and invoke the wait() system call to wait
for the child to finish.
f. Output the value of the Fibonacci sequence in the shared-memory
segment.
g. Detach and remove the shared-memory segment.
Because the child process is a copy of the parent, the shared-memory region will be attached to the child’s address space as well as the parent’s. The child process will then write the Fibonacci sequence to shared memory and finally will detach the segment. One issue of concern with cooperating processes involves synchro- nization issues. In this exercise, the parent and child processes must besynchronized
so that the parent does not output the Fibonacci sequence until the child finishes generating the sequence. These two processes will be synchronized using the wait() system call; the parent process will invoke wait(), which will cause it to be suspended until the child
process exits.

Check back soon!

Problem 19

Most UNIX and Linux systems provide the ipcs command. This command lists the status of various POSIX interprocess communication mechanisms, including shared-memory segments. Much of the information for the command comes from the data structure struct shmid_ds, which is available in the/usr/include/sys/shm.h file. Some of the fields in this structure include:
$\bullet$ int shm_segsz-size of the shared-memory segment
$\bullet$ short shm_nattch-number of attaches to the shared-memory segment
$\bullet$ struct ipc_perm shm_perm-permission structure of the sharedmemory segment
The struct ipc_perm data structure (which is available in the file /usr/include/sys/ipc.h) contains the fields:
$\bullet$ unsigned short uid-identifier of the user of the shared-memory segment
$\bullet$unsigned short mode-permission modes
$\bullet$ key_t key (on Linux systems,__key)_- user-specified key identifier
The permission modes are set according to how the shared-memory segment is established with the shmget () system call. Permissions are identified according to the following:
$$
\begin{array}{|c|c|}
\hline \text { mode } & {\text { meaning }} \\
\hline \hline 0400 & \text { Read permission of owner. } \\
\hline 0200 & \text { Write permission of owner. } \\
\hline 0040 & \text { Read permission of group. } \\
\hline 0020 & \text { Write permission of group. } \\
\hline 0004 & \text { Read permission of world. } \\
\hline 0002 & \text { Write permission of world. } \\
\hline
\end{array}
$$
Permissions can be accessed by using the bitwise $A N D$ operator \&. For example, if the statement mode \& 0400 evaluates to "true," the permission mode gives read permission to the owner of the sharedmemory segment.
A shared-memory segment can be identified according to a userspecified key or according to the integer value returned from the shmget () system call, which represents the integer identifier of the shared-memory segment created. The shm_ds structure for a given integer segment identifier can be obtained with the following shmctl()
system call:
/* identifier of the shared memory segment*/
int segment id;
shm ds shmbuffer;
shmctl(segment id, IPC STAT, &shmbuffer);
If successful, shmctl() returns 0; otherwise, it returns ?1 indicating an
error condition (the global variable errno can be accessed to determine
the error condition).
Write a C program that is passed an identifier for a shared-memory
segment. This program will invoke the shmctl() function to obtain
its shm ds structure. It will then output the following values of the
shared-memory segment:
• Segment ID
• Key
• Mode
• Owner UID
• Size
• Number of attaches

Check back soon!