Re: Can shared memory data be used as a signal like gtk-input-add does with a pipe?
- From: G Hasse <gh raditex se>
- To: Marco Quezada <mquezada nlxcorp com>
- Cc: "Norris, Kendall P." <Kendall Norris Honeywell com>, "'gtk-app-devel-list gnome org'" <gtk-app-devel-list gnome org>
- Subject: Re: Can shared memory data be used as a signal like gtk-input-add does with a pipe?
- Date: Fri, 28 Sep 2001 00:40:36 +0200 (CEST)
On Thu, 27 Sep 2001, Marco Quezada wrote:
I'm not sure if this is the best and or only way to do it but I just have a
timeout that runs at a certain rate (10 Hz in my case) and looks at the value
of the variable in memory, if the value changes then it goes in and does
whatever processing it needs to do.
This is not the way to do it. The best way is in fact to use a
select or poll on a file-descriptor - but you could also use a semaphore
like this.
/*
Simple example
semaphore.c
*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/sem.h>
void main( int argc , char *argv[] )
{
int shmid, semid, i , j, size=256;
char *buf;
pid_t pid;
key_t key;
static struct sembuf raise[] = {
{0,1,0}
};
static struct sembuf isdown[] = {
{0,0,0}
};
static struct sembuf isup[] = {
{0,-1,0},
{0,1,0}
};
static struct sembuf lower[] = {
{0,-1,0}
};
if( pid = fork() ) { /* Parent */
printf("Parent: pid=%d\n", getpid() );
key = ftok( argv[0], 0 );
shmid = shmget( key, size, IPC_CREAT | SHM_R | SHM_W );
buf = shmat( shmid, 0 , 0);
semid = semget( key , 1, IPC_CREAT | SEM_R | SEM_A );
printf("Parent semid=%d\n", semid);
semop( semid, isdown, 1);
sprintf(buf, "%8.8d_", getpid() );
printf("parent: msg=%s\n", buf);
semop(semid, raise, 1);
for ( i=0; i<8 ; i++) {
semop(semid, isdown, 1);
strcat(buf, "*");
semop(semid, raise, 1);
}
sleep(1);
} else { /* Child */
sleep(2);
printf("Child: pid=%d\n", getpid() );
key = ftok( argv[0], 0 );
shmid = shmget( key, size, SHM_R | SHM_W );
buf = shmat( shmid, 0 , 0);
semid = semget( key , 1, SEM_R | SEM_A );
printf("Child semid=%d\n", semid);
semop(semid, isup, 2);
printf("child: msg=%s\n", buf);
semop(semid, lower, 1);
for( i = 0 ; i < 4; i++ ) {
semop(semid, isup, 2);
printf("child: msg=%s\n", buf );
semop(semid, lower, 1);
}
semop(semid, isup, 2);
sprintf(buf, "%8.8d_", getpid());
semop(semid, lower, 1);
for( i = 0 ; i < 4; i++ ) {
semop(semid, isup, 2);
printf("child: msg=%s\n", buf );
semop(semid, lower, 1);
}
shmdt(buf);
semctl(semid, 0, IPC_RMID, NULL); /* Remove semaphore */
shmctl(shmid, IPC_RMID, NULL); /* Remove memory */
}
}
----------------------------------------------------------------
Göran Hasse email: gh raditex se Tel: +46 8 694 92 70
Raditex AB http://www.raditex.se Fax: +46 8 442 05 91
Sickla Alle 7, 1tr Mob: 070-5530148
131 34 NACKA, SWEDEN
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]