[CO004]オペレーティングシステムの練習用クイズ1コードパーツリファレンス回答

著者:YYクラスメート

人生は無限であり、コードは無限です。楽しいプロジェクトはすべてGitHubにあります


PS:テストはMUST-CO004Linuxとローカル仮想マシンUbuntuLinuxで完了しており、正しいです。他のLinux環境でより多くのテストバージョンを提供することを歓迎します

1. [簡単]子プロセスはwhoコマンドを実行します

Cプログラムを作成して、1つの子プロセス(child1)を作成します。Child1は、コマンド「who」を実行します。

サンプル出力:

henry
s14F047
s14G059
s14G173
s14J056
s14U028

解決:

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/types.h>

int main(){
    
    
	// 这样写严格意义上有可能会产生 Zombie,但是不违反题目要求
	int child1 = fork();
	if(child1 == 0){
    
    
		execl("/bin/who","who",NULL);
	}
	return 0;
}

2. [簡単]親子プロセス待機の問題

fork()を使用して1つの子プロセスを作成するCプログラムを作成してください。プログラムでは、子プロセスが停止した後、親プロセス(プログラム自体)が終了します。この目標を達成するには、wait()またはwaitpid()を使用してください。

サンプル出力:

The child process with PID 1235 terminated
The parent process with PID 1234 terminated

解決:

#include<stdio.h>
#include<unistd.h>
#include<sys/wait.h>
#include<sys/types.h>

int main(){
    
    
    int pid_child = fork();
    int status; // 这里需要自己定义 status 参数,因为题目明确要求要用 waitpid() 
    if( pid_child !=0 ){
    
    
        waitpid(pid_child,&status,0);
        printf("The parent process with PID: %d terminated\n", getpid());
    }
    else{
    
    
        printf("The child process with PID: %d terminated\n", getpid());
    }
    return 0;
}

3. [簡単]父と息子のプロセスを別々に計算して製品化する

子プロセスを使用して合計を計算し、親プロセスを使用して5つの整数の配列の積を計算するCプログラムを完了してください。子と親のPIDもそれぞれ出力してください。
指定された配列:int A [] = {1,2,3,4,5};

サンプル出力:

The child process with PID 2345 and the computed sum is 15.
The parent process with PID 2344 and the computed product is 120.

解決:

#include<stdio.h>
#include<unistd.h>
#include<sys/wait.h>
#include<sys/types.h>

int main(){
    
    
    int A[]={
    
    1,2,3,4,5};
    int pid_child = fork();
    if( pid_child !=0 ){
    
    
        wait(NULL);
        int product=1;
        int i; // 注意在 C99 编译环境下不允许在 for 循环内部定义变量 i,所以需要定义在外面
        for(i=0; i <5;++i) {
    
    product*=A[i];}
        printf("The parent process with PID: %d and the computed product is %d.\n", getpid(), product);
    }
    else{
    
    
    	int sum=0;
		int i;
    	for(i=0; i <5;++i) {
    
    sum+=A[i];}
        printf("The child process with PID: %d and the computed sum is %d.\n", getpid(), sum);
    }

    return 0;
}

4.【簡単】デジタルパラメータサイズの判断

condition.shという名前のBashシェルスクリプトを記述して、入力引数(つまり、整数)が100より大きいか、100より小さいか、100に等しいかどうかを確認します。ヒント、ifまたはelseステートメントを使用する必要があります。

サンプル出力:

$./condition.sh 150
The number is larger than 100
$./condition.sh 90
The number is smaller than 100
$./condition.sh 100
The number is equal to 100

解決:

#!/bin/bash

# 注意题目要求是读取参数而非读取用户输入的数字,所以这里要用 $1 代表第一个参数
if [ $1 -gt 100 ]; then                         
    echo "The number is larger than 100"
elif [ $1 -lt 100 ]; then 
	echo "The number is smaller than 100"
else
	echo "The number is equal to 100"
fi

5.【通常】単語数

filesearch.shという名前のBashシェルスクリプトを記述して、各行の「the」という単語の数と、指定されたファイルのファイル全体を計算します。

tinyTale.txt

it was the best of times it was the worst of times
it was the age of wisdom it was the age of foolishness
it was the epoch of belief it was the epoch of incredulity
it was the season of light it was the season of darkness
it was the spring of hope it was the winter of despair

サンプル出力:

./filesearch.sh tinyTale.txt
Line 1 contains 2 "the".
Line 2 contains 2 "the".
Line 3 contains 2 "the".
Line 4 contains 2 "the".
Line 5 contains 2 "the".
File tinyTale.txt contains 10 "the".

解決:

#!/bin/bash

read source

index=0
sum=0

while read line
do
	index=$((index+1))
	count=0

	for word in ${line}; do
    	if [ $word == 'the' ]; then
    		(( count++ ))
    	fi
    done
        
    echo "Line ${index} contains ${count} \"the\"."
    
    (( sum+=count ))

done < ${source} # 感谢 zsj 大佬提醒:由于管道循环内部无法改变外部变量,因此需要采取重定向解决方案

echo "File ${source} contains ${sum} \"the\"."

1つの実行可能で簡単な解決策:(cyyyyxxxxによって提供されます


awk -v awkvar='Line' -v awkvar1='contains', -v awkvar2='"the"' -F'the' 'NF{print(awkvar,NR,awkvar1,NF-1,awkvar2)}' tinyTale.txt

a=$(grep -o "the" tinyTale.txt | wc -l)
echo File tinyTale.txt contains $a '"the"'

おすすめ

転載: blog.csdn.net/qq_42950838/article/details/114953288