Exercise 6
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
int key=2709;
char *messu[] = {
"Hello",
"Countdown",
"5", "4","3","1","sorry, 2","1","GO\007",
"------------------------------"
};
int n=10;
char *shmat();
main()
{
int id,i;
char *seg;
id = shmget(key,1024,IPC_CREAT | 0777);
if (id == -1) {perror("shmget"); exit(1);}
seg = shmat(id,(char *)0,0);
for(i=0;;) {
sprintf(seg,"%s",messu[i]);
i++; i = i % n;
sleep(1);
}
}
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
char *shmat();
int key=2709;
main()
{
int id,i;
char *seg;
id = shmget(key,1024, SHM_RDONLY);
if (id == -1) {perror("shmget"); exit(1);}
seg = shmat(id,(char *)0,0);
while(1){
printf("%s\n",seg);
sleep(1);
}
}
ctrl-c
to quit the programs.