Sponsored Content
Top Forums Programming forking. sharing global data in childs Post 302264007 by anup13 on Wednesday 3rd of December 2008 01:58:27 AM
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);
}

 

8 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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
timeradd(3C)						   Standard C Library Functions 					      timeradd(3C)

NAME
timeradd, timersub, timerclear, timerisset, timercmp - operations on timeval structures SYNOPSIS
#include <sys/time.h> void timeradd(struct timeval *a, struct timeval *b, struct timeval *res); void timerclear(struct timeval *tvp); int timercmp(struct timeval *a, struct timeval *b, CMP); int timerisset(struct timeval *tvp); void timersub(struct timeval *a, struct timeval *b, struct timeval *res); DESCRIPTION
These macros are provided for manipulating timeval structures for use with gettimeofday(3C) and settimeofday(3C) operands. The structure is defined in <sys/time.h> as: struct timeval { long tv_sec; /* seconds since Jan. 1, 1970 */ long tv_usec; /* and microseconds */ }; The timeradd() macro adds the time information stored in a to b and stores the resulting timeval in res. The results are simplified such that the value of res->tv_usec is always less than 1,000,000 (1 second). The timersub() macro subtracts the time information stored in b from a and stores the resulting timeval in res. The timerclear() macro initializes tvp to midnight (0 hour) January 1st, 1970 (the Epoch). The timerisset() macro returns true if tvp is set to any time value other than the Epoch. The timercmp() macro compares a to b using the form a CMP b, where CMP is one of <, <=, ==, !=, >=, or >. USAGE
These macros are not available in function form. All of these macros evaluate their arguments more than once. If parameters passed to these macros are expressions with side effects, the results are undefined. ATTRIBUTES
See attributes(5) for descriptions of the following attributes: +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |Interface Stability |Committed | +-----------------------------+-----------------------------+ |MT-Level |MT-Safe with Exceptions | +-----------------------------+-----------------------------+ SEE ALSO
gettimeofday(3C), attributes(5) SunOS 5.11 12 Jun 2008 timeradd(3C)
All times are GMT -4. The time now is 11:03 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy