Help me about segmentation fault (core dump)

W

Willy Wijaya

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) 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);
}
oper(semid, 1);
} while(1);

shmdt(ptr);
shmctl(shmid, IPC_RMID, 0);
semctl(semid, 0, IPC_RMID);

return 0;
}
 
W

Walter Roberson

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.
 
W

Willy Wijaya

THX for ur help, Walter Roberson.. Thx a lot.. I am a beginner in C
programming.. ^^
 
E

EventHelix.com

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) 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);
                }
                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/RealtimeMantra/Basics/debugging_software_crashes.htm

http://www.eventhelix.com/RealtimeMantra/Basics/debugging_software_crashes_2..htm
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top