简体   繁体   中英

Running a C program multiple times with different inputs each time

I need a way to run a c program 256 times in the terminal with the operation number being the input for the function ( ie for 3rd time running it, input is 3) and find the value of input at which the program doesn't return a segmentation fault.

You can get multiple execution using a bash loop, or using seq/xargs

The code assumes that program will finish with exit code zero or crash.

# Bash
for ((i=1 ; i<=256 ; i++ )) do
   if ! program $i > result.$i ; then
      echo "Failed on $i"
   fi
done

Using seq/xargs,one liner

seq 1 256 | xargs -I@ program @ '||' echo "Failed on $@" \;

The advantage of seq/xargs is that you can run multiple values at the same time - potential speedup.

I think a shell script might help you

#!/bin/bash

for i in 1 2 3 4 5
do
./program $i
exit_status=$?
if [ $exit_status -eq 127 ]; then
    echo $i
fi
done

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM