月份:六月 2007

share memory example

Posted on

Reference from http://cboard.cprogramming.com/archive/index.php/t-326.html

shmserver.c

#include
#include
#include
#include
#include
#include
#include
#include

int main (int argc, char ** argv) {
int shmid;
void * mem;
char buffer [1024];

/* get the shared memory (handle) */
if ((shmid = shmget(IPC_PRIVATE, sizeof(buffer), IPC_CREAT|0666)) == -1) {
fprintf(stderr, "Error [%d]: shmget failed - %s.\n", errno, strerror(errno));
exit(EXIT_FAILURE);
}
printf("shmid: [%d]\n", shmid);

/* attach the memory to an address */
if ((mem = shmat(shmid, NULL, SHM_W)) == (void*) -1) {
fprintf(stderr, "Error [%d]: shmat failed - %s.\n", errno, strerror(errno));
exit(EXIT_FAILURE);
}

while (1) {
printf("Enter Message: ");
if (!fgets(buffer, sizeof(buffer), stdin) || !strncmp(buffer, "exit", 4)) {
break;
}
strncpy(mem, buffer, sizeof(buffer));
}

if (shmdt(mem) == -1) {
fprintf(stderr, "Error [%d]: shmdt failed - %s.\n", errno, strerror(errno));
exit(EXIT_FAILURE);
}

return EXIT_SUCCESS;
}

shmclient.c

#include
#include
#include
#include
#include
#include
#include
#include

int main (int argc, char ** argv) {
int shmid;
void * mem;

if (argc != 2) {
fprintf(stderr, "Usage: %s \n", argv[0]);
exit(EXIT_FAILURE);
}

shmid = atoi(argv[1]);

/* attach the memory to an address */
if ((mem = shmat(shmid, NULL, SHM_R)) == (void*) -1) {
fprintf(stderr, "Error [%d]: shmat failed - %s.\n", errno, strerror(errno));
exit(EXIT_FAILURE);
}

printf("Read from shared memory:\n[%s]\n", mem);

if (shmdt(mem) == -1) {
fprintf(stderr, "Error [%d]: shmdt failed - %s.\n", errno, strerror(errno));
exit(EXIT_FAILURE);
}

return EXIT_SUCCESS;
}

Running ther server


$ shmserver
shmid: [792]
Enter Message: hello shared memory!
Enter Message: maybey i should finish fixing the olstores table and send a report to my boss
Enter Message: or work on my robocode tank
Enter Message: exit

Running the client

$ shmclient 792
Read from shared memory:
[hello shared memory!
]
[jabba@/users/dp/bryanc/shm]
$ shmclient 792
Read from shared memory:
[maybey i should finish fixing the olstores table and send a report to my boss
]
[jabba@/users/dp/bryanc/shm]
$ shmclient 792
Read from shared memory:
[or work on my robocode tank
]

How to restart X window

Posted on

/etc/init.d/gdm (or kdm, by your dm) restart

Shadow Framebuffer of X Window

Posted on

There is a mode, called shadow framebuffer, where the graphics are used to display the images. All rendering is done in memory in a so-called shadow framebuffer.

Karl also had an experience that is he enabled the shadow frame buffer and XGetImage dramatically speeds up 50~100 time of non-shadow frame buffer.

I had a trial and the results are as the following statement:
on IBM A30 desktop machine, Pentium-4 500MHz cpu and 256M Bytes ram.

  1. Enable vesa framebuffer to run Xwindow and get an 1024*768*16-bits full screen frame by XShmGetImage, it takes about 160~170 milliseconds per frame.
  2. At the same condition of 1., but map /dev/fb0 frame buffer to memory and get the full screen frame from the mapped memory, it takes about 160 ~ 170 milliseconds per frame.
  3. At the same condition of 1., but use i810 driver which is auto detected by Xorg after I installed Ubuntu on IBM A30, it takes about 77 milliseconds per frame.

Reference:

dos2unix

Posted on

apt-get install sysutils