Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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 threads that wait while the main program reads the specified input file into an array. The file will contain worth of integers that are between 0-999. Then the main program should indicate to the threads that they should each search a disjoint portion of the input for the value . Each time a thread finds an instance of the number, it should store the line number of the input file where that instance of the number was found in an array that is shared by all threads. Be sure to use some sort of synchronization primitive to protect this array so that the value is correct. When all of the threads finish, the main process should print out the line numbers where was found.

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 was found

The main thread should join on all of the worker threads prior to printing out the line numbers where the was found.

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

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started