Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Help me about segmentation fault (core dump)

Reply
Thread Tools

Help me about segmentation fault (core dump)

 
 
Willy Wijaya
Guest
Posts: n/a
 
      01-19-2008
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;
}
 
Reply With Quote
 
 
 
 
Walter Roberson
Guest
Posts: n/a
 
      01-19-2008
In article <(E-Mail Removed)>,
Willy Wijaya <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
Willy Wijaya
Guest
Posts: n/a
 
      01-22-2008
THX for ur help, Walter Roberson.. Thx a lot.. I am a beginner in C
programming.. ^^
 
Reply With Quote
 
Willy Wijaya
Guest
Posts: n/a
 
      01-23-2008
Thx for your help, Walter Roberson!!.. I am a beginner in c
programming.. ^^v


On 19 Jan, 17:17, (E-Mail Removed)-cnrc.gc.ca (Walter Roberson) wrote:
> In article <(E-Mail Removed)>,
> Willy Wijaya *<(E-Mail Removed)> 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


 
Reply With Quote
 
EventHelix.com
Guest
Posts: n/a
 
      01-24-2008
On Jan 19, 3:42*am, Willy Wijaya <(E-Mail Removed)> 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 -


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with array/pointer segmentation fault needed Ben C Programming 8 06-13-2006 07:10 AM
Beginner requesting help - Segmentation fault Fra-it C Programming 13 11-06-2005 11:46 PM
Segmentation fault need help Marcia Hon C Programming 4 02-16-2004 08:13 PM
Re: string calloc segmentation fault -- help! Kevin Goodsell C++ 0 09-02-2003 09:44 PM
Re: string calloc segmentation fault -- help! Andrey Tarasevich C++ 0 09-02-2003 07:03 PM



Advertisments