diff --git a/published/20201005 How the Linux kernel handles interrupts.md b/published/20201005 How the Linux kernel handles interrupts.md new file mode 100644 index 0000000000..2abfe041e5 --- /dev/null +++ b/published/20201005 How the Linux kernel handles interrupts.md @@ -0,0 +1,111 @@ +[#]: collector: (lujun9972) +[#]: translator: (mengxinayan) +[#]: reviewer: (wxy) +[#]: publisher: (wxy) +[#]: url: (https://linux.cn/article-12965-1.html) +[#]: subject: (How the Linux kernel handles interrupts) +[#]: via: (https://opensource.com/article/20/10/linux-kernel-interrupts) +[#]: author: (Stephan Avenwedde https://opensource.com/users/hansic99) + +Linux 内核如何处理中断 +====== + +> 中断是计算机处理数据的关键部分。 + +![](https://img.linux.net.cn/data/attachment/album/202012/29/063805o5taf82ftgz85e22.jpg) + +中断是现代 CPU 工作方式中重要的部分。例如:当你每次在键盘上按下一个按键后,CPU 会被中断以使得 PC 读取用户键盘的输入。这个过程发生得相当快,以致于在使用体验上你不会感到任何变化或损害。 + +此外,键盘并不是导致中断的唯一组件。一般来说,有三种类型的事件会导致 CPU 发生中断:硬件中断、软件中断和异常。在具体介绍不同类型的中断前,我需要先定义一些术语。 + +### 定义 + +中断请求interrupt request(IRQ)是由可编程的中断控制器programmable interrupt controlle(PIC)发起的,其目的是为了中断 CPU 和执行中断服务程序interrupt service routine(ISR)。中断服务程序(ISR)是一个小的程序,用来处理具体的数据,其具体的处理方式依赖于造成中断请求(IRQ)的原因。之前正在运行的进程在中断服务程序(ISR)运行结束前都会被中断。 + +在过去,中断请求由单独的芯片处理(中断控制器芯片 PIC),I/O 设备直接与中断控制器(PIC)相连。中断控制器(PIC)管理着多种硬件的中断请求(IRQ),并且可以直接与 CPU 通信。当一个中断请求(IRQ)产生后,中断控制器(PIC)向 CPU 写入数据,并且触发中断请求引脚(INTR)。 + +现如今,中断请求(IRQ)由 CPU 中的高级可编程中断控制器advanced programmable interrupt controller(APIC)部分来处理。每个核中都拥有属于自己的高级可编程中断控制器。 + +### 中断的类型 + +正如我前文中提到的,中断可以根据其来源分为三种类型。 + +#### 硬件中断 + +当一个硬件设备想要告诉 CPU 某一需要处理的数据已经准备好后(例如:当键盘被按下或者一个数据包到了网络接口处),它将会发送一个中断请求(IRQ)来告诉 CPU 数据是可用的。接下来会调用在内核启动时设备驱动注册的对应的中断服务程序(ISR)。 + +#### 软件中断 + +当你在播放一个视频时,音频和视频是同步播放是相当重要的,这样音乐的速度才不会变化。这是由软件中断实现的,由精确的计时器系统(称为 [jiffies][2])重复发起的。这个计时器会使得你的音乐播放器同步。软件中断也可以被特殊的指令所调用,来读取或写入数据到硬件设备。 + +当系统需要实时性时(例如在工业应用中),软件中断会变得重要。你可以在 Linux 基金会的文章中找到更多相关信息:[面向嵌入式开发者的实时 Linux 介绍][3]。 + +#### 异常 + +异常exception是你可能之前就知道的中断类型。当 CPU 执行一些将会导致除零或缺页错误的指令时,任何其他运行中的程序都会被中断。在这种情况下,你会被一个弹窗提醒,或在控制台输出中看到**段错误segmentation fault核心已转储core dumped)**。但并不是所有异常都是由指令错误引起的。 + +异常可以进一步分为错误Fault陷阱Trap终止Abort。 + + * **错误**:错误是系统可以纠正的异常。例如当一个进程尝试访问某个已经被换出到硬盘的页时。当请求的地址在进程的地址空间中,并且满足访问权限时,如果页不在内存(RAM)中,将会产生一个中断请求(IRQ),并开始启用**缺页异常处理程序**把所需的页加载到内存中。如果操作成功执行,程序将继续运行。 + * **陷阱**:陷阱主要用在调试中。如果你在某个程序中设置了一个断点,你就插入了一条可以触发陷阱执行的特殊指令。陷阱可以触发上下文切换来允许你的调试器读取和展示局部变量的值。之后程序可以继续运行。陷阱同样也是运行系统调用的方式(如杀死一个进程) + * **终止**:终止是由系统表中的硬件错误或值不一致而导致的。终止不会报告造成异常的指令的所在位置。这是最严重的中断,终止将会调用系统的**终止异常处理程序**来结束造成异常的进程。 + +### 动手实践 + +中断请求按照高级可编程中断控制器(APIC)中的优先级高低排序(0是最高优先级)。前 32 个中断(0~31)是由 CPU 指定的固定序列。你可以在 [OsDev 异常][4] 页面找到关于它们的概述。随后的中断请求可以以不同的方式进行分配。中断描述表interrupt descriptor table(IDT)中记录了中断请求(IRQ)和中断服务程序(ISR)的对应关系。Linux 中定义了从 0 到 256 的 IRQ 向量。 + +为了打印出在你的系统中已注册的中断,打开一个终端并输入: + +``` +cat /proc/interrupts +``` + +你应该会看到类似如下图的结果: + +![注册的中断列表][5] + +*内核版本为5.6.6中注册的中断 (Stephan Avenwedde, [CC BY-SA 4.0][6])* + +从左到右各列的含义依次为:中断向量号、每个 CPU(0~n)中断发生次数、硬件来源、硬件源通道信息、以及造成中断请求的设备名。 + +在表的末尾,有一些非数字的中断。它们是特定于体系结构的中断,如本地计时器中断local timer interrupt(LOC)的中断请求(IRQ)号为 236。其中一些在 Linux 内核源树中的[Linux IRQ 向量布局][7]中指定。 + +![特定于体系结构的中断][8] + +*特定于体系结构的中断 (Stephan Avenwedde, [CC BY-SA 4.0][6])* + +如果要实时获取该表,请运行如下命令: + +``` +watch -n1 "cat /proc/interrupts" +``` + +### 总结 + +正确的中断请求(IRQ)处理对于硬件、驱动和软件的正常交互是必要的。幸运地是,Linux 内核很好地完成了它,一个 PC 的普通用户几乎不会注意到内核的整个中断处理过程。 + +中断相当复杂,本文仅仅是一个关于中断的概述。如果想要深入了解该主题可以阅读 [Linux Inside 电子书][9](CC BY-NC-SA 4.0)和 [Linux 内核教程][10] 仓库。 + +-------------------------------------------------------------------------------- + +via: https://opensource.com/article/20/10/linux-kernel-interrupts + +作者:[Stephan Avenwedde][a] +选题:[lujun9972][b] +译者:[萌新阿岩](https://github.com/mengxinayan) +校对:[wxy](https://github.com/wxy) + +本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出 + +[a]: https://opensource.com/users/hansic99 +[b]: https://github.com/lujun9972 +[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/car-penguin-drive-linux-yellow.png?itok=twWGlYAc (Penguin driving a car with a yellow background) +[2]: https://elinux.org/Kernel_Timer_Systems +[3]: https://www.linuxfoundation.org/blog/2013/03/intro-to-real-time-linux-for-embedded-developers/ +[4]: https://wiki.osdev.org/Exceptions +[5]: https://opensource.com/sites/default/files/uploads/proc_interrupts_1.png (Registered interrupts list) +[6]: https://creativecommons.org/licenses/by-sa/4.0/ +[7]: https://github.com/torvalds/linux/blob/master/arch/x86/include/asm/irq_vectors.h +[8]: https://opensource.com/sites/default/files/uploads/proc_interrupts_2.png (Architecture-specific interrupts) +[9]: https://0xax.gitbooks.io/linux-insides/content/Interrupts/ +[10]: https://linux-kernel-labs.github.io/refs/heads/master/lectures/interrupts.html# diff --git a/sources/tech/20201222 4 reasons businesses adopted open source in 2020.md b/sources/tech/20201222 4 reasons businesses adopted open source in 2020.md deleted file mode 100644 index 389adc0773..0000000000 --- a/sources/tech/20201222 4 reasons businesses adopted open source in 2020.md +++ /dev/null @@ -1,55 +0,0 @@ -[#]: collector: (lujun9972) -[#]: translator: (geekpi) -[#]: reviewer: ( ) -[#]: publisher: ( ) -[#]: url: ( ) -[#]: subject: (4 reasons businesses adopted open source in 2020) -[#]: via: (https://opensource.com/article/20/12/open-source-survey) -[#]: author: (Chris Grams https://opensource.com/users/cgrams) - -4 reasons businesses adopted open source in 2020 -====== -Differences emerge based on company size, according to Tidelift's third -managed open source survey. -![Metrics and a graph illustration][1] - -Companies are turning to open source during the pandemic, with 44% of organizations reporting they will increase their use of open source for application development, finds Tidelift's [third managed open source survey][2]. - -We've heard this lyric before; in previous recessions, organizations turned to open source [for cost savings][3] and stayed for its [other transformational benefits][4]. We wanted to understand which long-term benefits were most helpful to organizations of different sizes. Here's a summary of what we found. - -**Open source is driving cost and time savings while improving efficiency.** A key driver, cited by 68% of organizations, is saving money and development time, as using open source reduces the time developers spend writing new code from scratch. Almost half (48%) report that it increases app development and maintenance efficiency. Organizations with more than 1,000 employees are far more likely to cite this as a reason to encourage the use of more open source (61% vs. 41% for organizations with less than 1,000). - -![Graph showing reasons for using open source][5] - -(Tidelift ©2020) - -**Eliminating vendor lock-in is high on the list of reasons organizations are using more open source.** We found 40% of respondents cited this as a primary reason. Replacing expensive proprietary software with open source ensures that organizations can be more nimble and avoid dependency on vendors. Again, larger organizations favor this reason; 50% of organizations with over 1,000 employees identify this as a key benefit. - -**Increasing developer satisfaction is another reason to use more open source, with 31% of organizations citing it.** As companies compete fiercely for talent, they understand the value of ensuring developers are happy in their work and with the tools they use. The survey found that the top three languages developers are using are JavaScript (78%), Python (52%), and Java (41%). - -**Additionally, as open source usage increases, 83% of organizations continue to contribute to it, and nearly half have a policy in place to govern contributions**. These policies include contributions during work hours to projects an organization uses but doesn't sponsor or manage; contributions to projects they do sponsor or manage; contributions during personal time to non-job-related (personal) projects; and contributions during work hours to non-job-related (personal) projects. - -While the long-term migration towards open source continues, it's clear that the impact of COVID-19 may be accelerating this process, and organizations continue to glean deeper value from using and contributing to it. - -For more information, view all findings from the [2020 Managed Open Source Survey][2]. - -Developing your organization's story—the narrative that gives it its current identity and charts... - --------------------------------------------------------------------------------- - -via: https://opensource.com/article/20/12/open-source-survey - -作者:[Chris Grams][a] -选题:[lujun9972][b] -译者:[译者ID](https://github.com/译者ID) -校对:[校对者ID](https://github.com/校对者ID) - -本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出 - -[a]: https://opensource.com/users/cgrams -[b]: https://github.com/lujun9972 -[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/metrics_graph_stats_blue.png?itok=OKCc_60D (Metrics and a graph illustration) -[2]: https://www.tidelift.com/subscription/2020-managed-open-source-survey -[3]: https://blog.tidelift.com/the-third-wave-of-open-source-migration?utm_source=opensource&utm_medium=referral&utm_campaign=2020-survey -[4]: https://blog.tidelift.com/theres-one-thing-stopping-developers-from-using-open-source-even-more?utm_source=opensource&utm_medium=referral&utm_campaign=2020-survey -[5]: https://opensource.com/sites/default/files/uploads/tidelift_reasons-for-using-open-source.png (Graph showing reasons for using open source) diff --git a/sources/tech/20201228 Learn Python by coding a simple game.md b/sources/tech/20201228 Learn Python by coding a simple game.md new file mode 100644 index 0000000000..c934b19c2c --- /dev/null +++ b/sources/tech/20201228 Learn Python by coding a simple game.md @@ -0,0 +1,114 @@ +[#]: collector: (lujun9972) +[#]: translator: (geekpi) +[#]: reviewer: ( ) +[#]: publisher: ( ) +[#]: url: ( ) +[#]: subject: (Learn Python by coding a simple game) +[#]: via: (https://opensource.com/article/20/12/learn-python) +[#]: author: (Moshe Zadka https://opensource.com/users/moshez) + +Learn Python by coding a simple game +====== +Explore Python (and other programming languages) by writing a "guess the +number" game. +![Python programming language logo with question marks][1] + +In this series, we're writing the same application in different programming languages to compare how various languages work and to show how using a standard test program is a great way to learn new ways to program. + +When you learn a new programming language, it's good to focus on the things they have in common. Variables, expressions, and statements are the basis of most programming languages. Once you understand these concepts, you can start figuring the rest out. + +Because programming languages share many similarities, once you know one, you can usually learn the basics of another by looking at how it differs from what you know. Using a standard test program you've written in other languages enables you to focus on the language, not the program's logic. + +To prove this point, we're testing how to write a "guess the number" program in multiple languages. The computer picks a number between one and 100 and asks you to guess it. The program loops until you guess the correct answer. + +The "guess the number" program exercises several concepts in programming languages: + + * Variables + * Input + * Output + * Conditional evaluation + * Loops + + + +It's a great practical experiment to learn a new programming language. + +### Guess the number in Python + +In the words of the [Python Software Foundation][2]: "Python is an interpreted, interactive, object-oriented programming language. It incorporates modules, exceptions, dynamic typing, very high-level dynamic data types, and classes." It's a great general-purpose programming language that's useful for everything from simple scripts to complex GUI applications. + +You can explore Python by writing a version of the "guess the number" game. Here is my implementation: + + +``` +import random as randomlib +random = randomlib.randint(1, 100) +print("Guess a number between 1 and 100") +while True: +    guess = int(input()) +    if guess < random: +        print("Too low") +    elif guess > random: +        print("Too high") +    else: +        print("That's right!") +        break +``` + +To assign a value to a variable, list the variable's name, followed by the `=` sign. For example, the statement `random = 0` assigns a zero value to the `random` variable. + +The first line of the script imports the `random` module. Since all the programs in this series use `random` as the name of the variable, you can use `import random as randomlib` to import it under an alias to avoid name conflicts. + +Few functions are built into Python; most have to be imported explicitly from the standard library. The `random` standard library module has the functionality to generate all kinds of random values. + +The second line of the script reads the result of the function `randint()` and assigns it to the variable called `random`. The function takes two arguments: a minimum and a maximum. In this case, the range is `1` to `100`, making the game just challenging enough. + +You can also prompt the user to enter a value using the `input()` function. If you write `guess = int(input())`, Python waits for the user to enter some text, converts it to an integer, and then stores that value in the guess variable. + +Python supports conditional expressions and flow control like loops. In the "guess the number" game, Python continues looping as long as the value in guess is not equal to `random`. + +If the guess is less than the random number, Python prints `Too low`, and if the guess is greater than the number, Python prints `Too high`. + +### Sample output + +Now that you've written your Python program, run it to play the "guess the number" game. Every time you run the program, Python will pick a different random number. To complete this game, you need to guess until you find the correct number: + + +``` +$ python guess.py +Guess a number between 1 and 100 + 50 +Too high + 25 +Too high + 12 +Too high + 7 +Too high + 3 +Too low + 5 +Too low + 6 +That's right! +``` + +This "guess the number" game is a great introductory program when learning a new programming language because it exercises several common programming concepts in a pretty straightforward way. By implementing this simple game in different programming languages, you can demonstrate some core concepts of different programming languages and compare each language's details. + +Do you have a favorite programming language? How would you write the "guess the number" game? Follow this article series to see examples of other programming languages that might interest you! + +-------------------------------------------------------------------------------- + +via: https://opensource.com/article/20/12/learn-python + +作者:[Moshe Zadka][a] +选题:[lujun9972][b] +译者:[译者ID](https://github.com/译者ID) +校对:[校对者ID](https://github.com/校对者ID) + +本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出 + +[a]: https://opensource.com/users/moshez +[b]: https://github.com/lujun9972 +[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/python_programming_question.png?itok=cOeJW-8r (Python programming language logo with question marks) +[2]: https://docs.python.org/3/faq/general.html#general-information diff --git a/sources/tech/20201228 Learn to use the JOE text editor on Linux.md b/sources/tech/20201228 Learn to use the JOE text editor on Linux.md new file mode 100644 index 0000000000..741d7cb5a2 --- /dev/null +++ b/sources/tech/20201228 Learn to use the JOE text editor on Linux.md @@ -0,0 +1,76 @@ +[#]: collector: (lujun9972) +[#]: translator: ( ) +[#]: reviewer: ( ) +[#]: publisher: ( ) +[#]: url: ( ) +[#]: subject: (Learn to use the JOE text editor on Linux) +[#]: via: (https://opensource.com/article/20/12/joe) +[#]: author: (Seth Kenlon https://opensource.com/users/seth) + +Learn to use the JOE text editor on Linux +====== +Joe's Own Editor (JOE) is a general text editor that is easy to learn +and use. +![Person programming on a laptop on a building][1] + +I’m a fan of text editors that make it easy for you to learn how to use them. GNU nano is my favorite example of this: you launch nano, and you see a list of the most common commands along the bottom of the window throughout your entire session. Joe’s Own Editor (that’s `joe` for short) is another great example. + +The `joe` editor uses a terminal-based interface, providing simple text editing capabilities and an easily accessible help screen. It’s written in C, it’s small with a hard dependency only on libc (ncurses is optional), and it’s licensed under the GPL. + +### Installing + +On Linux, you may be able to find JOE in your distribution’s software repository. It’s a little obscure, though, so not all distributions have packaged it. If that’s the case, you can download the source code from [SourceForge][2] and compile it yourself. It’s an easy process: + + +``` +$ ./configure +$ make +$ sudo make install +``` + +### Using JOE + +According to its author, JOE takes inspiration from an application called WordStar and from [GNU Emacs][3]. Most of its basic editing keys are the same as WordStar shortcuts, and the editor itself strives to approximate WordStar. JOE also has some of the key bindings and features of GNU Emacs. This mix of two sources of inspiration can sometimes be disorienting, but then again, weaning yourself off Emacs (or your usual text editor of choice) under any circumstance can be disorienting. The important thing is that JOE offers help, and it’s easy to reach. + +In the top right corner of JOE’s interface, there’s a persistent reminder that you can press **Ctrl+K** followed immediately by **H** to view a help screen. This is a toggle, so once you activate it, the help screen remains displayed at the top of your editor window until dismissed with the same key combo (**^KH** in JOE’s keyboard notation). + +### Keyboard shortcuts + +For all the pride JOE’s author takes in simulating a WordStar user experience, I have to admit it’s lost on me. I’d never heard of WordStar until I read about it in JOE’s documentation, and to me, it seemed to have a completely arbitrary keyboard shortcut scheme. Some are prefixed with **Ctrl+K** as an escape sequence, while others use **Esc** as the prefix, and still others require no escape sequence at all. I couldn’t determine the logic. Editing operations were just as likely as application options to require an escape sequence or not, and few of the letter associations made sense to me (**Ctrl+K** **D** for **save as**, for instance). + +Luckily, JOE lets you cheat on keyboard shortcuts. Upon installation, JOE quietly creates a few symlinks for the `joe` executable. Included are: + + * `jmacs` JOE with Emacs key bindings + * `jpico` JOE with Pico (or GNU nano) key bindings + * `rjoe` JOE with editing restricted only to the file passed to JOE at launch + * `jstar` JOE with WordStar key bindings + + + +![Black terminal with white text showing WordStar key bindings][4] + +The persistent help menu remains, so if you’re ever unclear as to what functions you have available to you, refer to them with the help prompt at the top right of the screen. Of course, in Pico/nano mode, the main commands are always visible. + +### Try JOE + +JOE isn’t intended as a text editor you’ll never leave. It won’t become your IDE, email client, web browser, and file manager. In fact, it may not even be your main text editor for all tasks. It focuses on being good at one thing, and that’s general text editing. + +JOE has all of the essential functions you need, like the ability to navigate your text quickly, the ability to select text, copy and paste, and so on. Give JOE a try, using your preferred symlink. + +-------------------------------------------------------------------------------- + +via: https://opensource.com/article/20/12/joe + +作者:[Seth Kenlon][a] +选题:[lujun9972][b] +译者:[译者ID](https://github.com/译者ID) +校对:[校对者ID](https://github.com/校对者ID) + +本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出 + +[a]: https://opensource.com/users/seth +[b]: https://github.com/lujun9972 +[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/computer_code_programming_laptop.jpg?itok=ormv35tV (Person programming on a laptop on a building) +[2]: https://joe-editor.sourceforge.io/ +[3]: https://opensource.com/article/20/12/emacs +[4]: https://opensource.com/sites/default/files/uploads/joe-jstar-31_days-joe-opensource.png (Black terminal with white text showing WordStar key bindings) diff --git a/sources/tech/20201228 Practice programming in C-- by writing a simple game.md b/sources/tech/20201228 Practice programming in C-- by writing a simple game.md new file mode 100644 index 0000000000..6ddf918a41 --- /dev/null +++ b/sources/tech/20201228 Practice programming in C-- by writing a simple game.md @@ -0,0 +1,175 @@ +[#]: collector: (lujun9972) +[#]: translator: ( ) +[#]: reviewer: ( ) +[#]: publisher: ( ) +[#]: url: ( ) +[#]: subject: (Practice programming in C++ by writing a simple game) +[#]: via: (https://opensource.com/article/20/12/learn-c-game) +[#]: author: (Seth Kenlon https://opensource.com/users/seth) + +Practice programming in C++ by writing a simple game +====== +The C++ language is complex, but it can teach you a lot about data +types, memory management, and code linking. +![A bunch of question marks][1] + +There are a couple of ways to learn a programming language. If you're new to coding, you usually learn some basic computer coding concepts and try to apply them. If you already know how to code in another language, you relearn how coding concepts are expressed in the new language. + +In either case, a convenient way to learn these new principles is to create a simple guessing game. This forces you to understand how a language receives input and sends output, how it compares data, how to control a program's flow, and how to leverage conditionals to affect an outcome. It also ensures that you know how a language structures its code; for instance, Lua or [Bash][2] can easily run as a script, while [Java][3] requires you to create a class. + +In this article, I'll demonstrate how to implement a guessing game for the terminal in [C++][4]. + +### Install dependencies + +To follow along with this article, you need C++ and a compiler. + +You can get everything you need on Linux by installing the Qt Creator IDE from your distribution's software repository. + +On Fedora, CentOS, or RHEL: + + +``` +`$ sudo dnf install qt-creator` +``` + +On Debian, Ubuntu, Chromebook, or similar: + + +``` +`$ sudo apt install qtcreator` +``` + +This article doesn't utilize the Qt Creator IDE, but it's an easy way to get everything you need installed, and for complex C++ projects (including those with a GUI), it's an essential tool to have. On macOS or Windows, follow the [installation instructions][5] on Qt's website. + +### Set up includes and namespace + +C++'s core language is minimal. Even a simple application requires the use of additional libraries. This application uses [iostream][6] to gain access to the `cout` and `cin` keywords. + +Also, ensure that the program uses the `std` namespace: + + +``` +#include <iostream> + +using namespace std; +``` + +This isn't strictly necessary, but without setting the namespace to `std`, all keywords from the iostream library require a namespace prefix. For instance, instead of writing `cout`, I would have to write `std::cout`. + +Statements in C++ terminate with a semicolon. + +### Create a function + +Every C++ application requires at least one function. The primary function of a C++ application must be called `main`, and it must return an integer (`int`), which corresponds to the [POSIX][7] expectation that a process returns 0 upon success and something else upon failure. You create a new function by providing its return type and then its name: + + +``` +int main() { + // code goes here +} +``` + +### Implement program logic + +The game code must first produce a random number for the player to guess. You do this in C++ by establishing a _seed_ for pseudo-random number generation. A simple seed is the current time. Once the seed starts, you retrieve a number between 1 and 100 by calling the `rand` function with an upper constraint of 100. This generates a random number from 0 to 99, so add 1 to whatever number is chosen and assign the result to a variable called `number`. You must also declare a variable to hold the player's guess. For clarity, I'm calling this variable `guess`. + +This sample code also includes a debug statement that tells you exactly what the random number is. This isn't very good for a guessing game, but it makes testing a lot faster. Later, you can remove the line or just comment it out by prefacing it with `//`: + + +``` + srand (time(NULL)); + int number = rand() % 100+1; + int guess = 0; + + cout << number << endl; //debug +``` + +### Add do-while and if statements + +A `do-while` statement in C++ starts with the keyword `do` and encloses everything that you want C++ to do in braces. Close the statement with the `while` keyword followed by the condition that must be met (in parentheses): + + +``` +do { + // code here +} while ( number != guess ); +``` + +The game code occurs within an `if` statement with an `else if` and `else` statements to provide the player with hints. + +First, prompt the player for a guess with a `cout` statement. The `cout` function prints output onto `stdout`. Because the `cout` statement isn't appended with the `endl` (endline) function, no linebreak occurs. Immediately following this `cout` statement, tell C++ to wait for input by using the `cin` function. As you might surmise, `cin` waits for input from `stdin`. + +Next, the program enters the `if` control statement. If the player's guess is greater than the pseudo-random number contained in the `number` variable, then the program prints out a hint followed by a newline character. This breaks the `if` statement, but C++ is still trapped within the `do-while` loop because its condition (the `number` variable being equal to `guess`) has not yet been met. + +If the player's guess is less than the pseudo-random number contained in the `number` variable, then the program prints out a hint followed by a newline character. This again breaks the `if` statement, but the program remains trapped within the `do-while` loop. + +When `guess` is equal to `number`, the key condition is finally met, the `else` statement is triggered, the `do-while` loop ends, and the application ends: + + +``` +do { +  cout << "Guess a number between 1 and 100: "; +  cin >> guess; + +  if ( guess > number) { cout << "Too high.\n" << endl; } +  else if ( guess < number ) { cout << "Too low.\n" << endl; } +  else { +    cout << "That's right!\n" << endl; +    exit(0); +  } // fi + } while ( number != guess ); +return 0; +} // main +``` + +### Building the code and playing the game + +You can build your application with GCC: + + +``` +`$ g++ -o guess.bin guess.cpp` +``` + +Run the binary to try it out: + + +``` +$ ./guess.bin +74 +Guess a number between 1 and 100: 76 +Too high. + +Guess a number between 1 and 100: 1 +Too low. + +Guess a number between 1 and 100: 74 +That's right! +``` + +Success! + +### Give C++ a try + +The C++ language is complex. Writing C++ applications for terminals can teach you a lot about data types, memory management, and code linking. Try writing a useful utility in C++ and see what you can discover! + +-------------------------------------------------------------------------------- + +via: https://opensource.com/article/20/12/learn-c-game + +作者:[Seth Kenlon][a] +选题:[lujun9972][b] +译者:[译者ID](https://github.com/译者ID) +校对:[校对者ID](https://github.com/校对者ID) + +本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出 + +[a]: https://opensource.com/users/seth +[b]: https://github.com/lujun9972 +[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/BIZ_question_B.png?itok=f88cyt00 (A bunch of question marks) +[2]: https://opensource.com/article/20/12/learn-bash +[3]: https://opensource.com/article/20/12/learn-java-writing-guess-number-game +[4]: https://www.cplusplus.com/ +[5]: https://www.qt.io/product/development-tools +[6]: http://www.cplusplus.com/reference/iostream/ +[7]: https://opensource.com/article/19/7/what-posix-richard-stallman-explains diff --git a/sources/tech/20201229 Rocket.Chat- An Amazing Open-Source Alternative to Slack That You Can Self-host.md b/sources/tech/20201229 Rocket.Chat- An Amazing Open-Source Alternative to Slack That You Can Self-host.md new file mode 100644 index 0000000000..abb88aeb25 --- /dev/null +++ b/sources/tech/20201229 Rocket.Chat- An Amazing Open-Source Alternative to Slack That You Can Self-host.md @@ -0,0 +1,119 @@ +[#]: collector: (lujun9972) +[#]: translator: ( ) +[#]: reviewer: ( ) +[#]: publisher: ( ) +[#]: url: ( ) +[#]: subject: (Rocket.Chat: An Amazing Open-Source Alternative to Slack That You Can Self-host) +[#]: via: (https://itsfoss.com/rocket-chat/) +[#]: author: (Ankush Das https://itsfoss.com/author/ankush/) + +Rocket.Chat: An Amazing Open-Source Alternative to Slack That You Can Self-host +====== + +_**Brief: Rocket.Chat is an open-source team communication application with features and looks similar to Slack. You are free to self-host it or opt for their managed service for a fee.**_ + +Slack is a useful and popular team communication app that potentially replaces emails for work. A lot of big and small teams use it, even we at It’s FOSS relied on Slack initially. + +However, we needed a good open-source alternative to Slack and that’s when we came across Rocket.Chat. Sure, there are several other [open-source slack alternatives][1], but we opted for Rocket.Chat for its similarity with Slack and ease of deployment. + +### Rocket.Chat: An Open Source Communication Platform + +![][2] + +Rocket.Chat is an open-source communication platform for team collaboration. + +You get all the essential features to facilitate proper communication along with the option to get started for free, opt for hosted service by the Rocket.Chat team or deploy it on your server. + +You can totally customize as per your requirements when deploying it on your server. No matter what you choose to do, the feature-set is impressive. + +Let us take a look at what it offers. + +### Features of Rocket.Chat + +![][3] + +Rocket.Chat is a powerful and flexible team communication tool. Here’s what you can expect from it: + + * Easy file sharing (drag and drop support) + * Audio file sharing support + * Video conferencing with [Jitsi Meet][4] integration + * Separate channels (private and public options) + * End-to-End encryption support + * Customize the theme of the service (including the ability to customize it) + * Guest access support + * Unlimited message history (depending on the storage of your server for self-managed setup) + * Broadcast channel support + * RSS Integration + * Several 3rd party app integration support + * White label (optional if you want a custom branding) + * Read receipt (Enterprise plan) + * Push notifications support + * Customizable user permission + * 24 x 7 Support (depending on the pricing plan) + * LiveChat integration support which you can add on your website + * Real-time translation + * Self-host support + * Cross-platform support (Windows, macOS, Android, iOS, and Linux) + + + +In addition to all the key points mentioned above, there are a lot of little nifty features that should come in useful in Rocket.Chat. + +### Installing Rocket.Chat client on Linux + +If you have a Rocket.Chat instance deployed or hosted by Rocket Chat itself, you can access it through web browser, desktop clients and mobile apps. + +Can’t self-host Rocket.Chat? Let us help you + +Deploying open source applications and managing Linux servers takes some expertise and time. If you lack either but still want to have your own instance of open source software, we can help you out. +With our new project, [High on Cloud][5], you can leave the deployment and server management part to us while you focus on your work. + +On Linux, Rocket.Chat is available as a [snap][6] and a [Flatpak package][7]. You can go through our guides on [using snap][6] or [Flatpak on Linux][8] to get started. + +I would recommend installing it as a Flatpak (that’s how I use it) to get the latest version. Of course, if you prefer to use it as a snap package, you can go with that as well. + +In either case, you can explore the source code on their [GitHub page][9] if you need. + +[Rocket.Chat][10] + +### My Thoughts on Using Rocket.Chat + +![][11] + +I’ve been using Rocket.Chat for quite a while now (for our internal communication at It’s FOSS). Even though I was not the one who deployed it on our server, the [documentation][12] hints at a swift process to set it up on your server. + +It supports automation tools like [Ansible][13], [Kubernetes][14], etc and also gives you the option to deploy it as a docker container directly. + +You will find plenty of administrative options to tweak the experience on your instance of Rocket.Chat. It is easy to customize many things even if you are not an expert at self-managed projects. + +Personally, I appreciate the ability to customize the theme (it is easy to add a dark mode toggle as well). You get all the essential options available on smartphone as well. Overall, it is indeed an exciting switch from Slack and it should be a similar experience for most of you. + +_What do you think about Rocket.Chat? Do you prefer something else over Rocket.Chat? Let me know your thoughts in the comments below._ + +-------------------------------------------------------------------------------- + +via: https://itsfoss.com/rocket-chat/ + +作者:[Ankush Das][a] +选题:[lujun9972][b] +译者:[译者ID](https://github.com/译者ID) +校对:[校对者ID](https://github.com/校对者ID) + +本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出 + +[a]: https://itsfoss.com/author/ankush/ +[b]: https://github.com/lujun9972 +[1]: https://itsfoss.com/open-source-slack-alternative/ +[2]: https://i1.wp.com/itsfoss.com/wp-content/uploads/2020/12/rocket-chat-feat.png?resize=768%2C433&ssl=1 +[3]: https://i0.wp.com/itsfoss.com/wp-content/uploads/2020/12/rocket-chat-itsfoss-1.resized.jpg?resize=800%2C509&ssl=1 +[4]: https://itsfoss.com/jitsi-meet/ +[5]: https://highoncloud.com/ +[6]: https://itsfoss.com/use-snap-packages-ubuntu-16-04/ +[7]: https://itsfoss.com/what-is-flatpak/ +[8]: https://itsfoss.com/flatpak-guide/ +[9]: https://github.com/RocketChat/Rocket.Chat +[10]: https://rocket.chat/ +[11]: https://i2.wp.com/itsfoss.com/wp-content/uploads/2020/12/rocket-chat-itsfoss.png?resize=800%2C504&ssl=1 +[12]: https://docs.rocket.chat/ +[13]: https://www.ansible.com/ +[14]: https://kubernetes.io/ diff --git a/translated/tech/20201005 How the Linux kernel handles interrupts.md b/translated/tech/20201005 How the Linux kernel handles interrupts.md deleted file mode 100644 index 5a74ed8922..0000000000 --- a/translated/tech/20201005 How the Linux kernel handles interrupts.md +++ /dev/null @@ -1,116 +0,0 @@ -[#]: collector: (lujun9972) -[#]: translator: () -[#]: reviewer: ( ) -[#]: publisher: ( ) -[#]: url: ( ) -[#]: subject: (How the Linux kernel handles interrupts) -[#]: via: (https://opensource.com/article/20/10/linux-kernel-interrupts) -[#]: author: (Stephan Avenwedde https://opensource.com/users/hansic99) - -Linux 内核如何处理中断 -====== - -中断是计算机处理数据的关键部分。 - -![企鹅驾驶着黄色背景的汽车][1] - -中断是现代CPU工作中必不可少的一部分。例如:当你每次在键盘上按下一个按键后,CPU会被中断以使得PC读取用户键盘的输入。这个过程发生得相当快以致于在使用体验上你不会感到任何变化或损害。 - -此外,键盘并不是导致中断的唯一元素。一般来说,有三种类型的事件会导致CPU发生中断:硬件中断、软件中断和异常。再具体介绍不同类型的中断前,我需要先定义一些术语。 - -### 定义 - -一个中断请求(**IRQ**)是被可编程的中断控制器(**PIC**)发起,其目的是为了中断CPU和执行中断服务程序(**ISR**)。中断服务程序(ISR)是一个小的程序来处理具体的数据,其具体的处理方式依赖于造成中断请求(IRQ)的原因。之前正在运行的进程在中断服务程序(ISR)运行结束前都会被中断。 - -在过去,中断请求由单独的芯片处理(中断控制器芯片),I/O 设备与中断控制器(PIC)直接相连。中断控制器(PIC)管理着多种硬件的中断请求并且可以直接与CPU通信。当一个中断请求(IRQ)产生后,中断控制器向CPU写入数据并且触发中断请求(**INTR**)引脚。 - -现如今,中断请求(IRQ)由CPU中的高级可编程中断控制器(APIC)部分来处理。每个核中都拥有属于自己的高级可编程中断控制器。 - -### 中断的类型 - -正如我前文中提到的,中断可以根据其来源分为三种类型。 - -#### 硬件中断 - -当一个硬件设备想要告诉CPU某一需要处理的数据已经准备好后(例如:当键盘被按下或者一个数据包到了网络接口处),它将会发送一个中断请求(IRQ)来告诉CPU数据是可用的。接下来会调用在内核启动时设备驱动注册的对应的中断服务程序(ISR)。 - -#### 软件中断 - -当你在播放一个视频时,音频和视频是同步播放是相当重要的,因此音频的播放速度不应该发生变化。这是由软件中断实现的,由精确的计时器系统(称为 [jiffies][2])重复发起的。这个计时器会使得你的音乐播放器同步。软件中断也可以被特殊的指令所调用来读取或写入数据到硬件设备。 - -当系统需要实时性时(例如在工业应用中),软件中断会变得重要。你可以在Linux基金会的文章中找到更多相关信息:[面向嵌入式开发者的实时Linux介绍][3]。 - -#### 异常 - -异常是你可能之前知道的中断类型。当CPU执行一些将会导致除以0或缺页错误的指令时,任何其他运行中的程序都会被中断。在这种情况下,你会被一个弹窗提醒,或在控制台输出中看到**段错误(核心已转存)**。但并不是所有异常都是由指令错误引起的。 - -异常可以进一步分为 _错误_、_陷阱_ 和 _中止_ ? - -Exceptions can be further divided into _Faults_, _Traps_, and _Aborts_. - - * **错误:** 错误是系统可以纠正的异常。例如当一个进程尝试访问某个已经被换出到硬盘的页时。当请求的地址在进程的地址空间中,并且满足访问权限时,如果页不在内存(RAM)中,将会产生一个中断请求(IRQ),并开始启用**缺页异常处理程序**把所需的页加载到内存中。如果操作成功执行,程序将继续运行。 - - * **陷阱:** 陷阱主要用在调试中。如果你在某个程序中设置了一个断点,你就插入了一条可以触发陷阱执行的指令。一个陷阱可以触发上下文切换来允许你的调试器读取和展示局部变量的值。之后程序可以继续运行。陷阱同样也是运行系统调用的方式(如杀死一个进程) - - * **中止:** 中止是由系统表中的硬件错误或值不一致而导致的。一个中止不会报告造成异常的指令的所在位置。这是最严重的中断,一个中止将会调用系统的**中止异常处理程序**来结束造成异常的进程。 - -### 动手实践 - -中断请求按照高级可编程中断控制器(APIC)中的优先级高低排序(0是最高优先级)。前32个中断(0~31)是由CPU指定的固定序列。你可以在 [OsDev 异常][4] 页面找到关于它们的概述。接下来的中断请求可以不同的分配。中断描述表(**IDT**)中记录了中断请求(IRQ)和中断服务程序(ISR)的对应关系。Linux 中定义了从 0 到 256 的 IRQ 向量。 - -为了打印出在你的系统中已注册的中断,打开一个终端并输入: - -``` -`cat /proc/interrupts` -``` - -你应该会看到类似如下图的结果: - -![注册的中断列表][5] - -内核版本为5.6.6中注册的中断 (Stephan Avenwedde, [CC BY-SA 4.0][6]) - -从左到右各列的含义依次为:中断向量号、每个CPU(0~n)中断发生次数、硬件来源、硬件源通道信息、以及造成中断请求的设备名。 - -在表的末尾,有一些非数字的中断。它们是特定于体系结构的中断,如本地计时器中断(**LOC**)的中断请求(IRQ)号为236。其中一些在 Linux 内核源树中的[Linux IRQ 向量布局][7]中指定。 - -![特定于体系结构的中断][8] - -特定于体系结构的中断 (Stephan Avenwedde, [CC BY-SA 4.0][6]) - - -如果要实时获取该表,请运行如下命令: - -``` -`watch -n1 "cat /proc/interrupts"` -``` - -### 总结 - -正确的中断请求(IRQ)处理对于硬件、驱动和软件的正常交互是必要的。幸运地是,Linux 内核很好地完成了它,一个PC的普通用户很难注意到内核整个中断处理的过程。 - -中断相当复杂,本文仅仅是一个关于中断的概述。如果想要深入了解该主题可以阅读 [Linux 内部电子书][9](CC BY-NC-SA 4.0)和 [Linux 内核教程][10] 仓库。 - --------------------------------------------------------------------------------- - -via: https://opensource.com/article/20/10/linux-kernel-interrupts - -作者:[Stephan Avenwedde][a] -选题:[lujun9972][b] -译者:[萌新阿岩](https://github.com/mengxinayan) -校对:[校对者ID](https://github.com/校对者ID) - -本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出 - -[a]: https://opensource.com/users/hansic99 -[b]: https://github.com/lujun9972 -[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/car-penguin-drive-linux-yellow.png?itok=twWGlYAc (Penguin driving a car with a yellow background) -[2]: https://elinux.org/Kernel_Timer_Systems -[3]: https://www.linuxfoundation.org/blog/2013/03/intro-to-real-time-linux-for-embedded-developers/ -[4]: https://wiki.osdev.org/Exceptions -[5]: https://opensource.com/sites/default/files/uploads/proc_interrupts_1.png (Registered interrupts list) -[6]: https://creativecommons.org/licenses/by-sa/4.0/ -[7]: https://github.com/torvalds/linux/blob/master/arch/x86/include/asm/irq_vectors.h -[8]: https://opensource.com/sites/default/files/uploads/proc_interrupts_2.png (Architecture-specific interrupts) -[9]: https://0xax.gitbooks.io/linux-insides/content/Interrupts/ -[10]: https://linux-kernel-labs.github.io/refs/heads/master/lectures/interrupts.html# diff --git a/translated/tech/20201222 4 reasons businesses adopted open source in 2020.md b/translated/tech/20201222 4 reasons businesses adopted open source in 2020.md new file mode 100644 index 0000000000..95321f3d5d --- /dev/null +++ b/translated/tech/20201222 4 reasons businesses adopted open source in 2020.md @@ -0,0 +1,52 @@ +[#]: collector: (lujun9972) +[#]: translator: (geekpi) +[#]: reviewer: ( ) +[#]: publisher: ( ) +[#]: url: ( ) +[#]: subject: (4 reasons businesses adopted open source in 2020) +[#]: via: (https://opensource.com/article/20/12/open-source-survey) +[#]: author: (Chris Grams https://opensource.com/users/cgrams) + +2020 年企业采用开源的 4 个原因 +====== +根据 Tidelift 的第三次托管开源调查,根据公司规模,出现了差异。 +![Metrics and a graph illustration][1] + +Tidelift 的[第三次托管开源调查][2]发现,企业在大流行期间正在转向开源,44% 的组织报告他们将增加使用开源进行应用开发。 + +我们以前见过类似现象。在以前的经济衰退中,组织转向开源[以节省成本][3],并因其[其他转型效益][4]而留下来。我们想了解哪些长期利益对不同规模的组织最有帮助。以下是我们发现的摘要。 + +**开源正在推动成本和时间的节约,同时提高效率。**68% 的组织提到的一个关键驱动力是节约资金和开发时间,因为使用开源减少了开发人员从头开始编写新代码的时间。近半数 (48%) 报告称,它提高了应用开发和维护效率。拥有超过 1000 名员工的组织更有可能将此作为鼓励使用更多开源的原因(61%,而少于 1000 人的组织为 41%)。 + +![Graph showing reasons for using open source][5] + +(Tidelift ©2020) + +**在组织使用更多的开源的原因中,消除供应商锁定是一个重要原因。** 我们发现 40% 的受访者将这视为主要原因。用开源软件取代昂贵的专有软件,可以确保组织更加灵活,避免对供应商的依赖。同样,规模较大的组织也倾向于这个原因。在拥有 1000 名以上员工的组织中,有 50% 的组织将此作为主要优势。 + +**增加开发人员的满意度是使用更多开源的另一个原因,有 31% 的组织提到了这一点.**随着企业对人才的激烈竞争,他们了解确保开发人员在工作中和使用的工具中感到快乐的价值。调查发现,开发人员使用的前三种语言是 JavaScript(78%)、Python(52%)和 Java(41%)。 + +**此外,随着开源使用量的增加,83% 的组织继续对其贡献,近一半的组织制定了管理贡献的政策**。这些政策包括:在工作时间对组织使用但不赞助或管理的项目的贡献、对他们赞助或管理的项目的贡献、在个人时间对与工作无关的(个人)项目的贡献、以及在工作时间对与工作无关的(个人)项目的贡献。 + +虽然向开源的长期迁移仍在继续,但很明显,COVID-19 的影响可能正在加速这一进程,组织继续从使用和贡献中获得更深层次的价值。 + +更多信息,请查看 [2020 年托管开源调查][2]的所有调查结果。 + +-------------------------------------------------------------------------------- + +via: https://opensource.com/article/20/12/open-source-survey + +作者:[Chris Grams][a] +选题:[lujun9972][b] +译者:[geekpi](https://github.com/geekpi) +校对:[校对者ID](https://github.com/校对者ID) + +本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出 + +[a]: https://opensource.com/users/cgrams +[b]: https://github.com/lujun9972 +[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/metrics_graph_stats_blue.png?itok=OKCc_60D (Metrics and a graph illustration) +[2]: https://www.tidelift.com/subscription/2020-managed-open-source-survey +[3]: https://blog.tidelift.com/the-third-wave-of-open-source-migration?utm_source=opensource&utm_medium=referral&utm_campaign=2020-survey +[4]: https://blog.tidelift.com/theres-one-thing-stopping-developers-from-using-open-source-even-more?utm_source=opensource&utm_medium=referral&utm_campaign=2020-survey +[5]: https://opensource.com/sites/default/files/uploads/tidelift_reasons-for-using-open-source.png (Graph showing reasons for using open source)