468,289 Members | 1,766 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,289 developers. It's quick & easy.

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 3580
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by sivignon | last post: by
9 posts views Thread by Maksim Kasimov | last post: by
59 posts views Thread by Christian Christmann | last post: by
15 posts views Thread by conor.robinson | last post: by
6 posts views Thread by KS | last post: by
3 posts views Thread by madunix | last post: by
13 posts views Thread by Aarti | last post: by
5 posts views Thread by johnericaturnbull | last post: by
3 posts views Thread by vamsi | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.