Section3.1
3.1 What are the four components of a process?
stack; heap; data; text
3.2 Provide at least three possible states a process may be in.
1. New. The process is being created.
2. Running. Instructions are being executed.
3. Waiting. The process is waiting for some event to occur (such as an I/O completion or reception of a signal).
4.Ready. The process is waiting to be assigned to a processor.
5.Terminated. The process has finished execution.
3.3 What is a Process Control Block (PCB) ?
Each process is represented in the operating system by a process control block (PCB) — also called a task control block.
It contains many pieces of information associated with a specific process, including these: process state, program counter, CPU registers, CPU- scheduling information, memory-management information, accounting information, I/O status information.
In brief, the PCB simply serves as the repository for any information that may vary from process to process.
3.4 What is another term for process?
Job.
3.5 True or False? Most operating systems allow a process to have multiple threads.
True.Most modern operating systems have extended the process concept to allow a process to have multiple threads of execution and thus to perform more than one task at a time.
Section 3.2
3.6 What is the role of the process scheduler?
The process scheduler selects an available process (possibly from a set of several available processes) for program execution on the CPU to have some process running at all times, to maximize CPU utilization.
3.7 What is the degree of multiprogramming?
the number of processes in memory
3.8 What is the term that describes saving the state of one process, and restoring the state of another?
context switch
Section 3.3
3.10 What is a process identifier (PID)?
PID is an integer number that provides a unique value for each process in the system, and it can be used as an index to access various attributes of a process within the kernel.
3.11 What system call creates a process on UNIX systems?
fork() ;
3.12 What system call creates a process on Windows systems?
CreateProcess()
3.13 What system call terminates a process on UNIX systems?
exit()
3.21 The Collatz conjecture concerns what happens when we take any positive integer n and apply the following algorithm: n= n / 2 , if n is even n= 3 * n + 1, if n is odd The conjecture states that when this algorithm is continually applied, all positive integers will eventually reach 1. For example, if n = 35, the sequence is 35, 106, 53, 160, 80, 40, 20, 10, 5, 16, 8, 4, 2, 1 Write a C program using the fork() system call that generates this sequence in the child process. The starting number will be provided from the command line. For example, if 8 is passed as a parameter on the command line, the child process will output 8, 4, 2, 1. 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 positive integer is passed on the command line.
#include <sys/types.h>
#include <stdio.h>
#include <unistd.h>
int main()
{
pid t pid;
/* fork a child process */
pid = fork();
if (pid < 0) { /* error occurred */
fprintf(stderr, "Fork Failed");
return 1;
}
else if (pid == 0) {
/*child process*/
int num = atoi(argv[1]);
printf("%d,",num);
while (num != 1){
if (num % 2 == 0)/*Even*/{
num = num / 2;
if (num == 1){
printf("%d\n",num);
}
else printf("%d,",num);
}
else{
num = 3*num + 1;
printf("%d,",num);
}
}
}
else{
/*Parent process*/
wait(NULL);/*Wait until the child process completed*/
printf("Child Complete\n");
}
return 0;
}
利用fork,在单个进程中实现父子进程间的shared memory通信
#include <sys/types.h>
#include <stdio.h>
#include <unistd.h>
#include <stlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/shm.h>
#include <sys/stat.h>
int main
{
pid_t pid;
/* the size (in bytes) of shared memory object */
const int SIZE 4096;
/* name of the shared memory object */
const char *name = "OS";
/* shared memory file descriptor */
int shm fd;
/* pointer to shared memory obect */
void *ptr;
/* fork a child process */
pid = fork();
if (pid < 0) { /* error occurred */
fprintf(stderr, "Fork Failed");
return 1;
}
else if (pid == 0) { /* child process */
/* strings written to shared memory */
const char *message 0 = "Hello";
const char *message 1 = "World!";
/* create the shared memory object */
shm fd = shm open(name, O CREAT | O RDRW, 0666);
/* configure the size of the shared memory object */
ftruncate(shm fd, SIZE);
/* memory map the shared memory object */
ptr = mmap(0, SIZE, PROT WRITE, MAP SHARED, shm fd, 0);
/* write to the shared memory object */
sprintf(ptr,"%s",message 0);
ptr += strlen(message 0);
sprintf(ptr,"%s",message 1);
ptr += strlen(message 1);
}
else { /* parent process */
/* parent will wait for the child to complete */
wait(NULL);
/* open the shared memory object */
shm fd = shm open(name, O RDONLY, 0666);
/* memory map the shared memory object */
ptr = mmap(0, SIZE, PROT READ, MAP SHARED, shm fd, 0);
/* read from the shared memory object */
printf("%s",(char *)ptr);
/* remove the shared memory object */
shm unlink(name);
}
return 0;
}