Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#define nProcess 2
struct timeval tp1;
struct timeval tp2;
long proc_id_A[5];
char filename[100];
static unsigned int trx_nr=0;
int *prt=&trx_nr;
struct record_log
{
int account_id;
};
typedef struct record_log record_log;
static record_log records[] = {85013,86709,86712,84966,84955};
static record_log *trx;
void get_transaction()
{
trx=&records[*prt];
(*prt)++;
}
void start_processing();
main()
{
int i,i_pid,i_no_of_process=0,i_wait_count=0;
int *i_stat_loc = NULL ;
void *pZero = 0;
if(gettimeofday(&tp1,pZero) == -1)
{
perror("First: ");
exit(0);
}
for(i=0;i<nProcess ;i++)
{
if((i_pid=fork())<0)
{
printf("\nFailed to execute instance %d\n",i_no_of_process);
return(1);
}
else if(i_pid==0)
{
start_processing();
}
else
{
proc_id_A[i]=i_pid;
i_no_of_process++;
}
}
if(i == nProcess )
{
printf("\n\nNo of process initiated = %d",i_no_of_process);
printf("\nSequence Process Id Wait Process Id Status\n\n");
for(i_wait_count=0;i_wait_count<i_no_of_process;i_wait_count++)
{
printf("%-8d %ld %21d %9d\n",i_wait_count+1,proc_id_A[i_wait_count],
waitpid(proc_id_A[i_wait_count],i_stat_loc ,WCONTINUED),i_stat_loc);
if((i_stat_loc)!=0)
{
printf("Process [Id=%ld] has failed\n",proc_id_A[i_wait_count]);
return(1);
}
}
}
if(gettimeofday(&tp2,pZero) == -1)
{
perror("Second: ");
exit(0);
}
printf(" \n\nTHE TOTAL = %f",(float)(tp2.tv_sec - tp1.tv_sec) + ((float)(tp2.tv_usec - tp1.tv_usec)/1000000.0));
printf("\n\nEnd Of Main Function\n");
}
void start_processing()
{
int num_ret,total_records = 0,tot_recs_fetched=0,records_fetched;
int not_complete = 1;
while (*prt < (sizeof(records)/sizeof(record_log)))
{
get_transaction();
C_INIT(filename);
sprintf(filename,"%s_%d.out","child",trx->account_id);
printf("\nfile name is : %s : %d %d",filename, getpid(),*prt);
/* after creating file for each account, code will write account data in file */
}
printf("\n\nOut of chield %d",getpid());
exit(0);
}