(ipc.info.gz) example

Info Catalog (ipc.info.gz) Overview (ipc.info.gz) Overview (ipc.info.gz) perms
 
 1.2 example
 ===========
 
 Here is a code fragment with pointers on how to use shared memory. The
 same methods are applicable to other resources.
 
    In a typical access sequence the creator allocates a new instance of
 the resource with the `get' system call using the IPC_CREAT flag.
 
 creator process:
      #include <sys/shm.h>
      int id;
      key_t key;
      char proc_id = 'C';
      int size = 0x5000;	/* 20 K */
      int flags = 0664 | IPC_CREAT;		/* read-only for others */
 
      key = ftok ("~creator/ipckey", proc_id);
      id = shmget (key, size, flags);
      exit (0);	/* quit leaving resource allocated */
 
 Users then gain access to the resource using the same key.
 Client process:
      #include <sys/shm.h>
      char *shmaddr;
      int id;
      key_t key;
      char proc_id = 'C';
 
      key = ftok ("~creator/ipckey", proc_id);
 
      id = shmget (key, 0, 004);		/* default size   */
      if (id == -1)
            perror ("shmget ...");
 
      shmaddr = shmat (id, 0, SHM_RDONLY); /* attach segment for reading */
      if (shmaddr == (char *) -1)
            perror ("shmat ...");
 
      local_var = *(shmaddr + 3); 	/* read segment etc. */
 
      shmdt (shmaddr);		/* detach segment */
 
 When the resource is no longer needed the creator should remove it.
 Creator/owner process 2:
      key = ftok ("~creator/ipckey", proc_id)
      id = shmget (key, 0, 0);
      shmctl (id, IPC_RMID, NULL);
 
Info Catalog (ipc.info.gz) Overview (ipc.info.gz) Overview (ipc.info.gz) perms
automatically generated by info2html