forking. sharing global data in childs


 
Thread Tools Search this Thread
Top Forums Programming forking. sharing global data in childs
# 1  
Old 12-03-2008
Error forking. sharing global data in childs

hi, i want to write a code for forking 3 4 child. n wants that every child process one of the account from global account list. i wrote a program for that, but problem is every child is processing every account in list. what can me done to avoid it.
attaching code with it

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);
}

# 2  
Old 12-04-2008
maybe you can use thread to instead.
# 3  
Old 12-04-2008
thanks,
actually i done it with threads. but i just wanted to try it with forking i.e. parent child.
any ways thanks
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Solaris

Global and non-global zone resource sharing - tricky

hi all, Just a simple question but i cant get the answers in the book - In my globalzone , assuming i have 4 cpus (psrinfo -pv = 0-3), if i set dedicated-cpu (ncpus=2) for my local zone Is my globalzone left with 2 cpus or still 4 cpus ? Does localzone "resource reservation.e.g. cpu in... (6 Replies)
Discussion started by: javanoob
6 Replies

2. Programming

Multi thread data sharing problem in uclinux

hello, I have wrote a multi thread application to run under uclinux. the problem is that threads does not share data. using the ps command it shows a single process for each thread. I test the application under Ubuntu 8.04 and Open Suse 10.3 with 2.6 kernel and there were no problems and also... (8 Replies)
Discussion started by: mrhosseini
8 Replies

3. Solaris

sharing a directory between local and global zone

is this the step? add fs set dir=/export set special=/export set type=lofs add options rw end i notice i can't post immediately, moderator needs to moderate. i have 1 more post still haven't appear in the forum..hmm.... (1 Reply)
Discussion started by: binary0011
1 Replies

4. Shell Programming and Scripting

data sharing between process

hi ! i want to make 2 c prog such tht if i give an input in 1st prog then i can use tht input in 2nd. so over all i want to do data sharing between process using files. plz give me suggestions how can i achieve this ? thanks ya! (2 Replies)
Discussion started by: Sgupta
2 Replies

5. Programming

Need help with fork, forking multiple childs and shared memory

Hi all, I m writing an application, where i need to fork multiple childs and those child should handle particular task given to them. More descriptive. For example, suppose i have 4 Network, each network has multiple nodes. Now on the basis of network child should be forked and these child... (8 Replies)
Discussion started by: helpmeforlinux
8 Replies

6. Solaris

Windows/Solaris data sharing

Hi all, I have a request from Developer team in my compagny, they would like to be able to share data between unix and windows world. 1. We would like to be able to see Unix data from Windows : ?Samba ? 2 We would like to be able to see windows data from Solaris (Mount point) : ?NFS server... (4 Replies)
Discussion started by: unclefab
4 Replies

7. IP Networking

sharing of IP address for load sharing avoiding virtual server & redirection machine

I have RedHat 9.0 installed on three of my servers (PIII - 233MHz) and want that they share a common IP address so that any request made reaches each of the servers. Can anyone suggest how should I setup my LAN. I'm new to networking in Linux so please elaborate and would be thankful for a timely... (2 Replies)
Discussion started by: Rakesh Ranjan
2 Replies

8. UNIX for Advanced & Expert Users

mmap vs shared memory - which is best for sharing data between applications?

Between mmap and shared memory which is the best method of sharing data between multiple applications, interms of speed? (2 Replies)
Discussion started by: nmds
2 Replies
Login or Register to Ask a Question