Welcome to pickSourcecode.com Login | Register    
pickZy.com
 Home  | search  | games  | General  | C  | C++  | Java  | Php  | Networking  | Visual Basic  | VC++  | Win32  | MFC  | JavaScript  | Jobs  | JavaScript  | Post jobs
C program using System V shared memory segments in linux       Share
2009-07-03 |  RatheeshTR  | Viewed: 709  |    0

//  An  example  C  program  using  System  V  shared  memory  segments  follows.  
It computes Pi, using the same algorithm given in section 1.3.


#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ipc.h>
#include <sys/shm.h>

volatile struct shared { double pi; 
int lock; } *shared;

inline extern 
int xchg(register int reg,
volatile 
int * volatile obj)
{

  
/*  Atomic  exchange  instruction  */
__asm__ __volatile__ ("xchgl %1,%0"

                      :"=r" (reg), "=m" (*obj)

                      :"r" (reg), "m" (*obj));

  return(reg);

}


main(
int argc, char **argv)
{

  register double width, localsum;

  register 
int intervals, i;
  register 
int shmid;
  register 
int iproc = 0;;

  
/*  Allocate  System  V  shared  memory  */
  shmid = shmget(IPC_PRIVATE,

                 sizeof(struct shared),

                 (IPC_CREAT | 0600));

  shared = ((volatile struct shared *) shmat(shmid, 0, 0));

  shmctl(shmid, IPC_RMID, 0);


  
/*  Initialize...  */
  shared->pi = 0.0;

  shared->lock = 0;


  
/*  Fork  a  child  */
  
if (!fork()) ++iproc;

  
/*  get  the  number  of  intervals  */
  intervals = atoi(argv[1]);

  width = 1.0 / intervals;


  
/*  do  the  local  computations  */
  localsum = 0;

  
for (i=iproc; i<intervals; i+=2) {
    register double x = (i + 0.5) * width;

    localsum += 4.0 / (1.0 + x * x);

  }

  localsum *= width;


  
/*  Atomic  spin  lock,  add,  unlock...  */
  while (xchg((iproc + 1), &(shared->lock))) ;  

  shared->pi += localsum;

  shared->lock = 0;


  
/*  Terminate  child  (barrier  sync)  */
  
if (iproc == 0) {
    wait(NULL);

    printf("Estimation of pi is %f\n", shared->pi);

  }


  
/*  Check  out  */
  return(0);

}


//  http://www.ecn.purdue.edu/~pplinux/PPHOWTO/pphowto-2.html


Latest topics
C - pointers to pointers  Viewed: 263
C - switch statement  Viewed: 264
Malloc Debugger  Viewed: 264
Is a prime number or not  Viewed: 264
C - Do while  Viewed: 265
C - Global variables , static variables  Viewed: 265
C - binsearch  Viewed: 265
C - binary tree  Viewed: 265
C Assign content to a function pointer  Viewed: 265
C - Structure example  Viewed: 266

Comments:





Submit comment's

Type:

User Comment's:

Submitted By:
Prof: Software Engineer
Tech: C ,Cpp
Send Mail: ratheesh



Related topics
C - Data Structure,Rev Linked list
C - Data Structure,linked list
C - time_t strftime example
C - time_t gmtime example
C - enum example
C - strtok example, Splitting string into tokens
C - libxml2 parsing example libxml
C - if else example
C - Structure example
C - pointers and arrays example strlen
C - Pointers to structures
C - gtk scrolled window example
C - Binary Search Tree example
C TCP socket example Server and Client program
C UDP socket example Server and Client program

Related References
c - basics of structure
c - functions and program structure, programmed pass-by-reference via pointers, pass-by-reference parameters
c - structures and functions
c - arrays of structures
c - pointers to structures
explain function pointers with examples?

Web site contents © Copyright 2007, All rights reserved.
Help | Terms and Conditions | Privacy Policy | About Us