uni

University stuff
git clone git://git.christosmarg.xyz/uni-assignments.git
Log | Files | Refs | README | LICENSE

commit 7f3e230d09d2a0121bef4b067ebdc5f0bb494703
parent 10ef6c622b262361c4dd98306a2dda55416534de
Author: Christos Margiolis <christos@margiolis.net>
Date:   Fri, 22 Jan 2021 21:02:34 +0200

os1: ex3 done

Diffstat:
Ac-parallel-computing/ex3/doc.pdf | 0
Ac-parallel-computing/ex3/doc.tex | 32++++++++++++++++++++++++++++++++
Ac-parallel-computing/ex3/ex3.c | 21+++++++++++++++++++++
Dsh-os1/ex3/avgs.awk | 17-----------------
Dsh-os1/ex3/cmds | 36------------------------------------
Ash-os1/ex3/ex1_cmds | 39+++++++++++++++++++++++++++++++++++++++
Ash-os1/ex3/ex2_studavgs.awk | 20++++++++++++++++++++
Ash-os1/ex3/ex3_courseavgs.awk | 25+++++++++++++++++++++++++
Ash-os1/ex3/ex4_rev.awk | 20++++++++++++++++++++
Ash-os1/ex3/ex5_filesize | 3+++
Ash-os1/ex3/ex6_votes.awk | 29+++++++++++++++++++++++++++++
Ash-os1/ex3/ex7_countmatch | 20++++++++++++++++++++
Dsh-os1/ex3/grades | 3---
13 files changed, 209 insertions(+), 56 deletions(-)

diff --git a/c-parallel-computing/ex3/doc.pdf b/c-parallel-computing/ex3/doc.pdf diff --git a/c-parallel-computing/ex3/doc.tex b/c-parallel-computing/ex3/doc.tex @@ -0,0 +1,32 @@ +\documentclass{article} +\usepackage[utf8]{inputenc} +\usepackage[greek,english]{babel} +\usepackage{alphabeta} +\usepackage{fancyhdr} +\usepackage{listings} +\usepackage{mathtools} +\usepackage{xcolor} +\usepackage{biblatex} +\usepackage[left=2cm,right=2cm]{geometry} + +\lstset { + basicstyle=\ttfamily, + columns=fullflexible, + breaklines=true, + keepspaces=true +} + +\title{Εργαστήριο Παράλληλου Υπολογισμού - Εργασία 3} +\author{Χρήστος Μαργιώλης} +\date{Ιανουάριος 2020} + +\begin{document} + +\begin{titlepage} + \maketitle +\end{titlepage} + +\renewcommand{\contentsname}{Περιεχόμενα} +\tableofcontents + +\end{document} diff --git a/c-parallel-computing/ex3/ex3.c b/c-parallel-computing/ex3/ex3.c @@ -0,0 +1,21 @@ +#include <stdio.h> +#include <stdlib.h> +#include <mpi.h> + +int +main(int argc, char *argv[]) +{ + int nproc, rank, root = 0; + int rc; + + if ((rc = MPI_Init(&argc, &argv)) != 0) { + fprintf(stderr, "%s: cannot initiliaze MPI.\n", argv[0]); + MPI_Abort(MPI_COMM_WORLD, rc); + } + MPI_Comm_size(MPI_COMM_WORLD, &nproc); + MPI_Comm_rank(MPI_COMM_WORLD, &rank); + + MPI_Finalize(); + + return 0; +} diff --git a/sh-os1/ex3/avgs.awk b/sh-os1/ex3/avgs.awk @@ -1,17 +0,0 @@ -#!/usr/bin/env -S awk -f - -BEGIN { - if (ARGC < 2) { - print "usage: avgs.awk grades" - exit 1 - } -} - -{ - sum = cnt = 0; - for (i = 4; i <= NF; i++) { - sum += $i; - cnt++; - } - print "Student", NR",", $1, $2",", sum / cnt -} diff --git a/sh-os1/ex3/cmds b/sh-os1/ex3/cmds @@ -1,36 +0,0 @@ -#!/bin/sh - -# TODO: make error checks: grades: 0 - 100 - -echo "First names starting with K or N:" -awk '$1 ~ /^K|N/ {print $2, $1}' grades -echo "" - -echo "Last names starting with C or L:" -awk '$2 ~ /^C|L/ {print $1, $2}' grades -echo "" - -echo "Students with their second grade being > 85:" -awk '{if ($5 > 85 && $5 > 85) print}' grades -echo "" - -echo "Students in the humanities:" -awk '{if ($3 == "Theo") print}' grades -echo "" - -echo "Average grades:" -awk '{ -sum = cnt = 0; -for (i = 4; i <= NF; i++) { - sum += $i; - cnt++; -} -print $1, $2, sum / cnt}' grades -echo "" - -echo "Students with their third grade being < 70:" -awk 'BEGIN {cnt = 0} {if ($6 < 70) cnt++;} END {print cnt}' grades -echo "" - -echo "How many student names end in 'is'?" -awk 'BEGIN {cnt = 0} {if ($2 ~ /is$/) cnt++} END {print cnt}' grades diff --git a/sh-os1/ex3/ex1_cmds b/sh-os1/ex3/ex1_cmds @@ -0,0 +1,39 @@ +#!/bin/sh + +test $# -ne 1 && echo "usage: ${0##*/} gradefile" 1>&2 && exit 1 +test ! -f "${1}" && echo "${0##*/}: ${1}: no such file" 1>&2 && exit 1 + +file="${1}" + +echo "First names starting with K or N:" +awk '$1 ~ /^K|N/ {print $2, $1}' ${file} +echo "" + +echo "Last names starting with C or L:" +awk '$2 ~ /^C|L/ {print $1, $2}' ${file} +echo "" + +echo "Students with their second grade being > 85:" +awk '{if ($5 > 85 && $5 > 85) print}' ${file} +echo "" + +echo "Students in the humanities:" +awk '{if ($3 == "Theo") print}' ${file} +echo "" + +echo "Average ${file}:" +awk '{ +sum = cnt = 0; +for (i = 4; i <= NF; i++) { + sum += $i; + cnt++; +} +print $1, $2, sum / cnt}' ${file} +echo "" + +echo "Students with their third grade being < 70:" +awk 'BEGIN {cnt = 0} {if ($6 < 70) cnt++;} END {print cnt}' ${file} +echo "" + +echo "How many student names end in 'is'?" +awk 'BEGIN {cnt = 0} {if ($2 ~ /is$/) cnt++} END {print cnt}' ${file} diff --git a/sh-os1/ex3/ex2_studavgs.awk b/sh-os1/ex3/ex2_studavgs.awk @@ -0,0 +1,20 @@ +#!/usr/bin/env -S awk -f + +BEGIN { + # Quit if we don't have an input file. + if (ARGC < 2) { + print "usage: ex2_studavgs.awk gradefile"; + exit 1; + } +} + +{ + sum = cnt = 0; + for (i = 4; i <= NF; i++) { + sum += $i; + cnt++; + } + + # We're calling sort(1) from awk(1). + print "Student", NR",", $1, $2",", sum / cnt | "sort -Vrk5"; +} diff --git a/sh-os1/ex3/ex3_courseavgs.awk b/sh-os1/ex3/ex3_courseavgs.awk @@ -0,0 +1,25 @@ +#!/usr/bin/env -S awk -f + +BEGIN { + # Quit if we don't have an input file. + if (ARGC < 2) { + print "usage: ex3_courseavgs.awk gradefile"; + exit 1; + } +} + +{ + # Loop from the 4th field onwards and + # add its value to the sum array. + for (i = 4; i <= NF; i++) + sum[i] += $i; +} + +END { + # `NR` is the number of rows in the file, so + # we divide by that since each column is a + # course. + cnt = 1; + for (i = 4; i <= NF; i++) + print "Course", cnt++":", sum[i] / NR; +} diff --git a/sh-os1/ex3/ex4_rev.awk b/sh-os1/ex3/ex4_rev.awk @@ -0,0 +1,20 @@ +#!/usr/bin/env -S awk -f + +BEGIN { + # Quit if we don't have an input file. + if (ARGC < 2) { + print "usage: ex4_rev.awk file"; + exit 1; + } +} + +{ + # Add all lines to an array. + ln[NR] = $0; +} + +END { + # Print them in reverse. + for (i = NR; i >= 1; i--) + print ln[i]; +} diff --git a/sh-os1/ex3/ex5_filesize b/sh-os1/ex3/ex5_filesize @@ -0,0 +1,3 @@ +#!/bin/sh + +ls -l | grep '^-' | awk '{sum += $5} END{print sum}' diff --git a/sh-os1/ex3/ex6_votes.awk b/sh-os1/ex3/ex6_votes.awk @@ -0,0 +1,29 @@ +#!/usr/bin/env -S awk -f + +BEGIN { + # Quit if we don't have an input file. + if (ARGC != 2) { + print "usage: ex6_votes.awk votefile"; + exit 1; + } + + max = 0; +} + +{ + cnt = 0; + # We're testing only against 'y's, + # no need to check for 'n's. + for (i = 2; i <= NF; i++) + if ($i == "y") + cnt++; + if (cnt > max) { + max = cnt; + winner = $1; + } +} + +END { + print "Winner:", winner; + print "Votes:", max; +} diff --git a/sh-os1/ex3/ex7_countmatch b/sh-os1/ex3/ex7_countmatch @@ -0,0 +1,20 @@ +#!/bin/sh + +main() { + xread "String: " str + test -z "${str}" && echo "${0##*/}: give a non-empty string" 1>&2 && exit 1 + + xread "File: " file + test ! -f "${file}" && echo "${0##*/}: ${file}: no such file" 1>&2 && exit 1 + + awk "/${str}/ {cnt++} END {print cnt}" ${file} +} + +# A portable `read` function with behavior similar to +# `read -erp`. +xread() { + printf "%s" "${1}" && read -r ${2} +} + +# Pass all command line arguments to `main`. +main "$@" diff --git a/sh-os1/ex3/grades b/sh-os1/ex3/grades @@ -1,3 +0,0 @@ -Thomas Georgiou Texn 96 89 81 84 -Mary Makridi Thet 67 75 56 74 -Nick Aliferis Theo 81 87 75 94