linux c/c++ 编程之-----操作系统原理学习笔记
来源:互联网 发布:模糊算法讲解 编辑:程序博客网 时间:2024/05/20 22:39
操作系统是中断驱动的
创建进程:
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 10typedef 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()
- linux c/c++ 编程之-----操作系统原理学习笔记
- 学习Linux C编程之操作系统基础训练
- Linux+C学习笔记之IO编程
- Linux+C学习笔记之网络编程
- 嵌入式linux学习笔记之c编程基础
- Linux操作系统下C语言编程入门学习笔记(...20090601(第一遍学完待整理)...)
- 读书笔记之《Linux操作系统下C语言编程入门》
- Linux C编程一站式学习 笔记
- 《linux c编程一站式学习》笔记1
- 《linux c编程一站式学习》笔记2
- Linux.C编程一站式学习-笔记
- linux C编程学习笔记(一)
- 学习笔记-Linux C编程实践
- Linux C 编程学习笔记1-GCC
- 七月十三日 Linux操作系统c语言学习笔记
- 七月十四日 Linux操作系统c语言学习笔记
- 七月十六日 Linux操作系统c语言学习笔记
- 七月二十四日 Linux操作系统c语言学习笔记
- ORACLE EVENT -- Wait for scn ack
- C++ 拷贝构造函数和赋值构造函数
- python 中继承list类举例
- JEECG 开创新开发模式(即将发布版本)
- Geoprocessor.Clip(裁剪)
- linux c/c++ 编程之-----操作系统原理学习笔记
- WP Super Cache 停用。请前往 插件管理页启用缓存。
- 多个Market共用一个InfoWindow
- Struts2的注解功能
- 子游戏下载一半重新下载
- Windows安全补丁检
- 用git下载Android的某个应用程序
- 聚类(初探)
- Android学习篇章45-HttpClient网络下载-IO流传输-下载图片资源