LinuxQuestions.org
Visit Jeremy's Blog.
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 09-03-2004, 11:46 PM   #1
brunnopessoa
Member
 
Registered: Nov 2003
Location: Rio de Janeiro, Brazil
Distribution: Fedora
Posts: 67

Rep: Reputation: 15
IPC Memory Share - C Program - Why not exiting for(;;) ??


Hi all!

I have two C program sources: PROC1.C and PROC2.C. Both share the same memory region, defined by t_info struct.

I only would like to know why it does NOT leave the FOR's when I type "sair" in the command line.

To run the program, you need pthread library and start first PROC1.C.

Tks a lot!
BP


/********* PROC1.C ********/


#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/errno.h>
#include <pthread.h>

typedef struct
{
int ok1, ok2;
char texto1[128], texto2[128];
}t_info;

extern int errno;
char *pgname;
t_info *info;

void * thread_escreve( void * arg ) {

for(;; ) {

printf("Digite um texto (proc1): ");

fgets( info->texto1, 128, stdin );
if( strcmp( info->texto1, "sair" ) == 0 ) {
info->ok1 = -1;
break;
} else {
info->ok1 = 1 ;
}
}
}


int main (int argc, char *argv[])
{
int md;
pthread_t tid;

pgname = argv[0];

// Criando a area de memoria compartilhada.
if ((md = shmget(8752, sizeof(t_info),0700|IPC_CREAT|IPC_EXCL)) == -1)
{
if (errno == EEXIST)
fprintf (stderr, "%s:Area de memoria ja existe.", pgname);
else
perror (pgname);
exit (-1);
}

// Mapeando a area de memoria compartilhada
if ((info = shmat (md, NULL, 0)) == (void *) -1)
{
perror (pgname);
exit (-1);
}

pthread_create( &tid, NULL, thread_escreve, NULL );

info->ok2 = 0;

for( ;; ) {

while (info->ok2 == 0)
/* aguarda */;
if( info->ok2 == -1 )
break;
printf ("Texto (proc1): %s", info->texto2);
info->ok2 = 0;
}

pthread_join( tid, NULL );

// Desfazendo o mapeamento
if (shmdt ((char *) info) == -1)
{
perror (pgname);
exit (-1);
}

// Destruindo a area de memoria
if (shmctl (md, IPC_RMID, NULL) == -1)
{
perror (pgname);
exit (-1);
}
}



/*********** PROC2.C ***********/

#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/errno.h>
#include <pthread.h>

typedef struct
{
int ok1, ok2;
char texto1[128], texto2[128];
}t_info;

extern int errno;
char *pgname;
t_info * info;

void * thread_escreve( void * argumento ) {
for( ;; ) {
printf( "Digite um texto (proc2): " );

fgets( info->texto2, 128, stdin );
if( strcmp( info->texto2, "sair" ) == 0 ) {
info->ok2 = -1 ;
break;
} else {
info->ok2 = 1 ;
}
}
}

int main (int argc, char *argv[])
{
int md;
pthread_t tid;

pgname = argv[0];

// Obtendo o identificador da area de memoria compartilhada.
if ((md = shmget(8752, sizeof(t_info),0700)) == -1)
{
perror (pgname);
exit (-1);
}

// Mapeando a area de memoria compartilhada
if ((info = shmat (md, NULL, 0)) == (void *) -1)
{
perror (pgname);
exit (-1);
}

info->ok1 = 0;

pthread_create( &tid, NULL, thread_escreve, NULL );


for( ;; ) {

while( info->ok1 == 0 ) ;

if( info->ok1 == -1 )
break;
printf( "Texto (proc2): %s", info->texto1 );
info->ok1 = 0 ;
}

pthread_join( tid, NULL );

// Desfazendo o mapeamento
if (shmdt ((char *) info) == -1)
{
perror (pgname);
exit (-1);
}
}

Last edited by brunnopessoa; 09-03-2004 at 11:50 PM.
 
Old 09-04-2004, 01:29 AM   #2
infamous41md
Member
 
Registered: Mar 2003
Posts: 804

Rep: Reputation: 30
probably b/c fgets() includes the newline in the buffer you read in, and thus the strcmp() will not be equal. use strncmp() or chomp the newline.
 
Old 09-05-2004, 12:41 AM   #3
brunnopessoa
Member
 
Registered: Nov 2003
Location: Rio de Janeiro, Brazil
Distribution: Fedora
Posts: 67

Original Poster
Rep: Reputation: 15
Could you give me a tip in C code, please?

Tks in advance!!!
 
Old 09-05-2004, 03:39 AM   #4
bruce ford
Member
 
Registered: Jul 2004
Location: Munich, Germany
Distribution: Sun Solaris 8, SuSE 9.0
Posts: 43

Rep: Reputation: 15
hi,

infamous41md is right! Change your 2 strcmps to
Code:
strcmp( info->texto1, "sair\n" )
and to
Code:
strcmp( info->texto2, "sair\n" )
and it works.

so long...
bruce

Last edited by bruce ford; 09-05-2004 at 03:40 AM.
 
Old 09-05-2004, 09:27 PM   #5
brunnopessoa
Member
 
Registered: Nov 2003
Location: Rio de Janeiro, Brazil
Distribution: Fedora
Posts: 67

Original Poster
Rep: Reputation: 15
Tks a lot, guys!
 
  


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
Samba share problems (IPC$) cuschu Linux - Software 10 02-22-2006 04:12 AM
IPC msgrcv - writing overflows memory extra 4 bytes tara Programming 1 11-08-2005 11:40 PM
IPC program sis650 Programming 1 04-27-2005 06:09 PM
Standard Way To Share Memory Among Processes? Sys-V IPC? overbored Programming 1 06-21-2003 01:33 PM
IPC Shared Memory support in kernel? stevho Linux - General 1 01-17-2002 07:48 PM

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

All times are GMT -5. The time now is 12:40 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