tazekaram
سه شنبه 30 مرداد 1386, 05:38 صبح
سلام به اساتید
کسی هست که بتونه برای ارتباط پردازش ها در یونیکس با استفاده از share memory va semaphor برنامه نویسی کرده باشه
اگه هست ایراد این دو برنامه من که با ERROR:segmentation fault مواجه می شه چیه؟///
/*
* Server
*/
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/sem.h>
#define SEMKEY 1234567L /* key value for semaphore */
#define SHMKEY 1234567L /* key value for shared memory */
#define PERMS 0666
static struct sembuf inc2[1] = {0, 2, 0};
static struct sembuf wait0[1] = {0, 0, 0};
int shmid, semid;
char *a;
main()
{
if (semid = semget(SEMKEY, 1, IPC_CREAT | PERMS)) < 0) {
printf(" Error in semget.\n");
exit(-1);
}
if (shmid = shmget(SHMKEY, 100, IPC_CREAT | PERMS) < 0) {
printf(" Error in shmget.\n");
exit(-1);
}
a = shmat(shmid, 0, 0);
while (1) {
gen_data(a);
semop(semid, inc2, 1); /* increment the semaphore value by 2 */
semop(semid, wait0, 1); /* wait for the semaphore to become 0 */
}
if (shmdt(a) < 0) {
printf(" Error in shmdt.\n");
exit(-1);
}
if (shmctl(shmid, IPC_RMID, 0) < 0) {
printf(" Error in shmctl.\n");
exit(-1);
}
if (semctl(semid, IPC_RMID, 0) < 0) {
printf(" Error in semctl.\n");
exit(-1);
}
}
/*
* Server
*/
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/sem.h>
#define SEMKEY 1234567L /* key value for semaphore */
#define SHMKEY 1234567L /* key value for shared memory */
#define PERMS 0666
static struct sembuf inc2[1] = {0, 2, 0};
static struct sembuf wait0[1] = {0, 0, 0};
int shmid, semid;
char *a;
main()
{
if (semid = semget(SEMKEY, 1, IPC_CREAT | PERMS)) < 0) {
printf(" Error in semget.\n");
exit(-1);
}
if (shmid = shmget(SHMKEY, 100, IPC_CREAT | PERMS) < 0) {
printf(" Error in shmget.\n");
exit(-1);
}
a = shmat(shmid, 0, 0);
while (1) {
gen_data(a);
semop(semid, inc2, 1); /* increment the semaphore value by 2 */
semop(semid, wait0, 1); /* wait for the semaphore to become 0 */
}
if (shmdt(a) < 0) {
printf(" Error in shmdt.\n");
exit(-1);
}
if (shmctl(shmid, IPC_RMID, 0) < 0) {
printf(" Error in shmctl.\n");
exit(-1);
}
if (semctl(semid, IPC_RMID, 0) < 0) {
printf(" Error in semctl.\n");
exit(-1);
}
}
/*
* Client
*/
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/sem.h>
#define SEMKEY 1234567L /* key value for semaphore */
#define SHMKEY 1234567L /* key value for shared memory */
#define PERMS 0666
static struct sembuf dec1[1] = {0, 1, 0};
int shmid, semid;
char *a;
main()
{
if (semid = semget(SEMKEY, 1, IPC_CREAT | PERMS)) < 0) {
printf(" Error in semget.\n");
exit(-1);
}
if (shmid = shmget(SHMKEY, 100, IPC_CREAT | PERMS) < 0) {
printf(" Error in shmget.\n");
exit(-1);
}
a = shmat(shmid, 0, 0);
while (1) {
semop(semid, dec1, 1); /* wait until the semaphore value become
positive, then decrement it by 1 */
consume_data(a);
semop(semid, dec1, 1); /* decrement the semaphore value by 1 */
}
if (shmdt(a) < 0) {
printf(" Error in shmdt.\n");
exit(-1);
}
if (shmctl(shmid, IPC_RMID, 0) < 0) {
printf(" Error in shmctl.\n");
exit(-1);
}
if (semctl(semid, IPC_RMID, 0) < 0) {
printf(" Error in semctl.\n");
exit(-1);
}
}
که به جای gen_data(a ) , consume_data (a) مثلا می نویسیم printf("s,a%") یا fgets(a,1,stdin)
کسی هست که بتونه برای ارتباط پردازش ها در یونیکس با استفاده از share memory va semaphor برنامه نویسی کرده باشه
اگه هست ایراد این دو برنامه من که با ERROR:segmentation fault مواجه می شه چیه؟///
/*
* Server
*/
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/sem.h>
#define SEMKEY 1234567L /* key value for semaphore */
#define SHMKEY 1234567L /* key value for shared memory */
#define PERMS 0666
static struct sembuf inc2[1] = {0, 2, 0};
static struct sembuf wait0[1] = {0, 0, 0};
int shmid, semid;
char *a;
main()
{
if (semid = semget(SEMKEY, 1, IPC_CREAT | PERMS)) < 0) {
printf(" Error in semget.\n");
exit(-1);
}
if (shmid = shmget(SHMKEY, 100, IPC_CREAT | PERMS) < 0) {
printf(" Error in shmget.\n");
exit(-1);
}
a = shmat(shmid, 0, 0);
while (1) {
gen_data(a);
semop(semid, inc2, 1); /* increment the semaphore value by 2 */
semop(semid, wait0, 1); /* wait for the semaphore to become 0 */
}
if (shmdt(a) < 0) {
printf(" Error in shmdt.\n");
exit(-1);
}
if (shmctl(shmid, IPC_RMID, 0) < 0) {
printf(" Error in shmctl.\n");
exit(-1);
}
if (semctl(semid, IPC_RMID, 0) < 0) {
printf(" Error in semctl.\n");
exit(-1);
}
}
/*
* Server
*/
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/sem.h>
#define SEMKEY 1234567L /* key value for semaphore */
#define SHMKEY 1234567L /* key value for shared memory */
#define PERMS 0666
static struct sembuf inc2[1] = {0, 2, 0};
static struct sembuf wait0[1] = {0, 0, 0};
int shmid, semid;
char *a;
main()
{
if (semid = semget(SEMKEY, 1, IPC_CREAT | PERMS)) < 0) {
printf(" Error in semget.\n");
exit(-1);
}
if (shmid = shmget(SHMKEY, 100, IPC_CREAT | PERMS) < 0) {
printf(" Error in shmget.\n");
exit(-1);
}
a = shmat(shmid, 0, 0);
while (1) {
gen_data(a);
semop(semid, inc2, 1); /* increment the semaphore value by 2 */
semop(semid, wait0, 1); /* wait for the semaphore to become 0 */
}
if (shmdt(a) < 0) {
printf(" Error in shmdt.\n");
exit(-1);
}
if (shmctl(shmid, IPC_RMID, 0) < 0) {
printf(" Error in shmctl.\n");
exit(-1);
}
if (semctl(semid, IPC_RMID, 0) < 0) {
printf(" Error in semctl.\n");
exit(-1);
}
}
/*
* Client
*/
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/sem.h>
#define SEMKEY 1234567L /* key value for semaphore */
#define SHMKEY 1234567L /* key value for shared memory */
#define PERMS 0666
static struct sembuf dec1[1] = {0, 1, 0};
int shmid, semid;
char *a;
main()
{
if (semid = semget(SEMKEY, 1, IPC_CREAT | PERMS)) < 0) {
printf(" Error in semget.\n");
exit(-1);
}
if (shmid = shmget(SHMKEY, 100, IPC_CREAT | PERMS) < 0) {
printf(" Error in shmget.\n");
exit(-1);
}
a = shmat(shmid, 0, 0);
while (1) {
semop(semid, dec1, 1); /* wait until the semaphore value become
positive, then decrement it by 1 */
consume_data(a);
semop(semid, dec1, 1); /* decrement the semaphore value by 1 */
}
if (shmdt(a) < 0) {
printf(" Error in shmdt.\n");
exit(-1);
}
if (shmctl(shmid, IPC_RMID, 0) < 0) {
printf(" Error in shmctl.\n");
exit(-1);
}
if (semctl(semid, IPC_RMID, 0) < 0) {
printf(" Error in semctl.\n");
exit(-1);
}
}
که به جای gen_data(a ) , consume_data (a) مثلا می نویسیم printf("s,a%") یا fgets(a,1,stdin)