Newer
Older
// $Id: OS.cpp,v 1.3 2006-03-01 16:07:17 geuzaine Exp $
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software

Christophe Geuzaine
committed
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
// USA.
//
// This file contains a bunch of functions that depend on OS-dependent
// features and/or system calls
// these are available on all OSes
#include <stdlib.h>
#include <sys/time.h>
#include <sys/resource.h>
#endif
#if defined(__APPLE__)
#define RUSAGE_SELF 0
#define RUSAGE_CHILDREN -1
#endif
#if !defined(WIN32) || defined(__CYGWIN__)
struct timeval tp;
gettimeofday(&tp, (struct timezone *)0);
double t = (double)tp.tv_sec + 1.e-6 * (double)tp.tv_usec;
return t;
#else
double t = 1.e-7 * 4294967296. * (double)ft.dwHighDateTime +
1.e-7 * (double)ft.dwLowDateTime;
#endif
#if !defined(WIN32) || defined(__CYGWIN__)
usleep((long)(1.e6 * s));
#else
#endif
void GetResources(double *s, long *mem)
{
#if !defined(WIN32) || defined(__CYGWIN__)
static struct rusage r;
getrusage(RUSAGE_SELF, &r);
*s = (double)r.ru_utime.tv_sec + 1.e-6 * (double)r.ru_utime.tv_usec;
*mem = (long)r.ru_maxrss;
#else
FILETIME creation, exit, kernel, user;
if(GetProcessTimes(GetCurrentProcess(), &creation, &exit, &kernel, &user)){
*s = 1.e-7 * 4294967296. * (double)user.dwHighDateTime +
1.e-7 * (double)user.dwLowDateTime;
}
*mem = 0;
}
double Cpu()
{
long mem = 0;
double s = 0.;
GetResources(&s, &mem);
return s;
}
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
int GetProcessId()
{
#if !defined(WIN32) || defined(__CYGWIN__)
return getpid();
#else
return _getpid();
#endif
}
int UnlinkFile(char *filename)
{
#if !defined(WIN32) || defined(__CYGWIN__)
return unlink(filename);
#else
return _unlink(filename);
#endif
}
int StatFile(char *filename)
{
#if !defined(WIN32) || defined(__CYGWIN__)
struct stat buf;
return stat(filename, &buf);
#else
struct _stat buf;
return _stat(filename, &buf);
#endif
}
int KillProcess(int pid)
{
#if !defined(WIN32) || defined(__CYGWIN__)
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#else
HANDLE hProc = OpenProcess(PROCESS_TERMINATE, FALSE, pid);
if(!TerminateProcess(hProc, 0)){
CloseHandle(hProc);
return 0;
}
#endif
return 1;
}
void SystemCall(char *command)
{
#if defined(WIN32)
STARTUPINFO suInfo;
PROCESS_INFORMATION prInfo;
memset(&suInfo, 0, sizeof(suInfo));
suInfo.cb = sizeof(suInfo);
Msg(INFO, "Calling '%s'", command);
CreateProcess(NULL, command, NULL, NULL, FALSE,
NORMAL_PRIORITY_CLASS, NULL, NULL, &suInfo, &prInfo);
#else
if(!system(NULL)) {
Msg(GERROR, "Could not find /bin/sh: aborting system call");
return;
}
Msg(INFO, "Calling '%s'", command);
system(command);
#endif
}