我用C写了一个程序在Linux系统执行,怎么样才能提高效率?
爱韩 2012-01-09 03:17:31 执行getCommand 函数获得命令,如果命令正确就发出去,如果没发送成功就每隔一段时间再发过去。如果发送成功,则每隔一段时间重新获得新的command命令,和上次的进行比较,如果不相同就再发过去。
怎么样提高效率??
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <sys/types.h>
#include <net/if.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <linux/sockios.h>
#include <sys/ioctl.h>
#include <arpa/inet.h>
#include <unistd.h>
/*
* 把字符串中的所有字符转换成小写字母
*/
void getLower(char *upper)
{
int i;
for(i = 0; upper[i] != '\0'; i++)
upper[i] = tolower(upper[i]);
}
int getCommand(char *comm)
{
//获得 comm,成功返回 1 ,else 返回 0
}
int main()
{
char command[150] = "";
char temp[150] = "";
int flag = 1, result = 0;
while(1)
{
result = getCommand(command);
printf("%s\n", command);
if(result == 1)
{
if(flag == 0)
{
if(strcmp(temp, command) != 0)
{
flag = system(command);
if(flag == 0)
strcpy(temp, command);
}
}
else
{
flag = system(command);
if(flag == 0)
strcpy(temp, command);
}
}
sleep(60);
}
return 0;
}