操作系統是中斷驅動的
創建進程:
1.父進程與子進程併發執行
2.父進程等待子進程結束
子進程空間兩種情況:
1.子進程是父進程的複製品
2.子進程重新裝載另一端程序
父進程:fork 返回子進程pid>0 可用wait 等待子進程結束
子進程:fork 返回 pid =0 用execlp 裝載
兩個fork 的例子
int main() { pid_t pid; /* fork another process */ pid = fork(); if (pid < 0) { /* error occurred */ fprintf(stderr, "Fork Failed"); exit(-1); } else if (pid == 0) { /* child process */ execlp("/bin/ls", "ls", NULL); } else { /* parent process */ /* parent will wait for the child to complete */ wait (NULL); printf ("Child Complete"); exit(0); } }
#include <sys/types.h>
#include<stdio.h>
#include<unistd.h>
int value = 5;
int main()
{
pid_t pid;
pid = fork();
if (pid == 0)
value+=15;
else if (pid>0)
{
wait(NULL);
printf(“parent: value = %d”,value);
exit(0);
}
}
生產者-消費者 進程代碼(共享緩衝區)
Solution is correct, but can only use BUFFER_SIZE-1 elements
#define BUFFER_SIZE 10
typedef struct {
. . .
} item;
item buffer[BUFFER_SIZE];
int in = 0;//緩衝區下一個位置
int out = 0;//第一個非空緩衝區
生產者 inset()
while (true) { /* Produce an item */
while (((in + 1) % BUFFER SIZE count) == out)
; /* do nothing -- no free buffers */
buffer[in] = item;
in = (in + 1) % BUFFER SIZE;
}
消費者 remove ()
while (true) {
while (in == out)
; // do nothing -- nothing to consume
// remove an item from the buffer
item = buffer[out];
out = (out + 1) % BUFFER SIZE;
return item;
}
進程間通信
1.直接通信,明確的命名通信的發送者和接受者
send (P, message) – send a message to process P
receive(Q, message) – receive a message from process Q
非對稱編址
Send(p, message)Receive(id, message)
2.間接通信, 通過郵箱/端口發送接收
Each mailbox has a unique id
Processes can communicate only if they share a mailbox
create a new mailbox
send and receive messages through mailbox
destroy a mailbox
send(A, message) – send a message to mailbox A
receive(A, message) – receive a message from mailbox A
Mailbox sharing
P1, P2, and P3 share mailbox A
P1, sends; P2 and P3 receive
Who gets the message?
Allow a link to be associated with at most two processes
Allow only one process at a time to execute a receive operation
Allow the system to select arbitrarily the receiver. Sender is notified who the receiver was.
同步
Message passing may be either blocking or non-blocking
Blocking is considered synchronous
Blocking send makes the sender blocked until the message is received
Blocking receive makes the receiver blocked until a message is available
Non-blocking is considered asynchronous
Non-blocking send makes the sender to send the message and to continue
Non-blocking receive makes the receiver receive a valid message or null
緩衝
Queue of messages attached to the link; implemented in one of three ways
1. Zero capacity – 0 messages Sender must wait for receiver (rendezvous)
2. Bounded capacity – finite length of n messages Sender must wait if link full
3. Unbounded capacity – infinite length Sender never waits
線程
A running entity of a process, and a unit that can be scheduled independently.
A basic unit of CPU utilization
Resources still belong to process
Code section
Data section
Open files
signals
Thread is a running unit (smallest unit)
Thread has few resources (counter, register, stack), shares all the resources that the process has.
User Level Thread
Thread management done by user-level threads library
Kernel knows nothing about threads
Implementedby thread libraryCreate,cancellationTransferdata or messageSaveand recover the context of threadsThekernel manage the process, but know nothing about threadWhena thread have a system call, the process will be blocked. To thread library,the thread’s state is running
Kernel level thread
All threads are managed by the kernel
Create, cancellation and schedule
No thread library, but provide API
Kernel maintains context of threads and processes
The switch between threads needs the support of kernel
多線程問題;
Does fork() duplicate only the calling thread or all threads?
Exec() is after fork()
No exec() after fork()