Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   Help me about segmentation fault (core dump) (http://www.velocityreviews.com/forums/t585777-help-me-about-segmentation-fault-core-dump.html)

Willy Wijaya 01-19-2008 08:42 AM

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;
}

Walter Roberson 01-19-2008 10:17 AM

Re: Help me about segmentation fault (core dump)
 
In article <801d5476-0a2b-4c9c-970c-66b61a2c62dd@c23g2000hsa.googlegroups.com>,
Willy Wijaya <wijaya.willy@gmail.com> wrote:
>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

Willy Wijaya 01-22-2008 05:03 AM

Re: Help me about segmentation fault (core dump)
 
THX for ur help, Walter Roberson.. Thx a lot.. I am a beginner in C
programming.. ^^

Willy Wijaya 01-23-2008 02:04 AM

Re: Help me about segmentation fault (core dump)
 
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.com> wrote:
>
> >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



EventHelix.com 01-24-2008 01:05 AM

Re: Help me about segmentation fault (core dump)
 
On Jan 19, 3:42*am, Willy Wijaya <wijaya.wi...@gmail.com> wrote:
> 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 -




All times are GMT. The time now is 11:40 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.