Final Exam 2024 Q-and-A#

The exercises are supposed to be done by each student solely on his desktop in the Linux lab, D-112. It is allowed to use the lecture notes at https://linuxcourse.rutgers.edu, any printed material, as well as internet published resources. The exam time is from 12 pm to 3 pm on Wednesday May 5 2024.

Number of exercises: 4. Each exercise has a maximum score.

Create directory FINAL and compose file answers.txt with answers to the exam questions. Please put your name and the desktop number in the beginning of the file.

  • Your name:

  • Your desktop number:

1. Compilation and Makefile (max score 6)#

  • On your desktop, download code multitask_openmp.c from https://people.math.sc.edu/Burkardt/c_src/multitask_openmp/multitask_openmp.c

  • A) Compile the code into binary file multitask_openmp.x for running with OpenMP. Make sure the executable, multitask_openmp.x, is runable. Run multitask_openmp.x on 1 processor then on 2 processors with OpenMP. Put the run times in your answers.txt file.

  • B) Develop Makefile that includes targets for multitask_openmp.x, and for deleting it. Post the Makefile in your answers.txt file.


  • Answer:

A)

wget https://people.math.sc.edu/Burkardt/c_src/multitask_openmp/multitask_openmp.c
gcc -fopenmp -o multitask_openmp.x multitask_openmp.c  -lm
export OMP_NUM_THREADS=1
./multitask_openmp.x
export OMP_NUM_THREADS=2
./multitask_openmp.x

B)


CC = gcc
LIBS = -lm

multitask_openmp.x: multitask_openmp.c
        $(CC) -fopenmp -o $@ $< $(LIBS)

clean:
        -@rm multitask_openmp.x


2. Shell script and a scheduled task (max score 4)#

A. Develop shell script run.sh that

  • sets the number of threads to 2

  • runs executable multitask_openmp.x and redirects the standard output to file output.txt.

B. Schedule the script to run now. Make sure file output.txt is updated.


  • Answer:

A.



#!/bin/bash

export OMP_NUM_THREADS=2

./multitask_openmp.x > output.txt

B.

chmod a+x run.sh
sudo apt install at
at -f /home/hostadm/Final/run.sh now
ls -l output.txt

3. Python script (max score 4)#

Develop python script run.py, that takes the numbers of threads as input parameter--threads and runs executable multitask_openmp.x

Execute run.py with 1, 2, and 3 threads:

./run.py --threads 1
./run.py --threads 2
./run.py --threads 3

  • Answer

run.py script:

#!/usr/bin/python3

import os, argparse

parser = argparse.ArgumentParser()

parser.add_argument('--threads', type = str, required=True, help = 'Number of threads')

args = parser.parse_args()

cmd1 = f'export OMP_NUM_THREADS={args.threads}'
cmd2 = f'./multitask_openmp.x'
#cmd = f'{cmd1};{cmd2}'  # one way
cmd = f"{cmd1}\n {cmd2}" # another way
os.system(cmd)

4. Command grep, sed, and regular expressions (max score 6)#

Specify the grep command options you use to find the answers to the questions below:

A) Find the number of printf calls in file multitask_openmp.c.

B) Select the lines that contain int *primes

C) By using command sed replace the values of variables prime_num and sine_num from 20000 to 30000.


  • Answer:

A)

grep printf multitask_openmp.c | wc -l

19

B)

grep 'int \*primes' multitask_openmp.c

C) Use option -i to commit the changes in place.

sed -i -e 's/prime_num = 20000/prime_num = 30000/g' -e 's/sine_num = 20000/sine_num = 30000/' multitask_openmp.c