473,626 Members | 3,234 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Help me about segmentation fault (core dump)

I have wrote this code.. But when i try to compile it using gcc in
PCLinuxOS 2007, i got this message "Help me about segmentation fault
(core dump)" when I run the client script..

//CLIENT
#include <stdio.h>
#include <sys/types.h>
#include <sys/sem.h>
#include <sys/ipc.h>
#include <string.h>

struct data {
int angka[10];
} *ptr;
void oper (int semid, int val) {
struct sembuf op;
op.sem_num = 0;
op.sem_op = val;
op.sem_flg = 0;
semop(semid, &op, 1);
}

int cekAngka (int n) {
int i;
printf("%d\n", ptr->angka[n]);
if (ptr->angka[n] < 0 || ptr->angka[n] 9) return 0;
if (n >= 1) {
for (i=0;i<n;i++) {
if (ptr->angka[n] == ptr->angka[i]) return 0; // false
}
}
return 1; // true
}
int main () {
//struct data *ptr;
int semid;
int shmid, n;
// int x;
char dummy;
system("clear") ;
shmid = shmget(1234, sizeof(struct data), 0666 | IPC_CREAT);

ptr = (struct data *) shmat(shmid, 0, 0); //nti akan di shmdt

semid = semget(1235, 1, 0666 | IPC_CREAT);
semctl (semid, 0, SETVAL, 0);

//logika nya :
do {
//oper(semid, 1);
//status = 0;
n=0;
printf("Client\ n\n");
printf("Himpuna n A\n");
while (n<5) {
do {
printf("Masukka n data %d [0-9] (0 exit) : ", (n+1));
scanf("%d", &ptr->angka[n]); // this is the bug.. Plz help me with
this.
scanf("%c", &dummy);
//ptr->angka[n] = x;

} while (cekAngka(n) == 0);
n = n+1;
}
oper(semid, 1);
oper(semid,-1);
} while (1);

shmdt(ptr);

return 0;
}
// SERVER
#include <stdio.h>
#include <sys/types.h>
#include <sys/sem.h>
#include <sys/ipc.h>
#include <string.h>

struct data {
int angka[10];
} *ptr;
void oper (int semid, int val) {
struct sembuf op;
op.sem_num = 0;
op.sem_op = val;
op.sem_flg = 0;
semop(semid, &op, 1);
}
int main () {
//struct data *ptr;
int semid, i;
int shmid;
char dummy;
system("clear") ;
shmid = shmget(1234, sizeof(struct data), 0666 | IPC_CREAT);

ptr = (struct data *) shmat(shmid, 0, 0); //nti akan di shmdt

semid = semget(1235, 1, 0666 | IPC_CREAT);
semctl (semid, 0, SETVAL, 0);

//logika nya :
do {
oper(semid, -1);

printf("\n\nSer ver\n\n");
//oper(semid,-1);
for(i=0;i<5;i++ ) {
printf("angka yang dimasukkan di client : %d\n", ptr->angka[i]);
}
oper(semid, 1);
} while(1);

shmdt(ptr);
shmctl(shmid, IPC_RMID, 0);
semctl(semid, 0, IPC_RMID);

return 0;
}
Jan 19 '08 #1
4 3919
In article <80************ *************** *******@c23g200 0hsa.googlegrou ps.com>,
Willy Wijaya <wi**********@g mail.comwrote:
>I have wrote this code.. But when i try to compile it using gcc in
PCLinuxOS 2007, i got this message "Help me about segmentation fault
(core dump)" when I run the client script..
Most of the routines that you call are not on topic for comp.lang.c,
as they involve operating-system-specific extension libraries
that are not part of C itself.

> ptr = (struct data *) shmat(shmid, 0, 0); //nti akan di shmdt
In both client and server, you fail to test the result of the shmat
before you use it. If shmat() returns a null pointer, then
you are going to get some kind of memory problem such as a
segmentation fault.

I didn't bother looking for other similar problems.
--
"I was very young in those days, but I was also rather dim."
-- Christopher Priest
Jan 19 '08 #2
THX for ur help, Walter Roberson.. Thx a lot.. I am a beginner in C
programming.. ^^
Jan 22 '08 #3
Thx for your help, Walter Roberson!!.. I am a beginner in c
programming.. ^^v
On 19 Jan, 17:17, rober...@ibd.nr c-cnrc.gc.ca (Walter Roberson) wrote:
In article <801d5476-0a2b-4c9c-970c-66b61a2c6...@c2 3g2000hsa.googl egroups.com>,
Willy Wijaya *<wijaya.wi...@ gmail.comwrote:
I have wrote this code.. But when i try to compile it using gcc in
PCLinuxOS 2007, i got this message "Help me about segmentation fault
(core dump)" when I run the client script..

Most of the routines that you call are not on topic for comp.lang.c,
as they involve operating-system-specific extension libraries
that are not part of C itself.
* *ptr = (struct data *) shmat(shmid, 0, 0); //nti akan di shmdt

In both client and server, you fail to test the result of the shmat
before you use it. If shmat() returns a null pointer, then
you are going to get some kind of memory problem such as a
segmentation fault.

I didn't bother looking for other similar problems.
--
* *"I was very young in those days, but I was also rather dim."
* *-- Christopher Priest
Jan 23 '08 #4
On Jan 19, 3:42*am, Willy Wijaya <wijaya.wi...@g mail.comwrote:
I have wrote this code.. But when i try to compile it using gcc in
PCLinuxOS 2007, i got this message "Help me about segmentation fault
(core dump)" when I run the client script..

//CLIENT
#include <stdio.h>
#include <sys/types.h>
#include <sys/sem.h>
#include <sys/ipc.h>
#include <string.h>

struct data {
* * * * int angka[10];

} *ptr;

void oper (int semid, int val) {
* * * * struct sembuf op;
* * * * op.sem_num = 0;
* * * * op.sem_op = val;
* * * * op.sem_flg = 0;
* * * * semop(semid, &op, 1);

}

int cekAngka (int n) {
* * * * int i;
* * * * printf("%d\n", ptr->angka[n]);
* * * * if (ptr->angka[n] < 0 || ptr->angka[n] 9) return 0;
* * * * if (n >= 1) {
* * * * * * * * for (i=0;i<n;i++) {
* * * * * * * * * * * * if (ptr->angka[n] == ptr->angka[i]) return 0; // false
* * * * * * * * }
* * * * }
* * * * return 1; // true

}

int main () {
* * * * //struct data *ptr;
* * * * int semid;
* * * * int shmid, n;
* * * * // int x;
* * * * char dummy;
* * * * system("clear") ;
* * * * shmid = shmget(1234, sizeof(struct data), 0666 | IPC_CREAT);

* * * * ptr = (struct data *) shmat(shmid, 0, 0); //nti akan di shmdt

* * * * semid = semget(1235, 1, 0666 | IPC_CREAT);
* * * * semctl (semid, 0, SETVAL, 0);

* * * * //logika nya :
* * * * do {
* * * * * * * * //oper(semid, 1);
* * * * * * * * //status = 0;
* * * * * * * * n=0;
* * * * * * * * printf("Client\ n\n");
* * * * * * * * printf("Himpuna n A\n");
* * * * * * * * while (n<5) {
* * * * * * * * * * * * do {
* * * * * * * * * * * * * * * * printf("Masukka n data %d [0-9] (0 exit) : ", (n+1));
* * * * * * * * * * * * * * * * scanf("%d", &ptr->angka[n]); // this is the bug.. Plz help me with
this.
* * * * * * * * * * * * * * * * scanf("%c", &dummy);
* * * * * * * * * * * * * * * * //ptr->angka[n] = x;

* * * * * * * * * * * * } while (cekAngka(n) == 0);
* * * * * * * * * * * * n = n+1;
* * * * * * * * }
* * * * * * * * oper(semid, 1);
* * * * * * * * oper(semid,-1);
* * * * } while (1);

* * * * shmdt(ptr);

* * * * return 0;

}

// SERVER
#include <stdio.h>
#include <sys/types.h>
#include <sys/sem.h>
#include <sys/ipc.h>
#include <string.h>

struct data {
* * * * int angka[10];

} *ptr;

void oper (int semid, int val) {
* * * * struct sembuf op;
* * * * op.sem_num = 0;
* * * * op.sem_op = val;
* * * * op.sem_flg = 0;
* * * * semop(semid, &op, 1);

}

int main () {
* * * * //struct data *ptr;
* * * * int semid, i;
* * * * int shmid;
* * * * char dummy;
* * * * system("clear") ;
* * * * shmid = shmget(1234, sizeof(struct data), 0666 | IPC_CREAT);

* * * * ptr = (struct data *) shmat(shmid, 0, 0); //nti akan di shmdt

* * * * semid = semget(1235, 1, 0666 | IPC_CREAT);
* * * * semctl (semid, 0, SETVAL, 0);

* * * * //logika nya :
* * * * do {
* * * * * * * * oper(semid, -1);

* * * * * * * * printf("\n\nSer ver\n\n");
* * * * * * * * //oper(semid,-1);
* * * * * * * * for(i=0;i<5;i++ ) {
* * * * * * * * * * * * printf("angka yang dimasukkan di client : %d\n", ptr->angka[i]);
* * * * * * * * }
* * * * * * * * oper(semid, 1);
* * * * } while(1);

* * * * shmdt(ptr);
* * * * shmctl(shmid, IPC_RMID, 0);
* * * * semctl(semid, 0, IPC_RMID);

* * * * return 0;
The following article might help:

http://www.eventhelix.com/RealtimeMa...re_crashes.htm

http://www.eventhelix.com/RealtimeMa...crashes_2..htm

--
EventStudio 4.0 - http://www.Eventhelix.com/Eventstudio/
Sequence diagram based systems engineering tool

}- Hide quoted text -

- Show quoted text -
Jan 24 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
6801
by: sivignon | last post by:
Hi, I'm writing a php script which deals with 3 ORACLE databases. This script is launch by a script shell on an linux machine like this : /../php/bin/php ./MySript.php (PHP 4.3.3) My script works fine and do all what I need. But at the end of the execution, I can read "Segmentation Fault". The segmentation fault appear at the end of my script execution,
9
31916
by: Maksim Kasimov | last post by:
Hello, my programm sometime gives "Segmentation fault" message (no matter how long the programm had run (1 day or 2 weeks). And there is nothing in log-files that can points the problem. My question is how it possible to find out where is the problem in the code? Thanks for any help. Python 2.2.3 FreeBSD -- Best regards,
59
2881
by: Christian Christmann | last post by:
Hi, I'm wondering why this program does not crash with a segmentation fault: #include <malloc.h> #include <string.h> #include <stdio.h> int main()
15
3759
by: conor.robinson | last post by:
I'm running operations large arrays of floats, approx 25,000 x 80. Python (scipy) does not seem to come close to using 4GB of wired mem, but segments at around a gig. Everything works fine on smaller batches of data around 10,000 x 80 and uses a max of ~600mb of mem. Any Ideas? Is this just too much data for scipy? Thanks Conor Traceback (most recent call last): File "C:\Temp\CR_2\run.py", line 68, in ?
6
4614
by: KS | last post by:
Hello, I'm writing some c++ code after a few years with Java and your help is appreciated. I am getting a core dump on a call to qsort. Can you take a look at the code and see if there are any obvious errors? (The function main is at the bottom of the file.) Code: http://www.grex.org/~kpp/cmultiknapsack.cpp To run this file you will need http://www.grex.org/~kpp/orlib1.txt in
3
5167
by: madunix | last post by:
My Server is suffering bad lag (High Utlization) I am running on that server Oracle10g with apache_1.3.35/ php-4.4.2 Web visitors retrieve data from the web by php calls through oci cobnnection from 10g release2 PHP is configured with the following parameters './configure' '--prefix=/opt/oracle/php' '--with-apxs=/opt/oracle/apache/bin/apxs' '--with-config-file-path=/opt/oracle/apache/conf' '--enable-safe-mode' '--enable-session'...
13
1477
by: Aarti | last post by:
I have a very elementary question about pointers. Please pardon me for my ignorance of C int main() { int* i; *i = 1 //at times this may give me a core dump. const char* str = "test"; //This seems to be a valid construct. }
5
6085
by: johnericaturnbull | last post by:
Hi - I am very new to python. I get this random core dump and am looking for a good way to catch the error. I know the function my core dump occurs. Is there any error catching/handling that I could use in python?
3
3793
by: vamsi | last post by:
Hi, I have a program, where involves creation of a thread with stack size of 16k(minimum stack size). There is an fprintf statement in the created thread code. I see that there is a core dump occuring at fprintf. code snippet : ********************************************************************************* #include<pthread.h>
0
8269
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8203
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8711
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8512
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7203
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6125
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4206
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2630
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1815
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.