For Programmers: Free Programming Magazines  


Home > Archive > Unix Programming > May 2004 > Segmentation Fault









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Segmentation Fault
Xarky

2004-05-12, 9:08 pm

Hi,
I am writing a program on Linux(Mandrake 10) OS and it is working
perfect. Now I am trying to run it on a Sun OS and it is giving a
segmentation fault error when I am receiving messages from a message
queue.

The code currently being used is the following
msgrcv (msgid, &data, length, 1, 0)


Can someone help me fix the problem out, because I have no idea what
the cause could be.


Thanks in Advance
Fletcher Glenn

2004-05-12, 9:08 pm



Xarky wrote:
> Hi,
> I am writing a program on Linux(Mandrake 10) OS and it is working
> perfect. Now I am trying to run it on a Sun OS and it is giving a
> segmentation fault error when I am receiving messages from a message
> queue.
>
> The code currently being used is the following
> msgrcv (msgid, &data, length, 1, 0)
>
>
> Can someone help me fix the problem out, because I have no idea what
> the cause could be.
>
>
> Thanks in Advance


Lacking any context, your code fragment is useless. What
could be happening is that your data pointer is not pointing
to real memory, or your length value is greater than the size
of your data area.

--

Fletcher Glenn

John Gordon

2004-05-12, 9:08 pm

In <bc42e1a.0405101108.22d5ba40@posting.google.com> bernardpace@yahoo.com (Xarky) writes:

> The code currently being used is the following
> msgrcv (msgid, &data, length, 1, 0)


> Can someone help me fix the problem out, because I have no idea what
> the cause could be.


Without the full source code of your program, we can only guess at
your problem. Off the top of my head, it sounds like the buffer
for receiving the message isn't big enough, or isn't initialized
correctly. But it could be anything, really.

And it doesn't necessarily mean much that the program worked on
Linux. Different machines and compilers work differently, and
your bug just happened to go unnoticed under Linux. In effect,
you got "lucky".

Rich Gibbs

2004-05-12, 9:08 pm

Xarky said the following, on 05/10/04 15:08:
> Hi,
> I am writing a program on Linux(Mandrake 10) OS and it is working
> perfect. Now I am trying to run it on a Sun OS and it is giving a
> segmentation fault error when I am receiving messages from a message
> queue.
>
> The code currently being used is the following
> msgrcv (msgid, &data, length, 1, 0)
>
>
> Can someone help me fix the problem out, because I have no idea what
> the cause could be.
>
>


Unfortunately, my new USB mind-reading device is not working yet; in any
case, I think you might get better answers if you showed us a bit more
of the program. (Ideally, what you want is a minimal example that
generates the error.)

However, segmentation faults on Suns are often caused by pointers that
don't point anywhere in the process address space. I'd suggest checking
that the pointer you are passing to 'msgrcv' is valid.


--
Rich Gibbs
rgibbs@his.com
Xarky

2004-05-12, 9:08 pm

bernardpace@yahoo.com (Xarky) wrote in message news:<bc42e1a.0405101108.22d5ba40@posting.google.com>...
Hi,
Sorry for not giving you so much detail. I hope that including
some main parts of my program, where the problem starts would help.

Thanks for viewing and helping in my problem.

/********* Server *********/

#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/wait.h>
#include <sys/types.h>

#define MAXLINE 256

struct msgq_data
{
long mtype;
pid_t client_pid;
int option;
int line_no;
char buff[MAXLINE+1];
}; // end struct msgq_data

int msgid; // having the unique identifier for the message queue

void error_msg (void)
// to give error messages set by errno
{
printf ("Error encountered: %s.\n", strerror(errno));
exit (EXIT_FAILURE);
} // end method error_msg

void remove_IPC()
// removing message queue
{
if (msgctl (msgid, IPC_RMID, NULL) == -1)
error_msg();
} // end method remove_IPC

void disconnet_server (int sig)
// to switch off server
{
remove_IPC();
exit (EXIT_SUCCESS);
} // end method disconnect_server

void kill_completely_child (int sig)
// to remove zombie processes
{
pid_t pid;
int data=-1;

pid = wait (&data);
if (pid == -1)
error_msg();

(void) signal (SIGCHLD, kill_completely_child);
} // end method kil_completely_child

void Client_Connections_Control (void)
{
struct msgq_data data;
int length;
pid_t pid;

length = sizeof(struct msgq_data) - sizeof(long);

while (1) // infinte loop, terminated only when swithcing off server
{
if (msgrcv (msgid, &data, length, 1, 0) == -1) // problem is
oocuring here, after receiving a message
{
remove_IPC();
error_msg();
} // end if

pid = fork();
if (pid == -1)
error_msg();

else if (pid == 0) // child stuff - handling request of client
handling_Client_Request(data, length);
// a method that handles options set by
clients

// parent will wait for another message and create a new process for
it
} // end while loop
} // end method Client_Connections_Control

int main()
{
key_t key;

key = ftok (".", 'a');
if (key == -1)
error_msg();

msgid = msgget (key, 0666 | IPC_CREAT);
if (msgid == -1)
error_msg();

printf ("Server connection established succesfully. Clients can start
to connect.\n\n");
(void) signal (SIGCHLD, kill_completely_child);
(void) signal (SIGINT, disconnet_server);

Client_Connections_Control();

return EXIT_SUCCESS;
} // end main

/******** end server *********/
//
/********* client **********/

#include <stdio.h>
#include <errno.h>
#include <ctype.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <signal.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/types.h>

#define MAXLINE 256

struct msgq_data
{
long mtype;
pid_t my_pid;
int option;
int line_no;
char buff[MAXLINE+1];
}; // end struct msgq_data

int msgid;



void connecting_to_server (void)
// trying to connect to server if on
// upon succesful, user is informed and gives him the main menu
{
key_t key;
int length;
struct msgq_data data;

length = sizeof(struct msgq_data) - sizeof(long);
system ("clear");
key = ftok(".", 'a'); // requesting a key
if (key == -1)
error_msg();

msgid = msgget (key, 0666); // msg cannot be created by client, but
given access
if (msgid == -1)
{
printf ("Cannot connect to server.... Please try again....\n\n");
exit (EXIT_FAILURE);
} // end if

data.mtype = 1; // to server
data.my_pid = getpid();
data.option = 1; // to connect

// on sending this message, server gives segmentation fault
if (msgsnd(msgid, &data, length, 0) == -1) // sending data to server
error_msg();

printf ("Checking if server has connected us....\n");
if (msgrcv (msgid, &data, length, getpid(), 0) == -1) // receiving
data from server
error_msg();

if (data.option != 0)
{
printf ("Server has not accepted us.\n");
exit (EXIT_SUCCESS);
} // end if

// 0 means that server accepted our connection
// need to wait for confirmation from server that connection is made
// server will send a 0 for confirmation.

printf ("Connnected to server succesfully....\n");
getchar();
} // end method connecting_to_server

nt main()
{
connecting_to_server();
// the menu is not reached, since when trying to connect, to
server
// clients sends message and server gives segementation fault
and
// clients keeps on waiting.


main_menu();
disconnect_from_server();

return EXIT_SUCCESS;
} // end main

/******* end client **********/


> Hi,
> I am writing a program on Linux(Mandrake 10) OS and it is working
> perfect. Now I am trying to run it on a Sun OS and it is giving a
> segmentation fault error when I am receiving messages from a message
> queue.
>
> The code currently being used is the following
> msgrcv (msgid, &data, length, 1, 0)
>
>
> Can someone help me fix the problem out, because I have no idea what
> the cause could be.
>
>
> Thanks in Advance

John Gordon

2004-05-12, 9:08 pm

In <bc42e1a.0405102140.789ea1a2@posting.google.com> bernardpace@yahoo.com (Xarky) writes:

> length = sizeof(struct msgq_data) - sizeof(long);


> while (1) // infinte loop, terminated only when swithcing off server
> {
> if (msgrcv (msgid, &data, length, 1, 0) == -1) // problem is
> oocuring here, after receiving a message


Why are you subtracting sizeof(long) from length? Aren't you lying
to msgrcv() about the size of the struct?

--
John Gordon "Between BST melee, their spells, their warders' melee,
gordon@panix.com and their warders' procs, they put out enough damage
to make monks cry." -- Dark Tyger

Richard

2004-05-12, 9:08 pm

gordon@panix.com wrote...
> In <bc42e1a.0405102140.789ea1a2@posting.google.com> bernardpace@yahoo.com (Xarky) writes:
>
>
>
> Why are you subtracting sizeof(long) from length? Aren't you lying
> to msgrcv() about the size of the struct?


Right; msgsnd() needs the sizeof(long) subtracted, but msgrcv() needs
the entire, actual, length.

--
History shows us that vast gaps in wealth distribution are eventually
self-correcting.

Ask Marie Antoinette for details.
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com