LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-12-2010, 08:55 AM   #1
vyshakh.krishnan
LQ Newbie
 
Registered: May 2010
Posts: 2

Rep: Reputation: 0
use of extern in c


hi,

In a c program the value of an integer variable is changing rapidly. I have to get the value of that variable at a particular instant from another program. How can i do it without using a file?

When i tried with extern variable as

prgm 1: update.c:

#include<stdio.h>
#include "header.h"

main()
{
extern int VAR_A, VAR_B;
while(1)
{
VAR_A++;
VAR_B++;
}
}

pgm 2: print.c:

#include<stdio.h>
#include "header.h"

main()
{
extern int VAR_A, VAR_B;
while(1)
{
printf("\nValue of variables are %d %d\n",VAR_A,VAR_B);
}
}

header.h:

#ifndef HEADER_H
#define HEADER_H

int VAR_A, VAR_B;

#endif

i execute the both .c file from 2 terminals but got only value 0 printed for both var_a and var_b all the time.

regards
vyshakh
 
Old 05-12-2010, 09:05 AM   #2
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
extern does not mean the variable is in another program.

It typically means the variable is in another source module of the same program. But it doesn't even need to mean another source module. It could be in the same source module.

You need shared memory for the type of communication between programs that you seem to want.

A google or forum search for linux shared memory should give you many simple examples of setting up and using shared memory.
 
Old 05-12-2010, 11:54 AM   #3
vyshakh.krishnan
LQ Newbie
 
Registered: May 2010
Posts: 2

Original Poster
Rep: Reputation: 0
thank u very much for ur reply........
 
Old 05-12-2010, 01:20 PM   #4
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Quote:
Originally Posted by johnsfine View Post
You need shared memory for the type of communication between programs that you seem to want.
I think you can be more general than shared memory. You need to use Interprocess Communication, IPC. For a very good primer on the subject, see Beej's Guide to Unix Interprocess Communication.
This should give you enough understanding of the process to choose a method that is appropriate for your purpose, as well as enough working sample code to get you started.

--- rod.
 
Old 05-12-2010, 02:12 PM   #5
Dogs
Member
 
Registered: Aug 2009
Location: Houston
Distribution: Slackware 13.37 x64
Posts: 105

Rep: Reputation: 25
Quote:
Originally Posted by theNbomr View Post
I think you can be more general than shared memory. You need to use Interprocess Communication, IPC. For a very good primer on the subject, see Beej's Guide to Unix Interprocess Communication.
This should give you enough understanding of the process to choose a method that is appropriate for your purpose, as well as enough working sample code to get you started.

--- rod.

Thank you so much! I love finding awesome Linux guides in PDF!

Last edited by Dogs; 05-12-2010 at 02:20 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
using extern variable kranti Programming 9 03-04-2006 05:30 PM
extern when to use or not alaios Programming 7 07-14-2005 05:47 PM
cannot find EXTERN.h chandan@bio Mandriva 0 04-10-2005 09:58 AM
extern between C and C++ linfan Programming 1 11-04-2003 05:08 PM
extern variables in c++ abi_sh Programming 1 08-11-2002 01:42 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:15 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration