
- Forum
- Programming Talk
- C and C++
- shared memory and semaphores
shared memory and semaphores
This is a discussion on shared memory and semaphores within the C and C++ forums, part of the Programming Talk category; Hi, I have a IPC program which uses shared memory and semaphores. I get a segmentation fault. The files i ...
-
shared memory and semaphores
Hi,
I have a IPC program which uses shared memory and semaphores. I get a segmentation fault. The files i am using are video files which can contain null characters also.
I have used gdb and strace but could not figure out the problem.
Plz help me to fix this.
#include<fcntl.h>
#include<stdio.h>
#include<unistd.h>
#include<string.h>
#include<sys/ipc.h>
#include<sys/shm.h>
#include<sys/sem.h>
#include<sys/stat.h>
#include<sys/types.h>
#define SHM_SIZE 1024
#define SEM_EMPTY 0
#define SEM_FULL 1
union semun{
int val; /* value for SETVAL */
struct semid_ds *buf; /* buffer for IPC_STAT, IPC_SET */
unsigned short *array; /* array for GETALL, SETALL */
/* Linux specific part: */
struct seminfo *__buf; /* buffer for IPC_INFO */
};
main()
{
int shmid, status,i,SemID;
int read_id, write_id;
int file_length;
pid_t pid;
char *ptr, *shmptr;
char read_char, write_char;
char read_buf[SHM_SIZE], write_buf[SHM_SIZE];
union semun Mysemun;
struct sembuf WaitEmpty={SEM_EMPTY, -1, SEM_UNDO};
struct sembuf SignalEmpty={SEM_EMPTY, 1, IPC_NOWAIT};
struct sembuf WaitFull={SEM_FULL, -1, SEM_UNDO};
struct sembuf SignalFull={SEM_FULL, 1, IPC_NOWAIT};
struct shmid_ds Myshmid_ds;
FILE *fp1, *fp2;
SemID = semget(IPC_PRIVATE, 2, 0666 | IPC_CREAT);
Mysemun.val = SHM_SIZE;
semctl(SemID, SEM_EMPTY, SETVAL, Mysemun);
Mysemun.val=0;
semctl(SemID, SEM_FULL, SETVAL, Mysemun);
if((shmid = shmget(IPC_PRIVATE, SHM_SIZE, 0666 | IPC_CREAT))<0)
{
perror(\"shmget\"
;
exit(1);
}
pid = fork();
if(pid == 0)
{
semop(SemID, &WaitFull, 1);
printf(\"in child process\\n\"
;
if((shmptr = shmat(shmid, 0, SHM_R)) == (void *) -1)
{
perror(\"child shmat\"
;
exit(1);
}
fp2 = fopen(\"myfile.264\",\"w\"
;
if(fp2 == NULL)
{
perror(\"file write\"
;
exit(1);
}
//while(shmptr != NULL)
while(1) // what would be the better check here
{
bzero(&write_buf, SHM_SIZE);
memcpy(write_buf, shmptr, SHM_SIZE);
fwrite(write_buf, sizeof(char), SHM_SIZE, fp2);
}
semop(SemID, &SignalEmpty, 1);
}
else
{
semop(SemID, &WaitEmpty, 1);
printf(\"In parent process\\n\"
;
if((shmptr = shmat(shmid, 0, SHM_W)) == (void *) -1)
{
perror(\"parent shmat\"
;
exit(1);
}
fp1 = fopen(\"test.264\",\"r\"
;
if(fp1 == NULL)
{
perror(\"file read\"
;
exit(1);
}
while(!feof(fp1))
{
fread(read_buf,sizeof(char),SHM_SIZE,fp1);
memcpy(shmptr, read_buf, SHM_SIZE);
bzero(&read_buf, SHM_SIZE);
}
semop(SemID, &SignalFull, 1);
wait(&status);
}
shmctl(shmid, IPC_RMID, 0);
semctl(SemID, SEM_EMPTY, IPC_RMID, Mysemun);
fclose(fp1);
fclose(fp2);
}
thanks a lot
from
Jagadish
-
08-24-2004, 04:42 PM #2dhee Guest
Re:shared memory and semaphores
can you also paste a gdb trace ?
run the executable under gdb and when it fails, issue the commant \'bt\'
dheeraj

Reply With Quote





