It is currently Sat, 26 Nov 2022 07:09:25 GMT



 
Author Message
 fork + accessing global variable

Hi all,

I developped a program in C, and using fork sistem call, in order to get faster prosessing.
But I encounter a problem to acces a global variable from child process. The global variable is pointer to link-list. I tried to manipulate the list form a module and travers it from the other module.

Does it have any difference to access a global variable from child prosess like this ?
Any sugestion would be great
I list the brief code below:
**************
/* header.h  */

struct CLIENTS{
    char *clientname;
    struct CLIENTS *next;

**************
/* file1.c  */

#include <header.h>
struct CLIENTS *client_list;
...
rad_accounting(authreq, activefd){

    client = client_list;
    while(client != NULL){
        if(client->ipaddr == authreq->ipaddr)
            break;
        client = client->next;
    }
    if(!client) {
        tempclient = (struct CLIENTS *)malloc(sizeof(struct CLIENTS));
        tempclient->ipaddr = authreq->ipaddr;
        tempclient->clientname = (char *)strdup(clientname);
        tempclient->topline = -1;
        tempclient->sessionmarkwas = 0;
        tempclient->user = (struct USERS *)NULL;
        tempclient->next = (struct CLIENTS *)client_list;
        client_list = tempclient;
        client = tempclient;
    }
....
**************
/* file2.c
...
#include <header.h>
extern struct CLIENTS *client_list;
...
   if(spawn_flag) {
       acct_pid = fork();
       if(acct_pid < 0) {
           log_err("could not fork to spawn accounting daemon\n");
           rad_exit(-1);
       }
   if(acct_pid > 0) {
       close(acctfd);
       acctfd = -1;
   }else {
       close(sockfd);
       sockfd = -1;
   }
  /*
  *      Receive user requests
  */
  sin = (struct sockaddr_in *) & saremote;
  if(sockfd != -1) {
      update_clients();
  }
  for(;;) {
      FD_ZERO(&readfds);
      if(sockfd >= 0) {
          FD_SET(sockfd, &readfds);
      }
      if(acctfd >= 0) {
          FD_SET(acctfd, &readfds);
      }
      status = select(32, &readfds, NULL, NULL, (struct timeval *)NULL);
          if(sockfd != -1) {
              update_clients();
          }  
          if(status == -1) {
              if (errno == EINTR)
                  continue;
                  sig_fatal(101);
          }
          if(sockfd >= 0 && FD_ISSET(sockfd, &readfds)) {  
                  rad_request(sockfd);
          }
          if(acctfd >=0 && FD_ISSET(acctfd, &readfds)) {
                  rad_request(acctfd);
          }
        }  
...
rad_request
...
        case PW_AUTHENTICATION_REQUEST:
                if(spawn_flag) {
                        rad_spawn_child(authreq, activefd);
                }else {
                        rad_authenticate(authreq, activefd);
                }
                break;
        case PW_ACCOUNTING_REQUEST:
                rad_accounting(authreq, activefd);
                break;
#ifdef PASSCHANGE
        case PW_PASSWORD_REQUEST:
                rad_passchange(authreq, activefd);
                break;
#endif
        case PW_SPECIAL_REQUEST:
                log_err("radrespond : PW_SPECIAL\n");
                rad_special_req(authreq, activefd);
                break;
...
rad_special_req(authreq, activefd) {
...
        Client=client_list;
        if(client_list==NULL){
          log_err("SendACK: Client NULL");
        }
....



 Tue, 25 Jul 2000 03:00:00 GMT   
 fork + accessing global variable

 Erri> I developped a program in C, and using fork sistem call, in
 Erri> order to get faster prosessing.  But I encounter a problem to
 Erri> acces a global variable from child process.  The global
 Erri> variable is pointer to link-list. I tried to manipulate the
 Erri> list form a module and travers it from the other module.

This is probably a mistake. When you fork, the process's memory is
not *shared* with the child process, it's *copied*. Changes made in
the child process are not visible to the parent.

Please don't post in HTML. Also, saying you have encountered "a problem"
is not helpful - be specific about what the problem is.

--
Andrew.

comp.unix.programmer FAQ: see <URL: http://www.erlenstar.demon.co.uk/unix/>
                           or <URL: http://www.whitefang.com/unix/>



 Tue, 25 Jul 2000 03:00:00 GMT   
 
   [ 2 post ] 

Similar Threads

1. fork, parent, child and global variables

2. Global variables and fork

3. access global data in child with fork()?

4. accessing a global variable

5. Unaligned access (bus error) on Solaris on global variable

6. Parent and Child process accessing global variable?

7. Accessing global variables from shared/dynmaic functions.

8. file contents into variable, or global variable from shell script

9. korn shell variable in variable access


 
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software