简体   繁体   English

多次运行 C 程序,每次使用不同的输入

[英]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.我需要一种方法在终端中运行 c 程序 256 次,操作号是 function 的输入(即第三次运行它,输入为 3)并找到程序不返回的输入值分段错误。

You can get multiple execution using a bash loop, or using seq/xargs您可以使用 bash 循环或使用 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/xargs,一个衬里

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. seq/xargs 的优点是您可以同时运行多个值 - 潜在的加速。

I think a shell script might help you我认为 shell 脚本可能会对您有所帮助

#!/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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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