äúåëðéåú
äáàåú îîçéùåú ùéîåù áñâîðèé æéëøåï îùåúôéí, åùéîåù
áòøëéí ùåðé àôñ ùì key:
#include
<sys/shm.h>
#include <sys/ipc.h>
#include
<sys/types.h>
int shmget(key_t
key, int size, int shmflg);
void *shmat(int
shmid, const void *shmaddr,
int shmflg);
int shmctl(int
shmid, int cmd, struct shmid_ds
*buf);
int shmdt(const void *shmaddr);
/* memdemo31.c - demonstrate shared
memory segments - UNIX system V */
#include
<stdio.h>
#include
<sys/types.h>
#include
<sys/ipc.h>
#include
<sys/shm.h>
void sys_err(char s[])
{
perror(s);
exit(1);
}
void main()
{
int
id;
volatile int *nptr;
int
memid;
struct
shmid_ds buff;
if ( (memid = shmget(4040, sizeof(int),
0666|IPC_CREAT |
IPC_EXCL)) < 0)
sys_err("Cannot shmget");
if ( ( id = fork() ) == 0 )
{ /* select child process */
puts("\n************** child process ***********\n");
if ( (nptr = (int *)shmat(memid,0,0))
== (int *) -1)
sys_err("Cannot shmat");
*nptr = 999;
printf("PID is %d and ID is %d.\n", getpid(),
id);
printf("*nptr is %d and nptr is %p.\n", *nptr, nptr);
puts("\n************** child process ***********\n");
sleep(6);
puts("\n************** child process ***********\n");
printf("PID is %d and ID is %d.\n", getpid(),
id);
printf("*nptr is %d and nptr is %p.\n", *nptr, nptr);
puts("\n************** child process ***********\n");
if (shmdt(nptr) < 0)
sys_err("Cannot shmdt");
/* Release the
shared segment */
if ( shmctl(memid, IPC_RMID,
&buff) < 0 )
sys_err("Cannot shmctl");
/* Note that
child terminates last */
exit(0);
} /* if */
/*
Parent process only */
sleep(5);
if ( (nptr = (int *)shmat(memid,0,0))
== (int *) -1)
sys_err("Cannot shmat");
puts("\n************** parent process ***********\n");
printf("PID is %d and ID is %d.\n", getpid(),
id);
printf("*nptr is %d and nptr is %p.\n", *nptr, nptr);
*nptr = 444;
printf("*nptr is %d and nptr is %p.\n", *nptr, nptr);
puts("\n************** parent process ***********\n");
if (shmdt(nptr) < 0)
sys_err("Cannot shmdt");
}
__________________________________________________
math2:/home/ronn/OS > cc memdemo31.c
math2:/home/ronn/OS > a.out
************** child process
***********
PID is 29419 and ID is 0.
*nptr is 999 and nptr is ff170000.
************** child process
***********
************** parent process
***********
PID is 29418 and ID is 29419.
*nptr is 999 and nptr is ff170000.
*nptr is 444 and nptr is ff170000.
************** parent process
***********
math2:/home/ronn/OS >
************** child process
***********
PID is 29419 and ID is 0.
*nptr is 444 and nptr is ff170000.
************** child process
***********
/*
memdemo32.c - demonstrate shared memory segments - UNIX system V */
#include
<stdio.h>
#include
<sys/types.h>
#include
<sys/ipc.h>
#include
<sys/shm.h>
void sys_err(char s[])
{
perror(s);
exit(1);
}
void main()
{
int
id;
volatile int *nptr;
int
memid;
struct
shmid_ds buff;
if ( (memid = shmget(IPC_PRIVATE, sizeof(int), 0666)) < 0)
sys_err("Cannot shmget");
if ( ( id = fork() ) == 0 )
{ /* select child process */
puts("\n************** child process ***********\n");
if ( (nptr = (int *)shmat(memid,0,0))
== (int *) -1)
sys_err("Cannot shmat");
*nptr = 999;
printf("PID is %d and ID is %d.\n", getpid(),
id);
printf("*nptr is %d and nptr is %p.\n", *nptr, nptr);
puts("\n************** child process ***********\n");
sleep(6);
puts("\n************** child process ***********\n");
printf("PID is %d and ID is %d.\n", getpid(),
id);
printf("*nptr is %d and nptr is %p.\n", *nptr, nptr);
puts("\n************** child process ***********\n");
if (shmdt(nptr) < 0)
sys_err("Cannot shmdt");
/* Release the
shared segment */
if ( shmctl(memid, IPC_RMID,
&buff) < 0 )
sys_err("Cannot shmctl");
/* Note that
child terminates last */
exit(0);
} /* if */
/*
Parent process only */
sleep(5);
if ( (nptr = (int *)shmat(memid,0,0))
== (int *) -1)
sys_err("Cannot shmat");
puts("\n************** parent process ***********\n");
printf("PID is %d and ID is %d.\n", getpid(),
id);
printf("*nptr is %d and nptr is %p.\n", *nptr, nptr);
*nptr = 444;
printf("*nptr is %d and nptr is %p.\n", *nptr, nptr);
puts("\n************** parent process ***********\n");
if (shmdt(nptr) < 0)
sys_err("Cannot shmdt");
}
/* main */
_____________________________________________________________
math:/home3/ronn/OS > cc memdemo32.c
math:/home3/ronn/OS > ./a.out
************** child process
***********
PID is 9109 and ID is 0.
*nptr is 999 and nptr is
0x40018000.
************** child process
***********
************** parent process ***********
PID is 9108 and ID is 9109.
*nptr is 999 and nptr is
0x40017000.
*nptr is 444 and nptr is
0x40017000.
************** parent process
***********
math:/home3/ronn/OS >
************** child process
***********
PID is 9109 and ID is 0.
*nptr is 444 and nptr is
0x40018000.
************** child process
***********
/*
memdemo4.c - demonstrate shared memory segments - UNIX system V */
/*
sperate shmget and shmat */
#include
<stdio.h>
#include
<sys/types.h>
#include
<sys/ipc.h>
#include
<sys/shm.h>
void sys_err(char s[])
{
perror(s);
exit(1);
}
void main()
{
int
id;
volatile int *nptr;
int
memid;
struct
shmid_ds buff;
if ( ( id = fork() ) == 0 )
{ /* select child process */
puts("\n************** child process ***********\n");
if ( (memid = shmget(4040, sizeof(int), 0666|IPC_CREAT))
< 0)
sys_err("Cannot shmget");
if ( (nptr = (int *)shmat(memid,0,0))
== (int *) -1)
sys_err("Cannot shmat");
*nptr = 999;
printf("PID is %d and ID is %d.\n", getpid(),
id);
printf("*nptr is %d and nptr is %p.\n", *nptr, nptr);
puts("\n************** child process ***********\n");
sleep(6);
puts("\n************** child process ***********\n");
printf("PID is %d and ID is %d.\n", getpid(),
id);
printf("*nptr is %d and nptr is %p.\n", *nptr, nptr);
puts("\n************** child process ***********\n");
if (shmdt(nptr) < 0)
sys_err("Cannot shmdt");
/* Release the
shared segment */
if ( shmctl(memid, IPC_RMID,
&buff) < 0 )
sys_err("Cannot shmctl");
/* Note that
child terminates last */
exit(0);
}
/* Parent process only */
sleep(5);
if ( (memid = shmget(4040, sizeof(int), 0666)) < 0)
sys_err("Cannot shmget");
if ( (nptr = (int *)shmat(memid,0,0))
== (int *) -1)
sys_err("Cannot shmat");
puts("\n************** parent process ***********\n");
printf("PID is %d and ID is %d.\n", getpid(),
id);
printf("*nptr is %d and nptr is %p.\n", *nptr, nptr);
*nptr = 444;
printf("*nptr is %d and nptr is %p.\n", *nptr, nptr);
puts("\n************** parent process ***********\n");
if (shmdt(nptr) < 0)
sys_err("Cannot shmdt");
}
__________________________________________________
mathcs31:/home4/ronn/OS > cc
memdemo4.c
mathcs31:/home4/ronn/OS > a.out
************** child process
***********
PID is 7942 and ID is 0.
*nptr is 999 and nptr is 10000.
************** child process
***********
************** parent process
***********
PID is 8103 and ID is 7942.
*nptr is 999 and nptr is 10000.
*nptr is 444 and nptr is 10000.
************** parent process
***********
mathcs31:/home4/ronn/OS >
************** child process
***********
PID is 7942 and ID is 0.
*nptr is 444 and nptr is 10000.
************** child process
***********
/* memdemo41.c - demonstrate shared
memory segments - UNIX system V */
/*
sperate shmget and shmat */
#include
<stdio.h>
#include
<sys/types.h>
#include
<sys/ipc.h>
#include
<sys/shm.h>
void sys_err(char s[])
{
perror(s);
exit(1);
}
void main()
{
int
id;
volatile int *nptr;
int
memid;
struct
shmid_ds buff;
if ( ( id = fork() ) == 0 )
{ /* select child process */
puts("\n************** child process ***********\n");
if ( (memid = shmget(4040, sizeof(int),
0666| IPC_CREAT |
IPC_EXCL)) < 0)
sys_err("Cannot shmget");
if ( (nptr = (int *)shmat(memid,0,0))
== (int *) -1)
sys_err("Cannot shmat");
*nptr = 999;
printf("PID is %d and ID is %d.\n", getpid(),
id);
printf("*nptr is %d and nptr is %p.\n", *nptr, nptr);
puts("\n************** child process ***********\n");
sleep(6);
puts("\n************** child process ***********\n");
printf("PID is %d and ID is %d.\n", getpid(),
id);
printf("*nptr is %d and nptr is %p.\n", *nptr, nptr);
puts("\n************** child process ***********\n");
if (shmdt(nptr) < 0)
sys_err("Cannot shmdt");
/* Release the
shared segment */
if ( shmctl(memid, IPC_RMID,
&buff) < 0 )
sys_err("Cannot shmctl");
/* Note that child terminates last */
exit(0);
} /* if */
/*
Parent process only */
sleep(5);
if ( (memid = shmget(4040, sizeof(int), 0666)) < 0)
sys_err("Cannot shmget");
if ( (nptr = (int *)shmat(memid,0,0))
== (int *) -1)
sys_err("Cannot shmat");
puts("\n************** parent process ***********\n");
printf("PID is %d and ID is %d.\n", getpid(),
id);
printf("*nptr is %d and nptr is %p.\n", *nptr, nptr);
*nptr = 444;
printf("*nptr is %d and nptr is %p.\n", *nptr, nptr);
puts("\n************** parent process ***********\n");
if (shmdt(nptr) < 0)
sys_err("Cannot shmdt");
}
sci2:/home3/ronn/OS > cc memdemo41.c
sci2:/home3/ronn/OS >
./a.out
************** child process
***********
PID is 14705 and ID is 0.
*nptr is 999 and nptr is
0xb7f09000.
************** child process
***********
************** parent process
***********
PID is 14704 and ID is 14705.
*nptr is 999 and nptr is
0xb7f0a000.
*nptr is 444
and nptr is 0xb7f0a000.
************** parent process
***********
sci2:/home3/ronn/OS >
************** child process
***********
PID is 14705 and ID is 0.
*nptr is 444 and nptr is
0xb7f09000.
************** child process
***********
/* memdemo5a.c - demonstrate shared
memory segments - UNIX system V */
/*
opposite extreme shmget and shmat
before fork - recommended
whenever
possible */
#include
<stdio.h>
#include
<sys/types.h>
#include
<sys/ipc.h>
#include
<sys/shm.h>
void sys_err(char s[])
{
perror(s);
exit(1);
}
void main()
{
int
id;
volatile int *nptr;
int
memid;
struct
shmid_ds buff;
if ( (memid = shmget(IPC_PRIVATE, sizeof(int), 0666)) < 0)
sys_err("Cannot shmget");
if ( (nptr = (int *)shmat(memid,0,0))
== (int *) -1)
sys_err("Cannot shmat");
if ( ( id = fork() ) == 0 )
{ /* select child process */
puts("\n************** child process ***********\n");
*nptr = 999;
printf("PID is %d and ID is %d.\n", getpid(),
id);
printf("*nptr is %d and nptr is %p.\n", *nptr, nptr);
puts("\n************** child process ***********\n");
sleep(6);
puts("\n************** child process ***********\n");
printf("PID is %d and ID is %d.\n", getpid(),
id);
printf("*nptr is %d and nptr is %p.\n", *nptr, nptr);
puts("\n************** child process ***********\n");
if (shmdt(nptr) < 0)
sys_err("Cannot shmdt");
/* Release the
shared segment */
if ( shmctl(memid, IPC_RMID,
&buff) < 0 )
sys_err("Cannot shmctl");
/* Note that
child terminates last */
exit(0);
} /* main */
/*
Parent process only */
sleep(5);
puts("\n************** parent process ***********\n");
printf("PID is %d and ID is %d.\n", getpid(),
id);
printf("*nptr is %d and nptr is %p.\n", *nptr, nptr);
*nptr = 444;
printf("*nptr is %d and nptr is %p.\n", *nptr, nptr);
puts("\n************** parent process ***********\n");
if (shmdt(nptr) < 0)
sys_err("Cannot shmdt");
}
___________________________________________________
alpha6:/home/ronn/OS > cc memdemo5a.c
alpha6:/home/ronn/OS > a.out
************** child process
***********
PID is 2572 and ID is 0.
*nptr is 999 and nptr is 10000.
************** child process
***********
************** parent process
***********
PID is 2594 and ID is 2572.
*nptr is 999 and nptr is 10000.
*nptr is 444 and nptr is 10000.
************** parent process
***********
alpha6:/home/ronn/OS >
************** child process
***********
PID is 2572 and ID is 0.
*nptr is 444 and nptr is 10000.
************** child process
***********