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
fork(2) 							System Calls Manual							   fork(2)

Name
       fork - create a new process

Syntax
       #include <sys/types.h>
       #include <unistd.h>

       pid = fork()
       pid_t pid;

Description
       The  system  call causes creation of a new process.  The new process (child process) is an exact copy of the calling process except for the
       following:

       o    The child process has a unique process ID.

       o    The child process has a different parent process ID (that is, the process ID of the parent process).

       o    The child process has its own copy of the parent's descriptors.  These descriptors reference the same underlying objects, so that, for
	    instance,  file  pointers  in file objects are shared between the child and the parent, so that a on a descriptor in the child process
	    can affect a subsequent read or write by the parent.  This descriptor copying is also used by the shell to	establish  standard  input
	    and output for newly created processes as well as to set up pipes.

       o    The child processes resource utilizations are set to 0.  For further information, see

Return Values
       Upon  successful  completion,  returns  a  value  of  0	to the child process and returns the process ID of the child process to the parent
       process.  Otherwise, a value of -1 is returned to the parent process, no child process is created, and the global variable errno is set	to
       indicate the error.

Diagnostics
       The system call fails and no child process are created under the following conditions:

       [EAGAIN]       The system-imposed limit {PROC_MAX} on the total number of processes under execution would be exceeded.

       [EAGAIN]       The system-imposed limit {CHILD_MAX} on the total number of processes under execution by a single user would be exceeded.

       [ENOMEM]       There is insufficient swap space for the new process.

See Also
       execve(2), wait(2)

																	   fork(2)
All times are GMT -4. The time now is 05:40 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy