472,993 Members | 2,566 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,993 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 3878
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: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...
3
SueHopson
by: SueHopson | last post by:
Hi All, I'm trying to create a single code (run off a button that calls the Private Sub) for our parts list report that will allow the user to filter by either/both PartVendor and PartType. On...

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.