- 论坛徽章:
- 0
|
先上我的运行结果
[root@localhost linux_ex3]# ./test_sched2.o
/home/qy/linux_ex3/test_sched.c
Process ID:3648, Parent process ID: 3647
This is parent process!!!!!!!!!!
Process ID:3647, Parent process ID: 3568
The child process costed 49.000000 seconds.
The child process costed 46660000.000000 clock cycles.
The entire test costed 51.00 seconds.
The entire test costed 48820000.00 clock cycles.
--------------------------------我是分割线---------------------------------------------
同时运行test_sched.o与test_sched2.o
[root@localhost linux_ex3]# ./test_sched.o
Process ID:3652, Parent process ID: 3651
This is parent process!!!!!!!!!!
Process ID:3651, Parent process ID: 3599
The child process costed 7.000000 seconds.
The child process costed 3840000.000000 clock cycles.
The entire test costed 14.00 seconds.
The entire test costed 11470000.00 clock cycles.
[root@localhost linux_ex3]# ./test_sched2.o
/home/qy/linux_ex3/test_sched.c
Process ID:3650, Parent process ID: 3649
This is parent process!!!!!!!!!!
Process ID:3649, Parent process ID: 3574
The entire test costed 51.00 seconds.
The entire test costed 41000000.00 clock cycles.
The child process costed 54.000000 seconds.
The child process costed 42270000.000000 clock cycles.
如上面红色部分所示,当我单独运行test_sched2.o程序时,所使用cpu时间为48.82s,而当我开了两个终端同时运行test_sched.o与test_sched2.o文件时,所使用的CPU时间为42.27s。
从实际情况来看,这两个时间应该是近似相等的,但是从程序的运行结果看来,相差很大,请问高手这是什么原因?
附上源程序:
==============================================================
//test_sched.c
#include<stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <errno.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/times.h>
#include <pthread.h>
#include <unistd.h>
#include <dirent.h>
void timeConsumer1()
{
long i;
double dj;
for (i=0;i<100000000;i++)
{
dj = 99999.99;
dj*= 999.99;
dj/= 777.77;
}
}
void timeConsumer2()
{
long i;
double dj;
for (i=0;i<100000000;i++)
{
dj = 88888.99;
dj*= 789.99;
dj/= 987.77;
}
}
int main(int argc, char* argv[])
{
pid_t pc=1;
time_t c_start,t_start, c_end,t_end, c_end2,t_end2;
c_start = clock();
t_start = time(NULL) ;
pc = fork();
if (pc<0)
printf("error occurred!\n");
else if (pc==0)
{
printf("Process ID:%d, Parent process ID: %d \n", getpid(), getppid());
timeConsumer1();
timeConsumer2();
c_end = clock();
t_end = time(NULL) ;
printf("The child process costed %f seconds.\n",difftime(t_end,t_start)) ;
printf("The child process costed %f clock cycles.\n",difftime(c_end,c_start)) ;
}
else
{
printf("This is parent process!!!!!!!!!!\n");
printf("Process ID:%d, Parent process ID: %d \n", getpid(), getppid());
timeConsumer1();
timeConsumer2();
timeConsumer1();
timeConsumer2();
timeConsumer1();
timeConsumer2();
c_end = clock();
t_end = time(NULL) ;
printf("The entire test costed %.2f seconds.\n",difftime(t_end,t_start)) ;
printf("The entire test costed %.2f clock cycles.\n",difftime(c_end,c_start)) ;
return 0;
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//test_sched2.c
#include<stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <errno.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/times.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>
#include <unistd.h>
#include <dirent.h>
static char* filePath = "/home/qy/linux_ex3/test_sched.c";
void timeConsumer1()
{
long i;
double dj;
for (i=0;i<100000000;i++)
{
dj = 99999.99;
dj*= 999.99;
dj/= 777.77;
}
}
void timeConsumer2()
{
long i;
double dj;
for (i=0;i<100000000;i++)
{
dj = 88888.99;
dj*= 789.99;
dj/= 987.77;
}
}
void timeConsumer3()
{
long i;
int fd;
for (i=0;i<10000000;i++)
{
fd = open(filePath, O_RDONLY);
close(fd);
}
}
int main(int argc, char* argv[])
{
pid_t pc=1;
time_t c_start,t_start, c_end,t_end, c_end2,t_end2;
c_start = clock();
t_start = time(NULL) ;
pc = fork();
if (pc<0)
printf("error occurred!\n");
else if (pc==0)
{
printf("%s \n", filePath);
printf("Process ID:%d, Parent process ID: %d \n", getpid(), getppid());
timeConsumer1();
timeConsumer3();
c_end = clock();
t_end = time(NULL) ;
printf("The child process costed %f seconds.\n",difftime(t_end,t_start)) ;
printf("The child process costed %f clock cycles.\n",difftime(c_end,c_start)) ;
}
else
{
printf("This is parent process!!!!!!!!!!\n");
printf("Process ID:%d, Parent process ID: %d \n", getpid(), getppid());
timeConsumer2();
timeConsumer3();
timeConsumer1();
c_end = clock();
t_end = time(NULL) ;
printf("The entire test costed %.2f seconds.\n",difftime(t_end,t_start)) ;
printf("The entire test costed %.2f clock cycles.\n",difftime(c_end,c_start)) ;
return 0;
}
} |
|