Δημοσιεύτηκε: 04 Ιούλ 2011, 11:06
Εάν βρεις και καλύτερο οκ! Απλά, το παρακάτω είναι η άσκηση που έιχα κάνει...
- Κώδικας: Επιλογή όλων
#include <sys/time.h>
#include <signal.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <sys/time.h>
#define N 40000
#define M 60000
#define K 50000
int yourfunction( void );
int myfunction( void );
int parentfunction( void );
//-------------------------------------------------------------------------------------------------------------------------------------
static long p_realt_secs=0;
static long p_virtt_secs=0;
static long p_proft_secs=0;
static long c1_realt_secs=0;
static long c1_virtt_secs=0;
static long c1_proft_secs=0;
static long c2_realt_secs=0;
static long c2_virtt_secs=0;
static long c2_proft_secs=0;
static struct itimerval p_realt, c1_realt, c2_realt;
static struct itimerval p_virtt, c1_virtt, c2_virtt;
static struct itimerval p_proft, c1_proft, c2_proft;
//-------------------------------------------------------------------------------------------------------------------------------------
static void p_handler(int signo){
switch(signo)
{
case SIGALRM:
//printf("PARENT RECEIVED SIGNAL\n");
p_realt_secs++;
case SIGVTALRM:
//printf("PARENT RECEIVED SIGNAL\n");
p_virtt_secs++;
case SIGPROF:
//printf("PARENT RECEIVED SIGNAL\n");
p_proft_secs++;
}
}
static void c1_handler(int signo){
switch(signo){
case SIGALRM:
//printf("CHILD1 RECEIVED SIGNAL\n");
c1_realt_secs++;
case SIGVTALRM:
//printf("CHILD1 RECEIVED SIGNAL\n");
c1_virtt_secs++;
case SIGPROF:
//printf("CHILD1 RECEIVED SIGNAL\n");
c1_proft_secs++;
}
}
static void c2_handler(int signo){
switch(signo)
{
case SIGALRM:
//printf("CHILD2 RECEIVED SIGNAL\n");
c2_realt_secs++;
case SIGVTALRM:
//printf("CHILD2 RECEIVED SIGNAL\n");
c2_virtt_secs++;
case SIGPROF:
//printf("CHILD2 RECEIVED SIGNAL\n");
c2_proft_secs++;
}
}
//--------------------------------------------------------------------------------------------------------------------------------------
main(void)
{
int pid1,pid2;
int status;
/* get command line argument, funarg ...*/
/*initialize parent, child1, and child2 timer values ...*/
p_realt.it_interval.tv_sec = 1; // to pedio it_value = orizei thn trexoysa timh gia ton xronometrhth
p_realt.it_value.tv_sec = 1; // xronometrhth otan mhdenistei
p_virtt.it_interval.tv_sec = 1;
p_virtt.it_value.tv_sec = 1;
p_proft.it_interval.tv_sec = 1;
p_proft.it_value.tv_sec = 1;
//------------------------------------------------------------------------------------------------------------------------------------
c1_realt.it_interval.tv_sec = 1;
c1_realt.it_value.tv_sec = 1;
c1_virtt.it_interval.tv_sec = 1;
c1_virtt.it_value.tv_sec = 1;
c1_proft.it_interval.tv_sec = 1;
c1_proft.it_value.tv_sec = 1;
//------------------------------------------------------------------------------------------------------------------------------------
c2_realt.it_interval.tv_sec = 1;
c2_realt.it_value.tv_sec = 1;
c2_virtt.it_interval.tv_sec = 1;
c2_virtt.it_value.tv_sec = 1;
c2_proft.it_interval.tv_sec = 1;
c2_proft.it_value.tv_sec = 1;
//-------------------------------------------------------------------------------------------------------------------------------------
/*enable your signal handlers for the parent ...*/
signal(SIGALRM,p_handler);
signal(SIGVTALRM,p_handler);
signal(SIGPROF,p_handler);
/*set the parent 's itimers ...*/
setitimer(ITIMER_REAL, &p_realt, NULL);
setitimer(ITIMER_VIRTUAL, &p_virtt, NULL);
setitimer(ITIMER_PROF, &p_proft, NULL);
pid1=fork();
if( pid1 == 0 ) // an einai mhden ektelw ton kwdika toy piadioy alliws toy patera
{
/*enable child 1 signal handlers (disable parent handlers) */
signal(SIGALRM,c1_handler);
signal(SIGVTALRM,c1_handler);
signal(SIGPROF,c1_handler);
/*set the 1 itimers ...*/
setitimer(ITIMER_REAL, &c1_realt, NULL);
setitimer(ITIMER_VIRTUAL, &c1_virtt, NULL);
setitimer(ITIMER_PROF, &c1_proft, NULL);
/*start yourfunction() at child 1 ..."res=yourfunction(funarg);"*/
yourfunction();
/*Read the child itimer values, and report them ...*/
getitimer(ITIMER_PROF, &c1_realt);
getitimer(ITIMER_REAL, &c1_virtt);
getitimer(ITIMER_VIRTUAL, &c1_proft);
printf("\n");
printf("Child 1 real time=%Id sec\n", c1_realt_secs);
printf("Child 1 cpu time=%Id sec\n", c1_proft_secs);
printf("Child 1 user time=%Id sec\n", c1_virtt_secs);
printf("Child 1 kernel time=%Id sec\n", c1_proft_secs-c1_virtt_secs);
fflush(stdout);
exit(0);
}
else
{
pid2=fork();
if( pid2 == 0 )
{
/* enable the child 2 signal handlers (disable parent handlers ...*/
signal(SIGALRM,c2_handler);
signal(SIGVTALRM,c2_handler);
signal(SIGPROF,c2_handler);
/*set the child 2 itimers ...*/
setitimer(ITIMER_REAL, &c2_realt, NULL);
setitimer(ITIMER_VIRTUAL, &c2_virtt, NULL);
setitimer(ITIMER_PROF, &c2_proft, NULL);
myfunction();
getitimer(ITIMER_PROF, &c2_realt);
getitimer(ITIMER_REAL, &c2_virtt);
getitimer(ITIMER_VIRTUAL, &c2_proft);
printf("\n");
printf("Child 2 real time=%Id sec\n", c2_realt_secs);
printf("Child 2 cpu time=%Id sec\n", c2_proft_secs);
printf("Child 2 user time=%Id sec\n", c2_virtt_secs);
printf("Child 2 kernel time=%Id sec\n", c2_proft_secs-c1_virtt_secs);
fflush(stdout);
exit(0);
}
else /* this is the parent */
{
/* start your function at the parent */
parentfunction();
/*wait for the children to terminate */
waitpid(pid1,&status,0);
waitpid(pid2,&status,0);
/* read the parent itimer values and report them */
getitimer(ITIMER_PROF, &p_realt);
getitimer(ITIMER_REAL, &p_virtt);
getitimer(ITIMER_VIRTUAL, &p_proft);
printf("\n");
printf("Parent real time=%Id sec\n", p_realt_secs);
printf("Parent cpu time=%Id sec\n", p_proft_secs);
printf("Parent user time=%Id sec\n", p_virtt_secs);
printf("Parent kernel time=%Id sec\n\n", p_proft_secs-c1_virtt_secs);
}
//printf("this line should never be printed\n");
}
return;
}
//--------------------------------------------------------------------------------------------------------------------------------------
int yourfunction( void )
{
int *x,i,j,T,b;
x=(int*)malloc(N*sizeof(int));
for(i=0; i<N; i++)
{
x[i]=((int)(RAND_MAX)-((int)rand() ) );
}
for(i=0; i<N-1; i++)
{
for(j=0; j<(N-1)-i; j++)
{
if( x[j+1] < x[j] )
{
T=x[j];
x[j]=x[j+1];
x[j+1]=T;
}
}
}
return;
}
//--------------------------------------------------------------------------------------------------------------------------------------
int myfunction( void )
{
int *x,i,j,T,b;
x=(int*)malloc(M*sizeof(int));
for(i=0; i<M; i++)
{
x[i]=((int)(RAND_MAX)-((int)rand() ) );
}
for(i=0; i<M-1; i++)
{
for(j=0; j<(M-1)-i; j++)
{
if( x[j+1] < x[j] )
{
T=x[j];
x[j]=x[j+1];
x[j+1]=T;
}
}
}
return;
}
//-------------------------------------------------------------------------------------------------------------------------------------
int parentfunction( void )
{
int *x,i,j,T,b;
x=(int*)malloc(K*sizeof(int));
for(i=0; i<K; i++)
{
x[i]=((int)(RAND_MAX)-((int)rand() ) );
}
for(i=0; i<K-1; i++)
{
for(j=0; j<(K-1)-i; j++)
{
if( x[j+1] < x[j] )
{
T=x[j];
x[j]=x[j+1];
x[j+1]=T;
}
}
}
return;
}
//---------------------------------------------------------------------------------------------------------------------------------------