Question
In C Programming Linux terminal ? Write a program that compiles to an executable binary named 'program1'. It should be run like this: ./program1 The
In C Programming Linux terminal ?
Write a program that compiles to an executable binary named 'program1'. It should be run like this:
./program1
The program should spawn
Program Requirements:
You may assume the file is no bigger than 5 MB. You may use genData.sh to generate the file.
You should allocate memory for the array based on the size of the file
The main thread should spawn the worker threads prior to reading the file
The worker threads should not read the input file. Pass a pointer to the array where the data will be when the threads are spawned, then signal the threads when the data has been read into that array.
Each thread should update the array storing the line numbers where
The main thread should join on all of the worker threads prior to printing out the line numbers where the
genData.sh file to read from:
#!/bin/bash
##################
## genData.sh will generate the specified MB worth of random data
## in the range of 0-999 with the exception of the number specified
## on the command line (which is omitted). The data is written to
## the file name specified.
## usage: ./genData.sh
#################
# Integers per MB
INTEGERS_PER_MB=$(((1024*1024)/4))
# In order to prevent skew in our numbers, we\'ll only use values <= 32000 since
# $RANDOM provides values in the range 0 - 32767
MAX_RANDOM_VALUE_TO_USE=32000
# If there aren\'t exactly 3 arguments passed to this script, exit
# with a helpful mention
NUM_PARAMS=$#
if [ "$#" -ne 3 ]; then
echo "genData must be called with exactly three parameters. "
exit -1
fi
echo "Command called with ${NUM_PARAMS} parameters"
# Parse command line paramters
NUMBER_TO_OMIT=$1
FILE_NAME=$2
SIZE_IN_MB=$3
# Compute the number of integers that should be generated
NUM_INTEGERS_TO_GENERATE=$(($SIZE_IN_MB * $INTEGERS_PER_MB))
# Remove the file that we\'ll be writing to in order to ensure
# that we don\'t append to a pre-existing file
`rm ${FILE_NAME}`
i=0
# Call /dev/random until we have enough integers
while [ $i -lt $NUM_INTEGERS_TO_GENERATE ]; do
# Generate a valid random number
INTEGER_TO_WRITE=$RANDOM
while [ $INTEGER_TO_WRITE -gt $MAX_RANDOM_VALUE_TO_USE ]; do
#Keep generating new random numbers until we get one that is <= $MAX_RANDOM_VALUE_TO_USE
INTEGER_TO_WRITE=$RANDOM
done
# Then take it mod 1000 to map that number into the desired range
INTEGER_TO_WRITE=$(( $INTEGER_TO_WRITE % 1000))
# Only increment $i and write to the output file if the generated
# value is not the number that needs to be omitted.
if [ $INTEGER_TO_WRITE -ne $NUMBER_TO_OMIT ]; then
#echo "Will write ${INTEGER_TO_WRITE}"
echo "${INTEGER_TO_WRITE}" >> $FILE_NAME
let i=i+1
fi
done
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started