跳至主要內容
OS 补血

你好,我是悦创。

在 Python 中,os 库提供了一组用于进程管理的函数,其中包括 os.forkos.waitpid

  1. os.fork()os.fork 是用于创建新的进程的函数。这个函数的工作方式是,它复制当前进程,并创建一个新的进程。这个新的进程被称为子进程,而原来的进程被称为父进程。

当你调用 os.fork 函数时,它会返回一个值。对于父进程,这个值是新创建的子进程的 PID;对于子进程,这个值是 0。


AI悦创原创...大约 7 分钟Python 一对一教学SCU CSSanta Clara University圣克拉拉大学Python 一对一教学SCU CSSanta Clara University圣克拉拉大学
Linux 速查表

你好,我是悦创。

猛肝了一晚,终于把这个表格整理出来,还没开始校对✍️。

Linux 的命令非常多,非常丰富,下面我会列举出一些常用的 Linux 命令,并按照文件操作、系统信息、进程管理等类别进行分类。

文件操作命令

序号 命令 作用 语法 示例
1 ls 列出目录 ls [option] [directory] ls -l
2 cd 更改目录 cd [directory] cd /home
3 pwd 显示当前目录 pwd pwd
4 touch 创建空文件 touch file touch example.txt
5 cat 查看文件内容 cat file cat example.txt
6 more 分页显示文件内容 more file more example.txt
7 head 查看文件开头 head file head example.txt
8 tail 查看文件末尾 tail file tail example.txt
9 cp 复制文件或目录 cp source destination cp example.txt example_copy.txt
10 mv 移动或重命名文件或目录 mv source destination mv /tmp/example.txt /home/
11 rm 删除文件或目录 rm option file rm -r example_folder
12 find 查找文件 find path -name filename find / -name example.txt
13 wc 统计文件中行数、字数、字符数 wc option file wc -l example.txt
14 diff 比较文件差异 diff file1 file2 diff example1.txt example2.txt
15 file 确定文件类型 file file file example.txt
16 chmod 更改文件或目录的权限 chmod permissions file chmod 755 example.txt
17 chown 更改文件或目录的所有者 chown user:group file chown root:root example.txt
18 df 显示磁盘使用情况 df [options] df -h
19 du 查看目录或文件的磁盘使用情况 du [options] [directory_or_file] du -sh /home
20 ln 创建链接文件 ln -s source_file link_file ln -s /path/to/example.txt link.txt
21 grep 文本搜索 grep [options] pattern [file] grep "example" example.txt
22 sed 文本处理 sed [option] 'command' file sed 's/test/best/g' example.txt
23 awk 文本处理 awk [options] 'command' file awk '{print $1}' example.txt
24 sort 对文本行排序 sort [options] file sort -n example.txt
25 tar 归档工具 tar [options] filename tar -cvf archive.tar /path/to/directory
26 gzip 压缩或解压文件 gzip [options] file gzip example.txt
27 gunzip 解压 gzip 文件 gunzip file gunzip example.txt.gz
28 zip 压缩或解压文件 zip [options] archive_name file zip example.zip example.txt
29 unzip 解压 zip 文件 unzip file unzip example.zip
30 cut 删除文件中的某些部分 cut [options] file cut -d ':' -f 1 example.txt

AI悦创原创...大约 11 分钟Python 一对一教学SCU CSSanta Clara University圣克拉拉大学Python 一对一教学SCU CSSanta Clara University圣克拉拉大学
Assignment 6

Scripting Problem (5 points)

The goal of this problem is to find the current user’s group with the largest number of members with the exclusion of the group users. That is, considering the groups that the current user belongs to (with the exclusion of the group users), which one is composed of the largest number of members? (Don’t worry about ties)


AI悦创原创...大约 2 分钟Python 一对一教学SCU CSSanta Clara University圣克拉拉大学Python 一对一教学SCU CSSanta Clara University圣克拉拉大学
Assignment 5

Problem 1 (1 point)

Find the number of existing processes. Your output should be formatted as follows:

On xxx, there were yyy processes

xxx is the current timestamp (which you can retrieve with the command date)

yyy is the number of processes

For example: On Thu Feb 9 15:28:48 PST 2023, there were 264 processes


AI悦创原创...大约 5 分钟Python 一对一教学SCU CSSanta Clara University圣克拉拉大学Python 一对一教学SCU CSSanta Clara University圣克拉拉大学
Assignment 4

Exercise 1 (1 point)

Position yourself in /home/OMIS107/Lecture7. Print the lines of students.txt relative to those students in MGMT that completed 2, 3, or 4 courses.

将自己定位在/home/OMIS107/Lecture7目录下。打印与MGMT学生完成2、3或4门课程相关的 students.txt 文件中的行。


AI悦创原创...大约 6 分钟Python 一对一教学SCU CSSanta Clara University圣克拉拉大学Python 一对一教学SCU CSSanta Clara University圣克拉拉大学
Assignment 3

Problem 1

Position yourself in /home/OMIS107/Lecture2. Print the lines of alice.txt that contain at least two words (not necessarily consecutive words) starting with “a”, with the first word being composed of at least 8 more letters after “a”.

grep -Ein "\ba[a-z]{8,}.*\ba" alice.txt

AI悦创原创...大约 8 分钟Python 一对一教学SCU CSSanta Clara University圣克拉拉大学Python 一对一教学SCU CSSanta Clara University圣克拉拉大学
Final Project

Problem 1 (1 pt)

Make a subdirectory myfinal inside your own directory

Solution:

mkdir myfinal

Problem 2 (5 pts)

Positionyourselfinmyfinal. Copyfrom/home/OMIS107/practice_final/filesto your myfinal directory the files whose name contains the letter “t”, followed later on (not necessarily right after) by the string “99”. Use only one command and use relative pathnames only (i.e., do not use absolute pathnames).


AI悦创原创...大约 6 分钟Python 一对一教学SCU CSSanta Clara University圣克拉拉大学Python 一对一教学SCU CSSanta Clara University圣克拉拉大学
Final Project

Problem 2 (5 pts)

Position yourself in /home/OMIS107 before solving this problem. Copy into your directory myexam (example: /home/fakestudent/myexam) all of the files in /home/OMIS107/final/files whose name satisfies both conditions:

在解决这个问题之前,请先定位到/home/OMIS107。将/home/ omis107 /final/files目录下所有满足以下两个条件的文件复制到你的目录myexam(例如:/home/fakestudent/myexam):


AI悦创原创...大约 9 分钟Python 一对一教学SCU CSSanta Clara University圣克拉拉大学Python 一对一教学SCU CSSanta Clara University圣克拉拉大学
Assignment 8

Problem 1 (4 points)

Write a Python program that asks the user for a number and store it in the variable n. Then, the main process creates two child processes (both must be children of the main process). One child exits successfully if n>10 and unsuccessfully otherwise, whereas the other child exits successfully if n>20 and unsuccessfully otherwise. The main process must print how many of its children ended successfully. The children must be allowed to run in parallel.


AI悦创原创...大约 2 分钟Python 一对一教学SCU CSSanta Clara University圣克拉拉大学Python 一对一教学SCU CSSanta Clara University圣克拉拉大学
Assignment 7

3 points

The folder /home/OMIS107/HW7 contains an executable program, prc. It asks the user for his/her name and for a number, which the program multiplies it by two.

/home/OMIS107/HW7文件夹中包含一个可执行程序prc。它要求用户输入“他/她”的名字和一个数字,程序将其乘以2。


AI悦创原创...大约 5 分钟Python 一对一教学SCU CSSanta Clara University圣克拉拉大学Python 一对一教学SCU CSSanta Clara University圣克拉拉大学
Assignment 2

Instructions:

Write the commands needed to solve the following exercises on mis01.scu.edu. Do not worry about upper/lower case.

Problem 1

First, make a directory temp inside your personal directory. Then, position yourself in directory temp. Use relative pathnames to copy into temp all of the files in /home/OMIS107/HW2 whose name ends with 120, 121, 122, ..., up to 180 included. (one or two cp commands)


AI悦创原创...大约 3 分钟Python 一对一教学SCU CSSanta Clara University圣克拉拉大学Python 一对一教学SCU CSSanta Clara University圣克拉拉大学
Linux inventory

1. cd

2. ls

3. cat

4. echo

echo 是一个在 Unix-like 系统(如 Linux)中非常常用的命令,它用于在命令行界面输出字符串或变量的值。基本语法是 echo [options] [string, variables...]

以下是一些常见的 echo 命令的用法和示例:

  1. 输出字符串:你可以使用 echo 命令输出一个或多个字符串。例如:

    echo "Hello, World!"
    

    这个命令会在终端中打印出 "Hello, World!"。

  2. 输出变量的值:你可以使用 echo 命令输出环境变量的值。例如:

    echo $HOME
    

    这个命令会在终端中打印出 HOME 环境变量的值,通常是你的家目录的路径。

  3. 使用转义字符echo 命令支持一些转义字符,比如 \n(新行),\t(制表符)等。要启用这些转义字符,你需要使用 -e 参数。例如:

    echo -e "Hello,\nWorld!"
    

    这个命令会打印出两行:第一行是 "Hello,",第二行是 "World!"。

  4. 不输出尾部的新行echo 命令默认会在输出的末尾添加一个新行。如果你不想输出这个新行,可以使用 -n 参数。例如:

    echo -n "Hello, World!"
    

    这个命令会打印出 "Hello, World!",但不会在末尾添加新行。因此,下一个终端提示符会紧接在 "Hello, World!" 后面,而不是在新的一行。


AI悦创原创...大约 88 分钟Python 一对一教学SCU CSLinux 手册Santa Clara University圣克拉拉大学Python 一对一教学SCU CSLinux 手册Santa Clara University圣克拉拉大学
Assignment 1

Instructions:

Write the commands needed to solve the following problems on mis01.scu.edu. The first two problems are already solved to show you how to proceed.

Each problem will be marked with a 1 if correct, with a 0.5 if partially correct, and with a 0 otherwise.


AI悦创原创...大约 5 分钟Python 一对一教学SCU CSSanta Clara University圣克拉拉大学Python 一对一教学SCU CSSanta Clara University圣克拉拉大学