[CO004] Operating system practice quiz1 code part reference answer

Author: YY classmate

Life is endless, and the code is endless. Fun projects are all on GitHub


PS: The test has been completed on MUST-CO004 Linux and the local virtual machine Ubuntu Linux, and it is correct. Welcome to provide more test versions under other Linux environments

1. [Easy] The child process executes the who command

Please write a C program shall create one child process - child1. Child1 shall execute a command “who”.

Sample Output:

henry
s14F047
s14G059
s14G173
s14J056
s14U028

Solution:

#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. [Easy] Parent-child process wait problem

Please write a C program to create one child process by using fork(). In your program, the parent process (the program itself) will terminate after the child process is dead. Please use wait() or waitpid() to achieve this goal.

Sample Output:

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

Solution:

#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. [Easy] Calculate and product the father and son processes separately

Please complete a C program that uses the child process to compute sums and the parent to compute the products of an array of 5 integers. Please also output the PIDs of the child and the parent, respectively.
The given array: int A[]={1,2,3,4,5};

Sample Output:

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

Solution:

#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. [Easy] Judgment of digital parameter size

Write a Bash shell script named condition.sh to check whether the input argument (i.e., an integer) is greater than 100, or smaller than 100 or equal to 100. Hint, you need to use if or else statement.

Sample Output:

$./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

Solution:

#!/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. 【Normal】Word count

Write a Bash shell script named filesearch.sh to compute the number of the word “the” in each line and the whole file of the given file.

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

Sample Output:

./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".

Solution:

#!/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\"."

One Feasible and Brief Solution: ( Provided By 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"'

Guess you like

Origin blog.csdn.net/qq_42950838/article/details/114953288