让Python脚本每次运行二进制代码

让Python脚本每次运行二进制代码,python,Python,我正在使用python脚本在终端中运行命令。我有一个C语言的程序,numgen,它只是用来随机生成数字。编译完numgen后,我模拟在不同的体系结构上运行numgen 要输入到命令行的文本: cmd= "~/simdir/build/ALPHA/gem5.opt ~/simdir/configs/example /se.py ~/First/NumGen/numgen -o " 然后,我使用以下命令在子shell中运行该命令 for x in range(5): //number o

我正在使用python脚本在终端中运行命令。我有一个C语言的程序,numgen,它只是用来随机生成数字。编译完numgen后,我模拟在不同的体系结构上运行numgen

要输入到命令行的文本:

cmd= "~/simdir/build/ALPHA/gem5.opt ~/simdir/configs/example /se.py   ~/First/NumGen/numgen -o   " 
然后,我使用以下命令在子shell中运行该命令

for x in range(5): //number of simulations
os.system(run)//run is cmd plus a string I created using a for loop because se.py requires an argument
不过我遇到了一个问题。运行完所有操作后,我使用result_file.write()将有关numgen的相关数据输出到一个文件中。这很好,除非这个新文件中的所有输出都是相同的。numgen程序是C语言的,用于生成随机数。它做得很好,当我在终端中运行二进制代码/numgen时,我会得到随机数列表

在脚本中,似乎os.system每次都没有在子shell中运行二进制文件。系统在开始时运行二进制文件一次,并为其余的模拟输出相同的数据。有人知道我怎样才能使数据真正随机吗?我希望在每次模拟之前运行二进制文件,以便生成的数据可以不同。是否可以在脚本中执行此操作?我确实在脚本中有编译标志和其他文本,但我不认为这些与上述代码中的问题有关

本质上,os.system在所有模拟中只运行一次二进制代码。我想知道是否有可能为每个不同的模拟运行二进制文件。感谢您的帮助。谢谢

编辑1-完整脚本:

from random import * 
from array import *
import sys, string, os, subprocess, shlex, binascii, collections, operator 
import re
import subprocess


plaintext = array('B', [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]); 

loop = long(0); 
loop_max = long(1000); 
number = long(0); 
byte_index = 0; 

cmd = "~/simdir/build/ALPHA/gem5.opt ~/simdir/configs/example /se.py   --num-cpus=1 --caches --l2cache --cpu-type=timing --cacheline_size=64  --l1d_size=32kB --l1d_assoc=2 --l1i_size=32kB --l1i_assoc=4 --l2_size=1MB --l2_assoc=2  --max-checkpoints=200 -c ~/First/NumGen/numgen -o  " 


string="" 

Somekeywords = ['sim_freq','sim_ticks','host_mem_usage', 'system.cpu.numCycles', 'host_seconds',  'sim_insts' ]

table = [[0 for first in range(26)] for second in range(loop_max)]; #16 for plaintext bytes, 1 for ticks, 9 for L1D/L1I/L2 miss/hit/access
result =[[0 for first in range(26)] for second in range(loop_max)];

outfile = stats_text = open('Data.txt', 'w') 



stats_text=open('RelStat', 'w')

numb=10


for x in range(numb): 



    number=number+1
    outfile.write('%d\n' % number)

    run = ""
    string= ""
    final_str = ""

    for x in range(16): 
       plaintext[i] = randint(0, 255) 

    tmp = ""

    #Dec -> HEX -> Str
    for x in range(16):
        tmp = hex(plaintext[i])[2:] 
        if len(tmp)==1:
            tmp = "0"+tmp 

        string = string + tmp 


    run = cmd + string 


    subprocess.Popen(run, shell=True).wait() #Just changed this previously used os.system(run)

    stats_text = open('newdir/analysis.txt', 'r') 


    count = 0;
    for line in stats_text: 
        for index in range(3): 
            if Somekeywords[index] in line: 

                count = count + 1; #    
        line=re.sub('[^0-9]',' ', line) 
        outfile.write(line)
        outfile.write("\n")




                if count>10 and count<21: 
                    result_tmp = re.findall(r'\b\d+\b',line); 
                    print(loop);
                    result[loop][16+index] = int(result_tmp[0]);



    for i in range(16):
        result[loop][i] = plaintext[i];

    loop = loop + 1;

    if loop == loop_max:
        loop = 0;

        for i in range(loop_max):
            for j in range(26):
                final_str = final_str + str(result[i][j]) + ' '; 

outfile.write("\n")


outfile.close
来自随机导入*
从数组导入*
导入系统、字符串、操作系统、子流程、shlex、binascii、集合、运算符
进口稀土
导入子流程
明文=数组('B',[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]);
循环=长(0);
回路_max=长(1000);
数字=长(0);
字节索引=0;
cmd=“~/simdir/build/ALPHA/gem5.opt~/simdir/configs/example/se.py--num cpu=1--cache--l2cache--cpu type=timening--cacheline_size=64--l1d_size=32kB--l1d_assoc=2--l1i_assoc=4--l2_size=1MB--l2--max checkpoints=200--c~/First/NumGen
string=“”
Somekeywords=['sim_freq','sim_ticks','host_mem_usage','system.cpu.numCycles','host_seconds','sim_insts']
表=[[0代表范围内第一(26)]代表范围内第二(loop_max)]#16表示明文字节,1表示记号,9表示L1D/L1I/L2未命中/命中/访问
结果=[[0表示范围内第一个(26)]表示范围内第二个(loop_max)];
outfile=stats\u text=open('Data.txt','w')
stats\u text=open('RelStat','w')
麻木=10
对于范围内的x(编号):
数字=数字+1
outfile.write(“%d\n”%number)
run=“”
string=“”
最终_str=“”
对于范围(16)内的x:
纯文本[i]=randint(0255)
tmp=“”
#十二进制->十六进制->字符串
对于范围(16)内的x:
tmp=hex(纯文本[i])[2:]
如果len(tmp)==1:
tmp=“0”+tmp
字符串=字符串+tmp
run=cmd+string
subprocess.Popen(run,shell=True).wait()#刚刚更改了以前使用的os.system(run)
stats\u text=open('newdir/analysis.txt','r')
计数=0;
对于stats_文本中的行:
对于范围(3)中的索引:
如果行中有一些关键字[索引]:
计数=计数+1;#
line=re.sub(“[^0-9]”,“,”,第行)
输出文件。写入(行)
outfile.write(“\n”)

如果计数>10和计数,你是否在C++中产生随机数?在生成随机数之前是否使用
seed
?我认为应该运行
子进程
模块在shell上运行命令。与
os.system
相比,它更强大。查看此链接了解更多信息:将完整脚本粘贴到此处,特别是如何构建
运行
@niyasc我有一个随机种子。我正在使用srand。Thanks@Shubham瓦萨卡:谢谢你的建议。不幸的是,我的问题仍然存在,但我将使用subprocess模块,特别是subprocess.Popen,而不是os.system来处理类似的情况。感谢你在C++中生成随机数吗?在生成随机数之前是否使用
seed
?我认为应该运行
子进程
模块在shell上运行命令。与
os.system
相比,它更强大。查看此链接了解更多信息:将完整脚本粘贴到此处,特别是如何构建
运行
@niyasc我有一个随机种子。我正在使用srand。Thanks@Shubham瓦萨卡:谢谢你的建议。不幸的是,我的问题仍然存在,但我将使用subprocess模块,特别是subprocess.Popen,而不是os.system来处理类似的情况。谢谢
#include <stdio.h>
#include <stdlib.h>
#include <time.h>


int main()
{
    FILE *of1;
    of1=fopen("Data.txt", "w");
    srand(time(NULL));
    for (int z=0; z<16; z++)
    {
        int numgen=rand()%100;
        fprintf(of1, "%d ", numgen);
    }
    fclose(of1);
}