473,395 Members | 1,442 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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("Himpunan A\n");
while (n<5) {
do {
printf("Masukkan 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\nServer\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 3905
In article <80**********************************@c23g2000hsa. googlegroups.com>,
Willy 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 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.nrc-cnrc.gc.ca (Walter Roberson) wrote:
In article <801d5476-0a2b-4c9c-970c-66b61a2c6...@c23g2000hsa.googlegroups.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...@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..

//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("Himpunan A\n");
* * * * * * * * while (n<5) {
* * * * * * * * * * * * do {
* * * * * * * * * * * * * * * * printf("Masukkan 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\nServer\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
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...
9
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...
59
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
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...
6
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...
3
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...
13
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...
5
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...
3
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...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.