Cuma, Mayıs 19, 2006

Client/Server1

Write a benchmark program to make a performance comparison of fork and thread
mechanisms. Your performance comparison will take into account only CPU overhead of
creating threads and child processes. The output of your program should be something like this:
“Creating a thread is %x faster than creating a child process using fork”.

#include "stdio.h"
#include "unistd.h"
#include "sys/types.h"
#include "pthread.h"
#include "sys/times.h"

#define THREADCYCLE 500000000
#define FORKCYCLE 500000000

void *print_message_function(void *ptr){
char *message;
message = (char *) ptr;

}

int main(){

struct tms endtime, starttime;
int iterator, return_pid;
pthread_t thread;
int iret;
char *message ="thread";

times(&starttime);
for( ; iterator < THREADCYCLE;){
while( iret == -1){
iret = pthread_create( &thread, NULL, print_message_function, (void*) message);
printf("error");
}
iterator ++;
}
times(&endtime);
printf("bu kadar %d\n",(endtime.tms_utime - starttime.tms_utime));

times(&starttime);
for(iterator =0; iterator < FORKCYCLE;){
while(return_pid == -1){
return_pid = fork();
}
iterator ++;
}
if(return_pid == 0){
exit(0);
}
times(&endtime);
printf("bu kadar %d\n", (endtime.tms_utime - starttime.tms_utime));
}

Hiç yorum yok: